class ShippingScale::Response

Attributes

raw[RW]
xml[R]

Public Class Methods

new(xml) click to toggle source
# File lib/shipping_scale/response.rb, line 13
def initialize(xml)
  @xml = xml
end
parse(xml) click to toggle source
# File lib/shipping_scale/response.rb, line 6
def parse(xml)
  response = self.new(xml)
  response.raw = xml
  response
end

Public Instance Methods

details() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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