Class: ItemBuilderMwh::ShopifyQuantityService

Inherits:
Object
  • Object
show all
Defined in:
lib/item_builder_mwh/shopify_quantity_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ShopifyQuantityService

Returns a new instance of ShopifyQuantityService.



7
8
9
10
11
12
# File 'lib/item_builder_mwh/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

#listingsObject (readonly)

Returns the value of attribute listings.



6
7
8
# File 'lib/item_builder_mwh/shopify_quantity_service.rb', line 6

def listings
  @listings
end

#skusObject (readonly)

Returns the value of attribute skus.



6
7
8
# File 'lib/item_builder_mwh/shopify_quantity_service.rb', line 6

def skus
  @skus
end

Instance Method Details

#credentialObject



87
88
89
90
91
92
93
# File 'lib/item_builder_mwh/shopify_quantity_service.rb', line 87

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_locationObject



74
75
76
77
78
# File 'lib/item_builder_mwh/shopify_quantity_service.rb', line 74

def data_location
  {
    "credential": JSON.parse(credential)['credential']
  }.to_json
end

#data_variantObject



80
81
82
83
84
85
# File 'lib/item_builder_mwh/shopify_quantity_service.rb', line 80

def data_variant
  {
    "credential": JSON.parse(credential)['credential'],
    "data": { "local_id": @sku }
  }.to_json
end

#headersObject



70
71
72
# File 'lib/item_builder_mwh/shopify_quantity_service.rb', line 70

def headers
  { content_type: :json, accept: :json }
end

#inventory_item_idObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/item_builder_mwh/shopify_quantity_service.rb', line 33

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_idObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/item_builder_mwh/shopify_quantity_service.rb', line 55

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

#performObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/item_builder_mwh/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['credential'] = JSON.parse(credential)['credential']
  datas
end

#response_processObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/item_builder_mwh/shopify_quantity_service.rb', line 105

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



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/item_builder_mwh/shopify_quantity_service.rb', line 118

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_locationObject



100
101
102
103
# File 'lib/item_builder_mwh/shopify_quantity_service.rb', line 100

def url_location
  url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
  url + "/shopify/data_location"
end

#url_variantObject



95
96
97
98
# File 'lib/item_builder_mwh/shopify_quantity_service.rb', line 95

def url_variant
  url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
  url + "/shopify/data_variant"
end