Class: ShippingScale::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/shipping_scale/response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#rawObject

Returns the value of attribute raw.



3
4
5
# File 'lib/shipping_scale/response.rb', line 3

def raw
  @raw
end

#xmlObject (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

#detailsObject



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

#priceObject



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

#pricesObject



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