Sha256: f79aab1835414ba188364341208c263ab826a5d88a2b40ec22c3e0627dff632b

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require 'cgi'
require 'time'
require 'money'
require 'structure'
require 'mws/orders/document'

module MWS
  module Orders
    # A parsed object
    class Entity < Document
      include Structure

      def boolean(path)
        string(path) == 'true'
      end

      def entities(path, klass)
        xpath(path).map { |node| klass.new(node) }
      end

      def entity(path, klass)
        node = at_xpath(path)
        klass.new(node) if node
      end

      def float(path)
        string(path)&.to_f
      end

      def integer(path)
        string(path)&.to_i
      end

      def money(path)
        amount = float("#{path}/Amount")
        return unless amount

        currency_code = string("#{path}/CurrencyCode")
        amount *= 100 unless currency_code == 'JPY'

        Money.new(amount, currency_code)
      end

      def string(path)
        at_xpath(path)&.text&.strip
      end

      def time(path)
        text = string(path)
        Time.parse(CGI.unescape(text)) if text
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mws-orders-0.5.1 lib/mws/orders/entity.rb
mws-orders-0.5.0 lib/mws/orders/entity.rb