Class: ParcelApi::Label
- Inherits:
-
Object
- Object
- ParcelApi::Label
- 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 Method Summary collapse
-
#create(label_options)
Create a label with the specified options.
-
#details(label_id)
Get label details.
-
#download(label_id)
Download label.
-
#initialize(connection = nil) ⇒ Label
constructor
Creates a new ParcelApi::Label instance.
-
#international_create(label_options)
Create an international label with the specified options.
Constructor Details
#initialize(connection = nil) ⇒ Label
Creates a new ParcelApi::Label instance.
11 12 13 |
# File 'lib/parcel_api/label.rb', line 11
def initialize(connection=nil)
@connection ||= connection || ParcelApi::Client.connection
end
|
Instance Method Details
#create(label_options)
Create a label with the specified options
19 20 21 22 23 24 |
# File 'lib/parcel_api/label.rb', line 19
def create(label_options)
domestic_url = File.join(LABEL_URL, 'domestic')
response = @connection.post domestic_url, body: label_options.to_json, headers: { 'Content-Type' => 'application/json' }
labels = response.parsed['labels'].map {|label| OpenStruct.new(label)}
labels.first if labels.count == 1
end
|
#details(label_id)
Get label details
41 42 43 44 45 46 |
# File 'lib/parcel_api/label.rb', line 41
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
52 53 54 55 56 |
# File 'lib/parcel_api/label.rb', line 52
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
30 31 32 33 34 35 |
# File 'lib/parcel_api/label.rb', line 30
def international_create(label_options)
international_url = File.join(LABEL_URL, 'international')
response = @connection.post international_url, body: label_options.to_json, headers: { 'Content-Type' => 'application/json' }
labels = response.parsed['labels'].map {|label| OpenStruct.new(label)}
labels.first if labels.count == 1
end
|