Class: ItemBuilderMwh::ZaloraQuantityService
- Inherits:
-
Object
- Object
- ItemBuilderMwh::ZaloraQuantityService
- Defined in:
- lib/item_builder_mwh/zalora_quantity_service.rb
Instance Attribute Summary collapse
-
#listings ⇒ Object
readonly
Returns the value of attribute listings.
-
#skus ⇒ Object
readonly
Returns the value of attribute skus.
Instance Method Summary collapse
- #credential ⇒ Object
- #data ⇒ Object
- #headers ⇒ Object
-
#initialize(args) ⇒ ZaloraQuantityService
constructor
A new instance of ZaloraQuantityService.
- #perform ⇒ Object
- #response_process(resp) ⇒ Object
- #rest_client(params, rescued_codes = 200) ⇒ Object
- #success_handle(resp) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(args) ⇒ ZaloraQuantityService
Returns a new instance of ZaloraQuantityService.
7 8 9 10 |
# File 'lib/item_builder_mwh/zalora_quantity_service.rb', line 7 def initialize(args) @listings = args.fetch(:listings) @skus = args.fetch(:skus) end |
Instance Attribute Details
#listings ⇒ Object (readonly)
Returns the value of attribute listings.
6 7 8 |
# File 'lib/item_builder_mwh/zalora_quantity_service.rb', line 6 def listings @listings end |
#skus ⇒ Object (readonly)
Returns the value of attribute skus.
6 7 8 |
# File 'lib/item_builder_mwh/zalora_quantity_service.rb', line 6 def skus @skus end |
Instance Method Details
#credential ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/item_builder_mwh/zalora_quantity_service.rb', line 31 def credential return @credential if @credential account_id = listings[0].profile_channel_association_id host = ENV['CREDENTIAL_URL'] || raise('credential url is not set') url = "#{host}/credential?account_id=#{account_id}" @credential = rest_client(method: :get, url: url) end |
#data ⇒ Object
24 25 26 27 28 29 |
# File 'lib/item_builder_mwh/zalora_quantity_service.rb', line 24 def data { "credential": JSON.parse(credential)['credential'], "data": { "skus": skus } }.to_json end |
#headers ⇒ Object
20 21 22 |
# File 'lib/item_builder_mwh/zalora_quantity_service.rb', line 20 def headers { content_type: :json, accept: :json } end |
#perform ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/item_builder_mwh/zalora_quantity_service.rb', line 12 def perform args = { method: :post, url: url, payload: data, headers: headers } resp = JSON.parse(rest_client(args, [200, 500, 406])) response_process(resp) end |
#response_process(resp) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/item_builder_mwh/zalora_quantity_service.rb', line 45 def response_process(resp) if resp.dig("response_code") && resp.dig("response_code") != 200 raise "Response Code is #{resp.dig("response_code")}" elsif resp.dig('ErrorResponse', 'Head', 'ErrorMessage') == 'E009: Access Denied' return nil elsif resp.dig('ErrorResponse').present? return nil end success_handle(resp) end |
#rest_client(params, rescued_codes = 200) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/item_builder_mwh/zalora_quantity_service.rb', line 64 def rest_client(params, rescued_codes = 200) RestClient::Request.execute(params.merge(timeout: 3)) do |response| code = response.code unless Array.wrap(rescued_codes).include?(code) raise "Response Code is #{code}" end response end end |
#success_handle(resp) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/item_builder_mwh/zalora_quantity_service.rb', line 56 def success_handle(resp) hash = Hash.new resp.dig("SuccessResponse", "Body", "ProductStocks", "ProductStock").each do |sku| hash[sku['SellerSku']] = sku['ReservedStock'] end if resp.dig('SuccessResponse').present? hash end |
#url ⇒ Object
40 41 42 43 |
# File 'lib/item_builder_mwh/zalora_quantity_service.rb', line 40 def url url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set') url + "/zalora/product_stocks" end |