class ParcelApi::Label

This module provides API requests to label parcels, get existing label details and download labels.

Constants

LABEL_URL

Attributes

connection[RW]
label_request[RW]
label_response[RW]
labels[RW]

Public Class Methods

new(connection = nil) click to toggle source

Creates a new ParcelApi::Label instance.

# File lib/parcel_api/label.rb, line 13
def initialize(connection = nil)
  self.connection ||= connection || ParcelApi::Client.connection
end

Public Instance Methods

create(label_options) click to toggle source

Create a label with the specified options @param label_options [Hash] @return Single or array of label objects

# File lib/parcel_api/label.rb, line 21
def create(label_options)
  create_label File.join(LABEL_URL, 'domestic'), label_options
end
details(label_id) click to toggle source

Get label details @param label_id [String] @return Object of label details

# File lib/parcel_api/label.rb, line 37
def details(label_id)
  details_url = File.join(LABEL_URL, "#{label_id}.json")
  response = connection.get details_url
  details = response.parsed.tap {|d| d.delete('success')}
  OpenStruct.new(details)
end
download(label_id) click to toggle source

Download label @param label_id [String] @return Object of label

# File lib/parcel_api/label.rb, line 48
def download(label_id)
  download_url = File.join(LABEL_URL, "#{label_id}.pdf")
  response = connection.get download_url
  StringIO.new(response.body)
end
international_create(label_options) click to toggle source

Create an international label with the specified options @param label_options [Hash] @return Single or array of label objects

# File lib/parcel_api/label.rb, line 29
def international_create(label_options)
  create_label File.join(LABEL_URL, 'international', label_options)
end

Private Instance Methods

create_label(url, label_options) click to toggle source
# File lib/parcel_api/label.rb, line 56
def create_label(url, label_options)
  self.label_request = {
    body: label_options.to_json.to_ascii,
    headers: { 'Content-Type' => 'application/json' }
  }
  self.label_response = connection.post(url, label_request)
  self.label_response.parsed['labels'].map {|label| OpenStruct.new(label)}.first
end