Sha256: fcec690bfa95a5e51219de6d6663067457f39dba0ab35217ad2fd8e0b3efae24

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

require "nokogiri"
require "mws/orders/orders"
require "mws/orders/order_items"
require "mws/orders/service_status"

module MWS
  module Orders
    class Parser
      SERVICE_STATUS = /GetServiceStatus/
      ORDER          = /GetOrder/
      ORDERS         = /ListOrders/
      ORDER_ITEMS    = /ListOrderItems/

      def self.parse(response, _)
        new(response).parse
      end

      def initialize(response)
        @response = response
      end

      def parse
        node = find_result_node

        case node.name
        when SERVICE_STATUS
          ServiceStatus.new(node)
        when ORDER
          Orders.new(node).first
        when ORDERS
          Orders.new(node)
        when ORDER_ITEMS
          OrderItems.new(node)
        else
          raise NotImplementedError
        end
      end

      private

      def find_result_node
        xml = Nokogiri(@response.body)
        root = xml.children.first

        root.children.find { |node| node.name.include?("Result") }
      end
    end

    # Override Peddler's default Parser.
    Client.parser = Parser
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mws-orders-0.1.1 lib/mws/orders/parser.rb
mws-orders-0.1.0 lib/mws/orders/parser.rb