Class: ParcelApi::Label

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

Overview

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

Constant Summary collapse

LABEL_URL =
'/ParcelLabel/2.0/labels'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection = nil) ⇒ Label

Creates a new ParcelApi::Label instance.



13
14
15
# File 'lib/parcel_api/label.rb', line 13

def initialize(connection = nil)
  self.connection ||= connection || ParcelApi::Client.connection
end

Instance Attribute Details

#connection

Returns the value of attribute connection.



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

def connection
  @connection
end

#label_request

Returns the value of attribute label_request.



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

def label_request
  @label_request
end

#label_response

Returns the value of attribute label_response.



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

def label_response
  @label_response
end

#labels

Returns the value of attribute labels.



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

def labels
  @labels
end

Instance Method Details

#create(label_options)

Create a label with the specified options

Parameters:

  • label_options (Hash)

Returns:

  • Single or array of label objects



21
22
23
# 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)

Get label details

Parameters:

  • label_id (String)

Returns:

  • Object of label details



37
38
39
40
41
42
# 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)

Download label

Parameters:

  • label_id (String)

Returns:

  • Object of label



48
49
50
51
52
# 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)

Create an international label with the specified options

Parameters:

  • label_options (Hash)

Returns:

  • Single or array of label objects



29
30
31
# File 'lib/parcel_api/label.rb', line 29

def international_create(label_options)
  create_label File.join(LABEL_URL, 'international', label_options)
end