class Postmen::Carrier

Constants

PRODUCTION
SANDBOX

Attributes

api_key[R]
api_url[R]
async[R]
is_document[R]
mode[R]
shipper_account_ids[R]

Public Class Methods

new(options) click to toggle source
# File lib/postmen/carrier.rb, line 17
def initialize(options)
  @api_key = options[:api_key]
  @mode = options[:mode]
  @api_url = get_api_url(options[:mode])
  @async = options[:async]
  @is_document = options[:is_document]
  @shipper_account_ids = options[:shipper_account_ids]
end

Public Instance Methods

calculate_rates(shipment) click to toggle source
# File lib/postmen/carrier.rb, line 26
def calculate_rates(shipment)
  request_body = {
      async: self.async,
      is_document: self.is_document
  }
  shipper_accounts = []
  self.shipper_account_ids.each do |account|
    shipper_accounts << {id: account}
  end
  request_body.merge!(shipper_accounts: shipper_accounts)
  request_body.merge!(shipment: shipment.to_hash)
  process_request("#{api_url}/rates", 'POST', request_body)
end
cancel_label() click to toggle source
# File lib/postmen/carrier.rb, line 68
def cancel_label
  raise NotImplementedError, "Method: cancel_label is not supported by #{self.class.name}."
end
create_label(base, invoice, messages, billing, shipment, service_options = nil, customs = nil) click to toggle source
# File lib/postmen/carrier.rb, line 44
def create_label(base, invoice, messages, billing, shipment, service_options = nil, customs = nil)
  request_body = {
      async: self.async,
      is_document: self.is_document
  }
  request_body.merge!(base.to_hash)
  request_body.merge!(invoice: invoice.to_hash)
  request_body.merge!(references: messages) #Reference messages for shipment.
  request_body.merge!(shipper_account: {id: self.shipper_account_ids.first})
  request_body.merge!(billing: billing.to_hash)
  request_body.merge!(shipment: shipment.to_hash)
  request_body.merge!(service_options: service_options.collect{|options| options.to_hash}) unless service_options.empty?
  request_body.merge!(customs: customs.to_hash) if customs
  process_request("#{api_url}/labels", 'POST', request_body)
end
list_all_cancel_labels() click to toggle source
# File lib/postmen/carrier.rb, line 72
def list_all_cancel_labels
  raise NotImplementedError, "Method: list_all_cancel_labels is not supported by #{self.class.name}."
end
list_all_label() click to toggle source
# File lib/postmen/carrier.rb, line 64
def list_all_label
  raise NotImplementedError, "Method: list_all_label is not supported by #{self.class.name}."
end
retrieve_cancel_label_by_id() click to toggle source
# File lib/postmen/carrier.rb, line 76
def retrieve_cancel_label_by_id
  raise NotImplementedError, "Method: retrieve_cancel_label_by_id is not supported by #{self.class.name}."
end
retrieve_label_by_id() click to toggle source
# File lib/postmen/carrier.rb, line 60
def retrieve_label_by_id
  raise NotImplementedError, "Method: retrieve_label_by_id is not supported by #{self.class.name}."
end
retrieve_rates_by_id() click to toggle source
# File lib/postmen/carrier.rb, line 40
def retrieve_rates_by_id
  raise NotImplementedError, "Method: retrieve_rates_by_id is not supported by #{self.class.name}."
end

Private Instance Methods

get_api_url(mode) click to toggle source
# File lib/postmen/carrier.rb, line 81
def get_api_url(mode)
  case mode
    when 'production'
      PRODUCTION
    when 'sandbox'
      SANDBOX
    else
      ''
  end
end
process_request(url, method, body = nil) click to toggle source
# File lib/postmen/carrier.rb, line 93
def process_request(url, method, body = nil)
  url = URI(url)
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  request = case method
              when 'GET'
                Net::HTTP::Get.new(url)
              when 'POST'
                Net::HTTP::Post.new(url)
            end
  request['postmen-api-key'] = api_key
  request['content-type'] = 'application/json'
  request.body = body.to_json if body
  response = http.request(request)
  JSON.parse(response.body)
end