Class: ItemBuilder::ShopifyQuantityService
- Inherits:
-
Object
- Object
- ItemBuilder::ShopifyQuantityService
- Defined in:
- lib/item_builder/shopify_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_location ⇒ Object
- #data_variant ⇒ Object
- #headers ⇒ Object
-
#initialize(args) ⇒ ShopifyQuantityService
constructor
A new instance of ShopifyQuantityService.
- #inventory_item_id ⇒ Object
- #location_id ⇒ Object
- #perform ⇒ Object
- #response_process ⇒ Object
- #rest_client(params, rescued_codes = 200) ⇒ Object
- #url_location ⇒ Object
- #url_variant ⇒ Object
Constructor Details
#initialize(args) ⇒ ShopifyQuantityService
Returns a new instance of ShopifyQuantityService.
7 8 9 10 11 12 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 7 def initialize(args) @listings = args.fetch(:listings) @skus = args.fetch(:skus) @account_id = listings[0].profile_channel_association_id @variant_listings = {} end |
Instance Attribute Details
#listings ⇒ Object (readonly)
Returns the value of attribute listings.
6 7 8 |
# File 'lib/item_builder/shopify_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/shopify_quantity_service.rb', line 6 def skus @skus end |
Instance Method Details
#credential ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 80 def credential return @credential if @credential 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_location ⇒ Object
67 68 69 70 71 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 67 def data_location { "credential": JSON.parse(credential)['credential'] }.to_json end |
#data_variant ⇒ Object
73 74 75 76 77 78 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 73 def data_variant { "credential": JSON.parse(credential)['credential'], "data": { "local_id": @sku } }.to_json end |
#headers ⇒ Object
63 64 65 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 63 def headers { content_type: :json, accept: :json } end |
#inventory_item_id ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 28 def inventory_item_id real_local_id = @listing.real_local_id listing_id = @listing.id if real_local_id.nil? || real_local_id.blank? # get data from shopify args = {method: :post, url: url_variant, payload: data_variant, headers: headers} resp = JSON.parse(rest_client(args, [200, 500, 406])) hash = { listing_id => { "real_local_id" => resp['inventory_item_id'] } } @variant_listings = @variant_listings.merge(hash) resp['inventory_item_id'] else # real_local_id exists real_local_id end end |
#location_id ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 48 def location_id cred = JSON.parse(credential)['credential'] if cred['primary_warehouse_id'].present? cred['primary_warehouse_id'] else # get data from shopify args = { method: :post, url: url_location, payload: data_location, headers: headers } resp = JSON.parse(rest_client(args, [200, 500, 406])) resp['id'] end end |
#perform ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 14 def perform datas = {} @listings.each do |listing| @listing = listing @sku = listing.local_id datas[@sku] = response_process end # update variant real_local_id database icava VariantListing.update(@variant_listings.keys, @variant_listings.values) datas end |
#response_process ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 98 def response_process hash = Hash.new hash[:inventory_item_id] = inventory_item_id.to_i hash[:location_id] = location_id.to_i hash end |
#rest_client(params, rescued_codes = 200) ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 107 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 |
#url_location ⇒ Object
93 94 95 96 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 93 def url_location url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set') url + "/shopify/data_location" end |
#url_variant ⇒ Object
88 89 90 91 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 88 def url_variant url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set') url + "/shopify/data_variant" end |