Module: EasyPost
- Defined in:
- lib/easypost.rb,
lib/easypost/version.rb
Defined Under Namespace
Modules: Util Classes: Address, ApiKey, Batch, Brand, CarrierAccount, CarrierType, CustomsInfo, CustomsItem, EasyPostObject, Error, Event, Insurance, Order, Parcel, Pickup, PickupRate, PostageLabel, Rate, Refund, Report, Resource, ScanForm, Shipment, TaxIdentifier, Tracker, User, Webhook
Constant Summary collapse
- DEFAULT_API_BASE =
'https://api.easypost.com'
- DEFAULT_USER_AGENT =
"EasyPost/v2 RubyClient/#{EasyPost::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
- VERSION =
File.open(File.('../../VERSION', __dir__)).read.strip
Class Attribute Summary collapse
-
.api_base ⇒ Object
Returns the value of attribute api_base.
-
.api_key ⇒ Object
Returns the value of attribute api_key.
- .default_connection ⇒ Object
Class Method Summary collapse
- .authorization(key) ⇒ Object
- .default_headers ⇒ Object
- .default_http_config ⇒ Object
-
.http_config ⇒ Object
Get the HTTP config.
-
.http_config=(http_config_params) ⇒ Object
Set the HTTP config.
- .make_client(url) ⇒ Object deprecated Deprecated.
-
.make_request(method, path, api_key = nil, body = nil) ⇒ Hash
Make an HTTP request against the EasyPost.default_connection.
- .parse_response(status:, body:, json:) ⇒ Object
-
.reset_http_config ⇒ Object
Reset the HTTP config.
Class Attribute Details
.api_base ⇒ Object
Returns the value of attribute api_base.
45 46 47 |
# File 'lib/easypost.rb', line 45 def api_base @api_base end |
.api_key ⇒ Object
Returns the value of attribute api_key.
45 46 47 |
# File 'lib/easypost.rb', line 45 def api_key @api_key end |
.default_connection ⇒ Object
58 59 60 61 62 63 |
# File 'lib/easypost.rb', line 58 def self.default_connection @default_connection ||= EasyPost::Connection.new( uri: URI(api_base), config: http_config, ) end |
Class Method Details
.authorization(key) ⇒ Object
65 66 67 |
# File 'lib/easypost.rb', line 65 def self.(key) "Basic #{Base64.strict_encode64("#{key}:")}" end |
.default_headers ⇒ Object
51 52 53 54 55 56 |
# File 'lib/easypost.rb', line 51 def self.default_headers @default_headers ||= { 'Content-Type' => 'application/json', 'User-Agent' => EasyPost::DEFAULT_USER_AGENT, } end |
.default_http_config ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/easypost.rb', line 75 def self.default_http_config http_config = { timeout: 60, open_timeout: 30, verify_ssl: OpenSSL::SSL::VERIFY_PEER, } ruby_version = Gem::Version.new(RUBY_VERSION) if ruby_version >= Gem::Version.new('2.5.0') http_config[:min_version] = OpenSSL::SSL::TLS1_2_VERSION else http_config[:ssl_version] = :TLSv1_2 # rubocop:disable Naming/VariableNumber end http_config end |
.http_config ⇒ Object
Get the HTTP config.
93 94 95 |
# File 'lib/easypost.rb', line 93 def self.http_config @http_config ||= default_http_config end |
.http_config=(http_config_params) ⇒ Object
Set the HTTP config.
98 99 100 101 102 |
# File 'lib/easypost.rb', line 98 def self.http_config=(http_config_params) http_config.merge!(http_config_params) self.default_connection = nil end |
.make_client(url) ⇒ Object
Create an EasyPost Client.
107 108 109 |
# File 'lib/easypost.rb', line 107 def self.make_client(url) EasyPost::Connection.new(uri: URI(url), config: http_config).create end |
.make_request(method, path, api_key = nil, body = nil) ⇒ Hash
Make an HTTP request against the default_connection
119 120 121 |
# File 'lib/easypost.rb', line 119 def self.make_request(method, path, api_key = nil, body = nil) default_connection.call(method, path, api_key || EasyPost.api_key, body) end |
.parse_response(status:, body:, json:) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/easypost.rb', line 123 def self.parse_response(status:, body:, json:) if status >= 400 error = JSON.parse(body)['error'] raise EasyPost::Error.new( error['message'], status, error['code'], error['errors'], body, ) end json ? JSON.parse(body) : body rescue JSON::ParserError raise "Invalid response object from API, unable to decode.\n#{body}" end |
.reset_http_config ⇒ Object
Reset the HTTP config.
70 71 72 73 |
# File 'lib/easypost.rb', line 70 def self.reset_http_config http_config.clear self.default_connection = nil end |