Class: ShippingScale::Response
- Inherits:
-
Object
- Object
- ShippingScale::Response
- Defined in:
- lib/shipping_scale/response.rb
Instance Attribute Summary collapse
-
#raw ⇒ Object
Returns the value of attribute raw.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Class Method Summary collapse
Instance Method Summary collapse
- #details ⇒ Object
-
#initialize(xml) ⇒ Response
constructor
A new instance of Response.
- #price ⇒ Object
- #prices ⇒ Object
Constructor Details
#initialize(xml) ⇒ Response
Returns a new instance of Response.
13 14 15 |
# File 'lib/shipping_scale/response.rb', line 13 def initialize(xml) @xml = xml end |
Instance Attribute Details
#raw ⇒ Object
Returns the value of attribute raw.
3 4 5 |
# File 'lib/shipping_scale/response.rb', line 3 def raw @raw end |
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
17 18 19 |
# File 'lib/shipping_scale/response.rb', line 17 def xml @xml end |
Class Method Details
.parse(xml) ⇒ Object
6 7 8 9 10 |
# File 'lib/shipping_scale/response.rb', line 6 def parse(xml) response = self.new(xml) response.raw = xml response end |
Instance Method Details
#details ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/shipping_scale/response.rb', line 19 def details details = {postage: []} xml.search("Package").children.each do |node| if node.name == "Postage" postage = {} node.children.each do |child| postage[child.name.to_s.snakecase.to_sym] = child.text end details[:postage].push(postage) else details[node.name.to_s.snakecase.to_sym] = node.text end end return details end |
#price ⇒ Object
37 38 39 |
# File 'lib/shipping_scale/response.rb', line 37 def price xml.search("Postage[CLASSID='1']").search("Rate").inject(0) { |sum, t| sum + t.text.to_f } end |
#prices ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/shipping_scale/response.rb', line 41 def prices prices = {} xml.search("Package").each do |package| key = package.attribute("ID").value.to_s value = package.search("Postage[CLASSID='1']").search("Rate").text.to_f prices[key] = value end prices end |