Class: MWS::Orders::Entity

Inherits:
Document show all
Includes:
Structure
Defined in:
lib/mws/orders/entity.rb

Overview

A parsed object

Instance Attribute Summary

Attributes inherited from Document

#node

Instance Method Summary collapse

Methods inherited from Document

#at_xpath, #initialize, #xpath

Constructor Details

This class inherits a constructor from MWS::Orders::Document

Instance Method Details

#boolean(path) ⇒ Object



17
18
19
# File 'lib/mws/orders/entity.rb', line 17

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

#entities(path, klass) ⇒ Object



21
22
23
# File 'lib/mws/orders/entity.rb', line 21

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

#entity(path, klass) ⇒ Object



25
26
27
28
# File 'lib/mws/orders/entity.rb', line 25

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

#float(path) ⇒ Object



30
31
32
# File 'lib/mws/orders/entity.rb', line 30

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

#integer(path) ⇒ Object



34
35
36
# File 'lib/mws/orders/entity.rb', line 34

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

#money(path) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/mws/orders/entity.rb', line 38

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

#string(path) ⇒ Object



48
49
50
# File 'lib/mws/orders/entity.rb', line 48

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

#time(path) ⇒ Object



52
53
54
55
# File 'lib/mws/orders/entity.rb', line 52

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