Class: ItemBuilder::Modes::Quantity::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/item_builder/modes/quantity/base.rb

Direct Known Subclasses

BlibliService, LazadaService, ZaloraService

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(listing, available_quantity) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
# File 'lib/item_builder/modes/quantity/base.rb', line 10

def initialize(listing, available_quantity)
  raise 'listing is not set' if listing.nil?

  @listing = listing
  @available_quantity = available_quantity
end

Instance Attribute Details

#available_quantityObject (readonly)

Returns the value of attribute available_quantity.



8
9
10
# File 'lib/item_builder/modes/quantity/base.rb', line 8

def available_quantity
  @available_quantity
end

#listingObject (readonly)

Returns the value of attribute listing.



7
8
9
# File 'lib/item_builder/modes/quantity/base.rb', line 7

def listing
  @listing
end

Instance Method Details

#api_dataObject



63
64
65
# File 'lib/item_builder/modes/quantity/base.rb', line 63

def api_data
  data.to_json
end

#apigateway_getObject



33
34
35
36
37
# File 'lib/item_builder/modes/quantity/base.rb', line 33

def apigateway_get
  RestClient.get("#{host}?#{params}")
rescue RestClient::ExceptionWithResponse => e
  e.response
end

#apigateway_postObject



67
68
69
70
71
# File 'lib/item_builder/modes/quantity/base.rb', line 67

def apigateway_post
  RestClient.post(url, api_data, headers)
rescue RestClient::ExceptionWithResponse => e
  e.response
end

#credentialObject



46
47
48
49
50
51
52
53
54
# File 'lib/item_builder/modes/quantity/base.rb', line 46

def credential
   = listing.profile_channel_association_id
  host = ENV['CREDENTIAL_URL'] || 'user.forstok.com'
  begin
    RestClient.get("#{host}/credential?account_id=#{}")
  rescue RestClient::ExceptionWithResponse => e
    e.response
  end
end

#dataObject



56
57
58
59
60
61
# File 'lib/item_builder/modes/quantity/base.rb', line 56

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

#headersObject



39
40
41
42
43
44
# File 'lib/item_builder/modes/quantity/base.rb', line 39

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

#order_hostObject



17
18
19
20
# File 'lib/item_builder/modes/quantity/base.rb', line 17

def order_host
  url = ENV['ORDERS_URL'] || 'orders.forstok.com'
  url + '/api/v2/item_line/reserved_stock'
end

#reserved_paramsObject



22
23
24
25
# File 'lib/item_builder/modes/quantity/base.rb', line 22

def reserved_params
  "account_id=#{listing.profile_channel_association_id}
  &item_variant_id=#{listing.variant_id}"
end

#reserved_stockObject



27
28
29
30
31
# File 'lib/item_builder/modes/quantity/base.rb', line 27

def reserved_stock
  RestClient.get("#{order_host}?#{reserved_params}")
rescue RestClient::ExceptionWithResponse => e
  e.response
end