Sha256: a2fd31eb5c5ec0e5c788827fd98cfe7ba9212fd148c3551be5e29feb79d3bd68

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

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

Money.rounding_mode = BigDecimal::ROUND_HALF_UP

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

1 entries across 1 versions & 1 rubygems

Version Path
mws-orders-0.6.1 lib/mws/orders/entity.rb