class CanadaPost::Request::Manifest

Attributes

destination[RW]
group_id[RW]
phone[RW]

Public Class Methods

new(credentials, options={}) click to toggle source
Calls superclass method CanadaPost::Request::Base::new
# File lib/canada_post/request/manifest.rb, line 7
def initialize(credentials, options={})
  @credentials = credentials
  if options.present?
    @phone = options[:phone]
    @destination = options[:destination]
    @group_id = options[:group_id]
  end
  super(credentials)
end

Public Instance Methods

get_artifact(url) click to toggle source
# File lib/canada_post/request/manifest.rb, line 36
def get_artifact(url)
  self.class.get(
    url,
    headers: artifact_header,
    basic_auth: @authorization
  )
end
get_manifest(url) click to toggle source
# File lib/canada_post/request/manifest.rb, line 27
def get_manifest(url)
  api_response = self.class.get(
    url,
    headers: manifest_header,
    basic_auth: @authorization
  )
  process_response(api_response)
end
process_request() click to toggle source
# File lib/canada_post/request/manifest.rb, line 17
def process_request
  api_response = self.class.post(
    api_url,
    body: build_xml,
    headers: manifest_header,
    basic_auth: @authorization
  )
  process_response(api_response)
end

Private Instance Methods

add_manifest_details(xml) click to toggle source
# File lib/canada_post/request/manifest.rb, line 83
def add_manifest_details(xml)
  xml.send(:'manifest-company', @destination[:company])
  xml.send(:'manifest-name', @destination[:name])
  xml.send(:'phone-number', @phone)
  xml.send(:'address-details') {
    manifest_address(xml, @destination[:address_details])
  }
end
api_url() click to toggle source
# File lib/canada_post/request/manifest.rb, line 46
def api_url
  api_url = @credentials.mode == "production" ? PRODUCTION_URL : TEST_URL
  api_url += "/rs/#{@credentials.customer_number}/#{@credentials.customer_number}/manifest"
end
artifact_header() click to toggle source
# File lib/canada_post/request/manifest.rb, line 58
def artifact_header
  {
    'Content-type' => 'application/pdf',
    'Accept' => 'application/pdf'
  }
end
build_xml() click to toggle source
# File lib/canada_post/request/manifest.rb, line 65
def build_xml
  ns = "http://www.canadapost.ca/ws/manifest-v7"
  xsi = 'http://www.w3.org/2001/XMLSchema-instance'
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.send(:"transmit-set", :'xmlns:xsi' => xsi, xmlns: ns) {
      xml.send(:'group-ids') {
        xml.send(:'group-id', @group_id)
      }
      xml.send(:'detailed-manifests', true)
      xml.send(:'method-of-payment', 'Account')
      xml.send(:'manifest-address') {
        add_manifest_details(xml)
      }
    }
  end
  builder.doc.root.to_xml
end
manifest_address(xml, params) click to toggle source
# File lib/canada_post/request/manifest.rb, line 92
def manifest_address(xml, params)
  xml.send(:'address-line-1', params[:address])
  xml.send(:'city', params[:city])
  xml.send(:'prov-state', params[:state])
  if params[:postal_code].present?
    xml.send(:'postal-zip-code', params[:postal_code].gsub(' ', ''))
  end
end
manifest_header() click to toggle source
# File lib/canada_post/request/manifest.rb, line 51
def manifest_header
  {
    'Content-type' => 'application/vnd.cpc.manifest-v7+xml',
    'Accept' => 'application/vnd.cpc.manifest-v7+xml'
  }
end