class Seko::Client

Constants

API_PATH
API_VERSION
CONTENT_TYPE
KEYS_MAP
LIVE_HOST

api.supplystream.com

PORT
TEST_HOST

Attributes

endpoint[RW]
options[RW]
path[RW]
request_uri[RW]
response[RW]
service[RW]
token[RW]
type[RW]

Public Class Methods

new(token, options = {}) click to toggle source
# 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

Public Instance Methods

cancel_order(guid, reason_code) click to toggle source
# File lib/seko/client.rb, line 135
def cancel_order(guid, reason_code)
  @service  = 'salesorders'
  @endpoint = "#{guid}/cancel/reasoncode/#{reason_code}"
  post({'Cancel' => 'Order'})
end
check_grn(guid) click to toggle source
# File lib/seko/client.rb, line 117
def check_grn(guid)
  @service  = 'grns'
  @endpoint = guid
  get
end
dispatch_statuses(from, to, warehouse = nil) click to toggle source
# File lib/seko/client.rb, line 153
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) click to toggle source

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 = warehouse.nil? ? "all" : "dc/#{warehouse}"
inventory_response

end

def inventory_response

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

end

# File lib/seko/client.rb, line 64
def get_inventory(warehouse = nil)
  @service  = 'stock'
  @endpoint = 'all'
  inventory_response(warehouse)
end
inventory_response(warehouse = nil) click to toggle source
# File lib/seko/client.rb, line 71
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) click to toggle source
# File lib/seko/client.rb, line 95
def map_keys(key)
  KEYS_MAP[key] || key
end
map_results(results) click to toggle source
# File lib/seko/client.rb, line 91
def map_results(results)
  results.map { |h| h.inject({ }) { |x, (k,v)| x[map_keys(k)] = v; x } }
end
mapped_inventory(upcs, inventory) click to toggle source
# File lib/seko/client.rb, line 83
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) click to toggle source
# File lib/seko/client.rb, line 34
def order_request(order_hash)
  Order.websubmit(order_hash).to_json
end
order_status(guid) click to toggle source
# File lib/seko/client.rb, line 123
def order_status(guid)
  @service  = 'salesorders'
  @endpoint = "#{guid}/status"
  get
end
order_tracking(guid) click to toggle source
# File lib/seko/client.rb, line 129
def order_tracking(guid)
  @service  = 'salesorders'
  @endpoint = "#{guid}/tracking"
  get
end
requires_warehouse?() click to toggle source
# File lib/seko/client.rb, line 169
def requires_warehouse?
  true
end
send_order_request(order_hash) click to toggle source
# File lib/seko/client.rb, line 28
def send_order_request(order_hash)
  @service  = 'salesorders'
  @endpoint = 'websubmit'
  post(Order.websubmit(order_hash))
end
send_return_request(line_item_array, warehouse) click to toggle source
# File lib/seko/client.rb, line 105
def send_return_request(line_item_array, warehouse)
  @service  = 'receipts'
  @endpoint = 'submit'
  post(Receipt.format(line_item_array, warehouse))
end
send_wholesale_request(order_hash) click to toggle source
# 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) click to toggle source
# File lib/seko/client.rb, line 141
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) click to toggle source
# File lib/seko/client.rb, line 147
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) click to toggle source
# File lib/seko/client.rb, line 111
def submit_company(company_hash)
  @service  = 'companies'
  @endpoint = 'submit'
  post(Company.format(company_hash))
end
submit_product(product_hash) click to toggle source
# File lib/seko/client.rb, line 99
def submit_product(product_hash)
  @service  = 'products'
  @endpoint = 'submit'
  post(Product.format(product_hash))
end
upcs(inventory) click to toggle source
# File lib/seko/client.rb, line 79
def upcs(inventory)
  inventory.collect { |s| s["upc"] }
end
wholesale_request(order_hash) click to toggle source
# File lib/seko/client.rb, line 44
def wholesale_request(order_hash)
  Order.submit(order_hash).to_json
end

Private Instance Methods

build_request(type, url = request_uri) click to toggle source
# File lib/seko/client.rb, line 202
def build_request(type, url = request_uri)
  request = Net::HTTP.const_get(type).new(url)
  request.content_type = CONTENT_TYPE
  request
end
default_options() click to toggle source
# File lib/seko/client.rb, line 174
def default_options
  { 
    verbose: false,
    test_mode: true
  }
end
format_from_to(from, to) click to toggle source
# File lib/seko/client.rb, line 231
def format_from_to(from, to)
  "#{from.strftime('%F')}T00:00:00Z/#{to.strftime('%F')}T00:00:00Z"
end
get(url = request_uri) click to toggle source
# File lib/seko/client.rb, line 215
def get(url = request_uri)
  request = build_request('Get', url)
  request(request)
end
host() click to toggle source
# File lib/seko/client.rb, line 189
def host
  testing? ? TEST_HOST : LIVE_HOST 
end
http() click to toggle source
# File lib/seko/client.rb, line 198
def http
  @http ||= Net::HTTP.new(host, PORT)
end
log(message) click to toggle source
# File lib/seko/client.rb, line 193
def log(message)
  return unless verbose?
  puts message
end
parse_response(json_response) click to toggle source
# File lib/seko/client.rb, line 226
def parse_response(json_response)
  log(json_response)
  @response = Response.new(json_response)
end
post(json_request, url = request_uri) click to toggle source
# File lib/seko/client.rb, line 220
def post(json_request, url = request_uri)
  request      = build_request('Post', url)
  request.body = json_request.to_json
  request(request)
end
request(request, json_request = nil) click to toggle source
# File lib/seko/client.rb, line 208
def request(request, json_request = nil)
  http.use_ssl     = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  response = http.request(request)
  parse_response(response.body)
end
testing?() click to toggle source
# File lib/seko/client.rb, line 181
def testing?
  @options[:test_mode]
end
verbose?() click to toggle source
# File lib/seko/client.rb, line 185
def verbose?
  @options[:verbose]
end