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
84 85 86 87 88 89 90 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 84 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
71 72 73 74 75 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 71 def data_location { "credential": JSON.parse(credential)['credential'] }.to_json end |
#data_variant ⇒ Object
77 78 79 80 81 82 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 77 def data_variant { "credential": JSON.parse(credential)['credential'], "data": { "local_id": @sku } }.to_json end |
#headers ⇒ Object
67 68 69 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 67 def headers { content_type: :json, accept: :json } end |
#inventory_item_id ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 32 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, 402])) 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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 52 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, 402])) resp['id'] end end |
#perform ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 14 def perform datas = {} @listings.each do |listing| if listing.local_id.present? @listing = listing @sku = listing.local_id resp = response_process next if resp[:inventory_item_id].nil? || resp[:inventory_item_id] == 0 datas[@sku] = resp end end # update variant real_local_id database icava VariantListing.update(@variant_listings.keys, @variant_listings.values) datas end |
#response_process ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 102 def response_process hash = Hash.new begin hash[:inventory_item_id] = inventory_item_id.to_i hash[:location_id] = location_id.to_i rescue hash end hash end |
#rest_client(params, rescued_codes = 200) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 115 def rest_client(params, rescued_codes = 200) RestClient::Request.execute(params.merge(timeout: 3)) do |response| code = response.code resp = response.body.to_str unless Array.wrap(rescued_codes).include?(code) raise "Response Code is #{code}" unless resp.include?('Response code = 404') end response end end |
#url_location ⇒ Object
97 98 99 100 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 97 def url_location url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set') url + "/shopify/data_location" end |
#url_variant ⇒ Object
92 93 94 95 |
# File 'lib/item_builder/shopify_quantity_service.rb', line 92 def url_variant url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set') url + "/shopify/data_variant" end |