Module: EasyPost
- Defined in:
- lib/easypost.rb,
lib/easypost/item.rb,
lib/easypost/rate.rb,
lib/easypost/user.rb,
lib/easypost/util.rb,
lib/easypost/batch.rb,
lib/easypost/error.rb,
lib/easypost/event.rb,
lib/easypost/order.rb,
lib/easypost/object.rb,
lib/easypost/parcel.rb,
lib/easypost/pickup.rb,
lib/easypost/refund.rb,
lib/easypost/report.rb,
lib/easypost/address.rb,
lib/easypost/printer.rb,
lib/easypost/tracker.rb,
lib/easypost/version.rb,
lib/easypost/webhook.rb,
lib/easypost/resource.rb,
lib/easypost/shipment.rb,
lib/easypost/container.rb,
lib/easypost/insurance.rb,
lib/easypost/print_job.rb,
lib/easypost/scan_form.rb,
lib/easypost/pickup_rate.rb,
lib/easypost/customs_info.rb,
lib/easypost/customs_item.rb,
lib/easypost/postage_label.rb,
lib/easypost/carrier_account.rb
Defined Under Namespace
Modules: Util Classes: Address, Batch, CarrierAccount, Container, CustomsInfo, CustomsItem, EasyPostObject, Error, Event, Insurance, Item, Order, Parcel, Pickup, PickupRate, PostageLabel, PrintJob, Printer, Rate, Refund, Report, Resource, ScanForm, Shipment, Tracker, User, Webhook
Constant Summary collapse
- VERSION =
File.open(File.("../../../VERSION", __FILE__)).read().strip
- @@api_key =
nil
- @@api_base =
'https://api.easypost.com/v2'
- @@api_version =
nil
- @@open_timeout =
30
- @@timeout =
60
Class Method Summary collapse
- .api_base ⇒ Object
- .api_base=(api_base) ⇒ Object
- .api_key ⇒ Object
- .api_key=(api_key) ⇒ Object
- .api_url(url = '') ⇒ Object
- .api_version ⇒ Object
- .api_version=(version) ⇒ Object
- .http_config ⇒ Object
- .http_config=(http_config_params) ⇒ Object
- .request(method, url, api_key, params = {}, headers = {}, api_key_required = true) ⇒ Object
Class Method Details
.api_base ⇒ Object
62 63 64 |
# File 'lib/easypost.rb', line 62 def self.api_base @@api_base end |
.api_base=(api_base) ⇒ Object
58 59 60 |
# File 'lib/easypost.rb', line 58 def self.api_base=(api_base) @@api_base = api_base end |
.api_key ⇒ Object
54 55 56 |
# File 'lib/easypost.rb', line 54 def self.api_key @@api_key end |
.api_key=(api_key) ⇒ Object
50 51 52 |
# File 'lib/easypost.rb', line 50 def self.api_key=(api_key) @@api_key = api_key end |
.api_url(url = '') ⇒ Object
46 47 48 |
# File 'lib/easypost.rb', line 46 def self.api_url(url='') @@api_base + url end |
.api_version ⇒ Object
70 71 72 |
# File 'lib/easypost.rb', line 70 def self.api_version @@api_version end |
.api_version=(version) ⇒ Object
66 67 68 |
# File 'lib/easypost.rb', line 66 def self.api_version=(version) @@api_version = version end |
.http_config ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/easypost.rb', line 74 def self.http_config @@http_config ||= { timeout: 60, open_timeout: 30, verify_ssl: false, ssl_version: "TLSv1", } end |
.http_config=(http_config_params) ⇒ Object
83 84 85 |
# File 'lib/easypost.rb', line 83 def self.http_config=(http_config_params) self.http_config.merge!(http_config_params) end |
.request(method, url, api_key, params = {}, headers = {}, api_key_required = true) ⇒ Object
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 118 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 |
# File 'lib/easypost.rb', line 87 def self.request(method, url, api_key, params={}, headers={}, api_key_required=true) api_key ||= @@api_key if api_key_required raise Error.new('No API key provided.') unless api_key end params = Util.objects_to_ids(params) url = self.api_url(url) case method.to_s.downcase.to_sym when :get, :head, :delete # Make params into GET parameters if params && params.count > 0 query_string = Util.flatten_params(params).collect{|key, value| "#{key}=#{Util.url_encode(value)}"}.join('&') url += "#{URI.parse(url).query ? '&' : '?'}#{query_string}" end payload = nil else payload = Util.flatten_params(params).collect{|(key, value)| "#{key}=#{Util.url_encode(value)}"}.join('&') end headers = { :user_agent => "EasyPost/v2 RubyClient/#{VERSION}", :authorization => "Bearer #{api_key}", :content_type => 'application/x-www-form-urlencoded' }.merge(headers) opts = http_config.merge( { :method => method, :url => url, :headers => headers, :payload => payload } ) begin response = execute_request(opts) rescue RestClient::ExceptionWithResponse => e if response_code = e.http_code and response_body = e.http_body begin response_json = MultiJson.load(response_body, :symbolize_keys => true) rescue MultiJson::DecodeError raise Error.new("Invalid response from API, unable to decode.", response_code, response_body) end begin raise NoMethodError if response_json[:error][:message] == nil raise Error.new(response_json[:error][:message], response_code, response_body, response_json) rescue NoMethodError, TypeError raise Error.new(response_json[:error], response_code, response_body, response_json) end else raise Error.new(e.) end rescue RestClient::Exception, Errno::ECONNREFUSED => e raise Error.new(e.) end begin response_json = MultiJson.load(response.body, :symbolize_keys => true) rescue MultiJson::DecodeError raise Error.new("Invalid response object from API, unable to decode.", response.code, response.body) end return [response_json, api_key] end |