Class: ItemBuilder::ZaloraQuantityService

Inherits:
Object
  • Object
show all
Defined in:
lib/item_builder/zalora_quantity_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ZaloraQuantityService

Returns a new instance of ZaloraQuantityService.



7
8
9
10
# File 'lib/item_builder/zalora_quantity_service.rb', line 7

def initialize(args)
  @listings = args.fetch(:listings)
  @skus = args.fetch(:skus)
end

Instance Attribute Details

#listingsObject (readonly)

Returns the value of attribute listings.



6
7
8
# File 'lib/item_builder/zalora_quantity_service.rb', line 6

def listings
  @listings
end

#skusObject (readonly)

Returns the value of attribute skus.



6
7
8
# File 'lib/item_builder/zalora_quantity_service.rb', line 6

def skus
  @skus
end

Instance Method Details

#credentialObject



31
32
33
34
35
36
37
38
# File 'lib/item_builder/zalora_quantity_service.rb', line 31

def credential
  return @credential if @credential

   = listings[0].profile_channel_association_id
  host = ENV['CREDENTIAL_URL'] || raise('credential url is not set')
  url = "#{host}/credential?account_id=#{}"
  @credential = rest_client(method: :get, url: url)
end

#dataObject



24
25
26
27
28
29
# File 'lib/item_builder/zalora_quantity_service.rb', line 24

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

#headersObject



20
21
22
# File 'lib/item_builder/zalora_quantity_service.rb', line 20

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

#performObject



12
13
14
15
16
17
18
# File 'lib/item_builder/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
55
# File 'lib/item_builder/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



65
66
67
68
69
70
71
72
73
74
# File 'lib/item_builder/zalora_quantity_service.rb', line 65

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



57
58
59
60
61
62
63
# File 'lib/item_builder/zalora_quantity_service.rb', line 57

def success_handle(resp)
  hash = {}
  resp.dig('SuccessResponse', 'Body', 'ProductStocks', 'ProductStock').each do |sku|
    hash[sku['SellerSku']] = sku['ReservedStock']
  end if resp.dig('SuccessResponse').present?
  hash
end

#urlObject



40
41
42
43
# File 'lib/item_builder/zalora_quantity_service.rb', line 40

def url
  url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
  url + '/zalora/product_stocks'
end