Class: Seko::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/seko/client.rb

Constant Summary collapse

PORT =
443
TEST_HOST =
'hubuat1.supplystream.com'
LIVE_HOST =

api.supplystream.com

'hub.supplystream.com'
API_PATH =
'/hub/api/'
API_VERSION =
'v1'
CONTENT_TYPE =
'application/json'
KEYS_MAP =
{ 
  "FreeQuantity" => "qty", 
  "ProductCode"  => "upc" 
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, options = {}) ⇒ Client

Returns a new instance of Client.



21
22
23
24
25
26
# File 'lib/seko/client.rb', line 21

def initialize(token, options = {})
  raise "Token is required" unless token

  @token   = token
  @options = default_options.merge(options)
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



19
20
21
# File 'lib/seko/client.rb', line 19

def endpoint
  @endpoint
end

#optionsObject

Returns the value of attribute options.



19
20
21
# File 'lib/seko/client.rb', line 19

def options
  @options
end

#pathObject

Returns the value of attribute path.



19
20
21
# File 'lib/seko/client.rb', line 19

def path
  @path
end

#request_uriObject

Returns the value of attribute request_uri.



19
20
21
# File 'lib/seko/client.rb', line 19

def request_uri
  @request_uri
end

#responseObject

Returns the value of attribute response.



19
20
21
# File 'lib/seko/client.rb', line 19

def response
  @response
end

#serviceObject

Returns the value of attribute service.



19
20
21
# File 'lib/seko/client.rb', line 19

def service
  @service
end

#tokenObject

Returns the value of attribute token.



19
20
21
# File 'lib/seko/client.rb', line 19

def token
  @token
end

#typeObject

Returns the value of attribute type.



19
20
21
# File 'lib/seko/client.rb', line 19

def type
  @type
end

Instance Method Details

#cancel_order(guid, reason_code) ⇒ Object



139
140
141
142
143
# File 'lib/seko/client.rb', line 139

def cancel_order(guid, reason_code)
  @service  = 'salesorders'
  @endpoint = "#{guid}/cancel/reasoncode/#{reason_code}"
  post({'Cancel' => 'Order'})
end

#check_grn(guid) ⇒ Object



121
122
123
124
125
# File 'lib/seko/client.rb', line 121

def check_grn(guid)
  @service  = 'grns'
  @endpoint = guid
  get
end

#dispatch_statuses(from, to, warehouse = nil) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/seko/client.rb', line 157

def dispatch_statuses(from, to, warehouse = nil)
  @service     = 'dispatches'
  @endpoint    = "status/#{format_from_to(from, to)}"
  request_url  = "#{request_uri}&status=Dispatched"
  request_url += "&dc=#{warehouse}" unless warehouse.nil?
  get(request_url)
end

#get_inventory(warehouse = nil) ⇒ Object

FIXME: use this method once SS fixes their API for now we are manually sorting the distribution centre see #inventory_response below

def get_inventory(warehouse = nil)

@service  = 'stock'
@endpoint = if warehouse.nil?
  "all"
else 
  "dc/#{warehouse}"
end
inventory_response

end

def inventory_response

response = get
response.parsed = map_results(Stock.parse(response))
response

end



68
69
70
71
72
# File 'lib/seko/client.rb', line 68

def get_inventory(warehouse = nil)
  @service  = 'stock'
  @endpoint = 'all'
  inventory_response(warehouse)
end

#inventory_response(warehouse = nil) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/seko/client.rb', line 75

def inventory_response(warehouse = nil)
  response = get
  response.parsed = map_results(Stock.parse(response)).select do |stock|
    warehouse.nil? ? true : stock["DCCode"] == warehouse
  end
  response
end

#map_keys(key) ⇒ Object



99
100
101
# File 'lib/seko/client.rb', line 99

def map_keys(key)
  KEYS_MAP[key] || key
end

#map_results(results) ⇒ Object



95
96
97
# File 'lib/seko/client.rb', line 95

def map_results(results)
  results.map { |h| h.inject({ }) { |x, (k,v)| x[map_keys(k)] = v; x } }
end

#mapped_inventory(upcs, inventory) ⇒ Object



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

def mapped_inventory(upcs, inventory)
  inventory.collect do |stock| 
    if upcs.include?(stock["upc"])
      { quantity: stock["qty"].to_i }
    end
  end.compact
end

#order_request(order_hash) ⇒ Object



34
35
36
# File 'lib/seko/client.rb', line 34

def order_request(order_hash)
  Order.websubmit(order_hash).to_json
end

#order_status(guid) ⇒ Object



127
128
129
130
131
# File 'lib/seko/client.rb', line 127

def order_status(guid)
  @service  = 'salesorders'
  @endpoint = "#{guid}/status"
  get
end

#order_tracking(guid) ⇒ Object



133
134
135
136
137
# File 'lib/seko/client.rb', line 133

def order_tracking(guid)
  @service  = 'salesorders'
  @endpoint = "#{guid}/tracking"
  get
end

#send_order_request(order_hash) ⇒ Object



28
29
30
31
32
# File 'lib/seko/client.rb', line 28

def send_order_request(order_hash)
  @service  = 'salesorders'
  @endpoint = 'websubmit'
  post(Order.websubmit(order_hash))
end

#send_wholesale_request(order_hash) ⇒ Object



38
39
40
41
42
# File 'lib/seko/client.rb', line 38

def send_wholesale_request(order_hash)
  @service  = 'salesorders'
  @endpoint = 'submit'
  post(Order.submit(order_hash))
end

#stock_adjustments(from, to, warehouse) ⇒ Object



145
146
147
148
149
# File 'lib/seko/client.rb', line 145

def stock_adjustments(from, to, warehouse)
  @service  = 'stock'
  @endpoint = "adjustment/#{format_from_to(from, to)}"
  get("#{request_uri}&dc=#{warehouse}")
end

#stock_movements(from, to, warehouse) ⇒ Object



151
152
153
154
155
# File 'lib/seko/client.rb', line 151

def stock_movements(from, to, warehouse)
  @service  = 'stock'
  @endpoint = "movement/#{format_from_to(from, to)}"
  get("#{request_uri}&dc=#{warehouse}")
end

#submit_company(company_hash) ⇒ Object



115
116
117
118
119
# File 'lib/seko/client.rb', line 115

def submit_company(company_hash)
  @service  = 'companies'
  @endpoint = 'submit'
  post(Company.format(company_hash))
end

#submit_product(product_hash) ⇒ Object



103
104
105
106
107
# File 'lib/seko/client.rb', line 103

def submit_product(product_hash)
  @service  = 'products'
  @endpoint = 'submit'
  post(Product.format(product_hash))
end

#submit_receipt(line_item_array, warehouse) ⇒ Object



109
110
111
112
113
# File 'lib/seko/client.rb', line 109

def submit_receipt(line_item_array, warehouse)
  @service  = 'receipts'
  @endpoint = 'submit'
  post(Receipt.format(line_item_array, warehouse))
end

#upcs(inventory) ⇒ Object



83
84
85
# File 'lib/seko/client.rb', line 83

def upcs(inventory)
  inventory.collect { |s| s["upc"] }
end

#wholesale_request(order_hash) ⇒ Object



44
45
46
# File 'lib/seko/client.rb', line 44

def wholesale_request(order_hash)
  Order.submit(order_hash).to_json
end