lib/carousel/client.rb in carousel-ruby-api-0.0.1 vs lib/carousel/client.rb in carousel-ruby-api-0.0.2

- old
+ new

@@ -7,10 +7,11 @@ TEST_HOST = "web.carousel.eu" TEST_PATH = "carouselwms" LIVE_HOST = "web.carousel.eu" LIVE_PATH = "carouselwms" PORT = 443 + KEYS_MAP = { "stockid" => "upc" } attr_accessor :username, :password, :response, :type, :request_uri, :path def initialize(username, password, options = {}) raise "Username is required" unless username @@ -21,18 +22,17 @@ @options = default_options.merge!(options) end def send_order_request(order) request = order_request(order) - @path = build_path(Order::PATH) post(request) end def get_inventory request = Inventory.new(self).build_inventory_request @path = build_path(Inventory::PATH) - post(request).response['stock'] + map_results(post(request).response['stock']) end def order_request(order) @path = build_path(Order::PATH) Order.new(self).build_order_request(order) @@ -41,17 +41,17 @@ def build_path(path) "/#{env_path}/default.asp#{path}" end def upcs(inventory) - inventory.collect { |s| s["stockid"][0] } + inventory.collect { |s| s["upc"] } end def mapped_inventory(upcs, inventory) inventory.collect do |stock| - if upcs.include?(stock["stockid"][0]) - { quantity: stock["qty"][0].to_i } + if upcs.include?(stock["upc"]) + { quantity: stock["qty"].to_i } end end.compact end def request_uri @@ -106,9 +106,26 @@ end def parse_response(xml_response) log(xml_response) @response = Response.new(xml_response, @type) + end + + 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 + + def flatten_results(results) + @flattened ||= results.map do |h| + h.each { |k,v| h[k] = v[0] } + end + end + + def map_keys(key) + KEYS_MAP[key] || key end end end