Class: Canadapost

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

Overview

CanadaPost gem for use with Canada Post REST API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Canadapost

Returns a new instance of Canadapost.

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :username (String)

    your API key username

  • :password (String)

    your API key password

  • :development (Boolean)

    to specify development mode

  • :customer_number (String)

    your Canada Post customer account number



22
23
24
25
26
27
28
# File 'lib/canadapost.rb', line 22

def initialize(params = {})
	@username = params[:username]
 	@password = params[:password]
 	@development = params[:development]
 	@customer_number = params[:customer_number]
 	@auth = { username: username, password: password }
end

Instance Attribute Details

#accept_headerObject

Returns the value of attribute accept_header.



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

def accept_header
  @accept_header
end

#authObject

Returns the value of attribute auth.



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

def auth
  @auth
end

#content_type_headerObject

Returns the value of attribute content_type_header.



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

def content_type_header
  @content_type_header
end

#customerObject

Returns the value of attribute customer.



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

def customer
  @customer
end

#customer_numberObject

Returns the value of attribute customer_number.



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

def customer_number
  @customer_number
end

#developmentObject

Returns the value of attribute development.



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

def development
  @development
end

#dimensionsObject

Returns the value of attribute dimensions.



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

def dimensions
  @dimensions
end

#origin_postal_codeObject

Returns the value of attribute origin_postal_code.



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

def origin_postal_code
  @origin_postal_code
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#get_rates(params = {}) ⇒ Object

Returns Hash a hash of services.

Parameters:

  • options (Hash)

    a customizable set of options

Returns:

  • Hash a hash of services



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/canadapost.rb', line 77

def get_rates(params = {})
		self.origin_postal_code = params[:origin_postal_code].gsub(/\s+/, "") if params[:origin_postal_code].present?
 	self.customer = params[:customer] if params[:customer].present?
 	self.dimensions = params[:dimensions] if params[:dimensions].present?

 	self.content_type_header = self.accept_header = 'application/vnd.cpc.ship.rate-v2+xml'

	begin
		do_request(get_rates_xml, get_rates_url)
		services = results[:price_quotes][:price_quote]
		services = services.map(&:deep_symbolize_keys!)
		return services
	rescue
		return nil
	end
	return services
end