class Stellae::Client

Constants

KEYS_MAP
LIVE_HOST
LIVE_PATH
PORT
TEST_HOST
TEST_PATH

Attributes

password[RW]
request_uri[RW]
response[RW]
type[RW]
username[RW]

Public Class Methods

new(username, password, options = {}) click to toggle source
# File lib/stellae/client.rb, line 16
def initialize(username, password, options = {})
  raise "Username is required" unless username
  raise "Password is required" unless password

  @username = username
  @password = password
  @options  = default_options.merge!(options)
end

Public Instance Methods

get_inventory() click to toggle source
# File lib/stellae/client.rb, line 34
def get_inventory
  request  = Inventory.new(self).build_inventory_request
  response = post(request)
  result   = response.result['Inventory_values'][0]['UPC_Inventory_Response']
  return nil if result.blank?
  map_results(result)
end
mapped_inventory(upcs, inventory) click to toggle source
# File lib/stellae/client.rb, line 46
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) click to toggle source
# File lib/stellae/client.rb, line 30
def order_request(order)
  Order.new(self).build_order_request(order)
end
send_order_request(order) click to toggle source
# File lib/stellae/client.rb, line 25
def send_order_request(order)
  request  = Order.new(self).build_order_request(order)
  response = post(request)
end
upcs(inventory) click to toggle source
# File lib/stellae/client.rb, line 42
def upcs(inventory)
  inventory.collect { |s| s["upc"] }
end

Private Instance Methods

default_options() click to toggle source
# File lib/stellae/client.rb, line 59
def default_options
  { 
    verbose: false,
    test_mode: false
  }
end
flatten_results(results) click to toggle source
# File lib/stellae/client.rb, line 117
def flatten_results(results)
  @flattened ||= results.map do |h| 
    h.each { |k,v| h[k] = v[0] }
  end
end
host() click to toggle source
# File lib/stellae/client.rb, line 74
def host
  testing? ? TEST_HOST : LIVE_HOST 
end
http() click to toggle source
# File lib/stellae/client.rb, line 87
def http
  @http ||= Net::HTTP.new(host, PORT)
end
log(message) click to toggle source
# File lib/stellae/client.rb, line 82
def log(message)
  return unless verbose?
  puts message
end
map_keys(key) click to toggle source
# File lib/stellae/client.rb, line 123
def map_keys(key)
  KEYS_MAP[key] || key
end
map_results(results) click to toggle source
# File lib/stellae/client.rb, line 110
def map_results(results)
  results = flatten_results(results)
  results.map do |h|
    h.inject({ }) { |x, (k,v)| x[map_keys(k)] = v; x }
  end
end
parse_response(xml_response) click to toggle source
# File lib/stellae/client.rb, line 105
def parse_response(xml_response)
  log xml_response
  @response = Response.new(xml_response, @type)
end
path() click to toggle source
# File lib/stellae/client.rb, line 78
def path
  testing? ? TEST_PATH : LIVE_PATH
end
post(xml_request) click to toggle source
# File lib/stellae/client.rb, line 100
def post(xml_request)
  response = request(xml_request)
  parse_response(response.body)
end
request(xml_request) click to toggle source
# File lib/stellae/client.rb, line 91
def request(xml_request)
  request              = Net::HTTP::Post.new(path)
  request.body         = xml_request
  request.content_type = 'application/soap+xml; charset=utf-8'
  http.use_ssl         = true
  http.verify_mode     = OpenSSL::SSL::VERIFY_NONE
  http.request(request)
end
testing?() click to toggle source
# File lib/stellae/client.rb, line 66
def testing?
  @options[:test_mode]
end
verbose?() click to toggle source
# File lib/stellae/client.rb, line 70
def verbose?
  @options[:verbose]
end