Module: PBShipping

Defined in:
lib/pbshipping/rate.rb,
lib/pbshipping.rb,
lib/pbshipping/error.rb,
lib/pbshipping/parcel.rb,
lib/pbshipping/account.rb,
lib/pbshipping/address.rb,
lib/pbshipping/carrier.rb,
lib/pbshipping/country.rb,
lib/pbshipping/customs.rb,
lib/pbshipping/version.rb,
lib/pbshipping/manifest.rb,
lib/pbshipping/merchant.rb,
lib/pbshipping/shipment.rb,
lib/pbshipping/tracking.rb,
lib/pbshipping/developer.rb,
lib/pbshipping/api_object.rb,
lib/pbshipping/scandetails.rb,
lib/pbshipping/api_resource.rb,
lib/pbshipping/authentication.rb,
lib/pbshipping/transactiondetails.rb,
lib/pbshipping/shipping_api_resource.rb

Overview

Copyright 2016 Pitney Bowes Inc.

Licensed under the MIT License (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License in the LICENSE file or at

https://opensource.org/licenses/MIT

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and limitations under the License.

File: shipping_api_resource.rb Description: shipping api resource base class

Defined Under Namespace

Classes: Account, Address, ApiError, ApiObject, ApiResource, AuthenticationError, AuthenticationToken, Carrier, Country, Customs, CustomsInfo, CustomsItem, DeliveryCommitment, Developer, Discount, Document, Manifest, Merchant, MissingResourceAttribute, Parameter, Parcel, ParcelDimension, ParcelWeight, Rate, ScanDetails, Shipment, ShipmentLabel, ShipmentOptions, ShippingApiResource, SpecialService, Surcharge, Tax, Tracking, TransactionDetails

Constant Summary collapse

VERSION =
"1.0.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_group_shippingObject

Returns the value of attribute api_group_shipping.



61
62
63
# File 'lib/pbshipping.rb', line 61

def api_group_shipping
  @api_group_shipping
end

.configurationObject

Returns the value of attribute configuration.



61
62
63
# File 'lib/pbshipping.rb', line 61

def configuration
  @configuration
end

.txid_attrnameObject

Returns the value of attribute txid_attrname.



61
62
63
# File 'lib/pbshipping.rb', line 61

def txid_attrname
  @txid_attrname
end

Class Method Details

.api_request(auth_obj, method, api_version, api_path, headers = {}, params = {}, payload = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/pbshipping.rb', line 119

def self.api_request(auth_obj, method, api_version, api_path, headers = {}, params = {}, payload = {})
  if auth_obj == nil || auth_obj.access_token == nil
    raise AuthenticationError.new("Invalid or missing authentication credentials")
  end
  begin
    url = api_url(api_version, api_path)
    headers.merge!(
      :accept => :json,
      :content_type => :json,
      :Authorization => "Bearer " + auth_obj.access_token
    )
    case method
    when :get    
      payload = {}        
      pairs = []
      params.each { |k, v|
        pairs.push "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
      }
      url += "?#{pairs.join('&')}" unless pairs.empty?
    end
    opts = { 
      :headers => headers,
      :method => method,
      :payload => payload.to_json,
      :url => url,
      :open_timeout => 15,
      :timeout => 30,
      :user_agent => "pbshipping/v1 RubyBindings"
    }
    res = make_http_request(opts)
    json_parse(res)      
  rescue => e
    case e
    when RestClient::Exception
      raise ApiError.new(e.to_s, e.http_code, e.http_body) 
    else
      raise ApiError.new(e.to_s)
    end 
  end
end

.api_url(api_version, api_path) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/pbshipping.rb', line 73

def self.api_url(api_version, api_path)
  if @configuration[:is_production] == true
    api_server = @configuration[:production]
  else
    api_server = @configuration[:sandbox] 
  end
  api_server + @api_group_shipping + api_version + api_path
end

.authenticate_request(api_key, api_secret) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/pbshipping.rb', line 82

def self.authenticate_request(api_key, api_secret)
  if @configuration[:is_production] == true
    api_server = @configuration[:production]
  else
    api_server = @configuration[:sandbox] 
  end
  url = api_server + "/oauth/token"
 
  headers = {
    :content_type => 'application/x-www-form-urlencoded',
    :Authorization => 'Basic ' + Base64.strict_encode64(api_key + ":" + api_secret)
  }
  payload = {
    :grant_type => 'client_credentials'
  }
  opts = {
    :method => 'post',
    :payload => payload,
    :headers => headers,
    :url => url,
    :open_timeout => 15,
    :timeout => 30
  }
  
  begin
    res = make_http_request(opts)
    json_parse(res)
  rescue => e
    case e
    when RestClient::Exception
      raise AuthenticationError.new(e.to_s, e.http_code, e.http_body) 
    else
      raise AuthenticationError.new(e.to_s)
    end           
  end  
end

.get_api_version(api_sig) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/pbshipping.rb', line 64

def self.get_api_version(api_sig)
  if @configuration[:override_api_version].key?(api_sig) == true
    api_version = @configuration[:override_api_version][api_sig]
  else 
    api_version = @configuration[:default_api_version]
  end
  return api_version  
end

.json_parse(response) ⇒ Object



160
161
162
# File 'lib/pbshipping.rb', line 160

def self.json_parse(response)
  JSON::parse(response.body, { :symbolize_names => true })
end