Class: CanadaPost

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

Instance Method Summary collapse

Constructor Details

#initializeCanadaPost

:nodoc:



9
10
11
# File 'lib/canada_post.rb', line 9

def initialize # :nodoc:
  @items = []
end

Instance Method Details

#addItem(item) ⇒ Object

This method allows you to add items to be shipped



30
31
32
# File 'lib/canada_post.rb', line 30

def addItem(item)
  @items << item;
end

#getRatesObject

The main method. Makes request and parse the response



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/canada_post.rb', line 37

def getRates
  url = URI.parse 'http://sellonline.canadapost.ca:30000/'
  request = Net::HTTP::Post.new(url.path)
  request.content_type = 'application/x-www-form-urlencoded'
  request.body = prepareXML
  response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
  xml = REXML::Document.new(response.body).root
  if error = xml.elements['error']
    error = error.elements['statusMessage'].text
    raise StandardError.new(error)
  end
  xml = xml.elements['ratesAndServicesResponse']
  result = {}
  result[:products] = []
  xml.elements.each('product') do |p|
    product = {}
    p.elements.each do |t|
      product[t.name.to_sym] = t.text
    end
    result[:products] << product
  end
  result[:options] = {}
  xml.elements['shippingOptions'].elements.each do |t|
    result[:options][t.name.to_sym] = t.text
  end
  return result
end

#setCustomer(customer) ⇒ Object

This method sets the customer information



23
24
25
# File 'lib/canada_post.rb', line 23

def setCustomer(customer)
  @customerInfo = customer;
end

#setMerchant(merchant) ⇒ Object

This method sets the merchant information



16
17
18
# File 'lib/canada_post.rb', line 16

def setMerchant(merchant)
  @merchantInfo = merchant;
end