Module: EasyPost

Defined in:
lib/easypost.rb,
lib/easypost/version.rb

Defined Under Namespace

Modules: Beta, 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.expand_path('../../VERSION', __dir__)).read.strip

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

Returns the value of attribute api_base.


46
47
48
# File 'lib/easypost.rb', line 46

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key.


46
47
48
# File 'lib/easypost.rb', line 46

def api_key
  @api_key
end

.default_connectionObject


59
60
61
62
63
64
# File 'lib/easypost.rb', line 59

def self.default_connection
  @default_connection ||= EasyPost::Connection.new(
    uri: URI(api_base),
    config: http_config,
  )
end

Class Method Details

.authorization(key) ⇒ Object


66
67
68
# File 'lib/easypost.rb', line 66

def self.authorization(key)
  "Basic #{Base64.strict_encode64("#{key}:")}"
end

.default_headersObject


52
53
54
55
56
57
# File 'lib/easypost.rb', line 52

def self.default_headers
  @default_headers ||= {
    'Content-Type' => 'application/json',
    'User-Agent' => EasyPost::DEFAULT_USER_AGENT,
  }
end

.default_http_configObject


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/easypost.rb', line 76

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_configObject

Get the HTTP config.


94
95
96
# File 'lib/easypost.rb', line 94

def self.http_config
  @http_config ||= default_http_config
end

.http_config=(http_config_params) ⇒ Object

Set the HTTP config.


99
100
101
102
103
# File 'lib/easypost.rb', line 99

def self.http_config=(http_config_params)
  http_config.merge!(http_config_params)

  self.default_connection = nil
end

.make_client(url) ⇒ Object

Deprecated.

Create an EasyPost Client.


108
109
110
# File 'lib/easypost.rb', line 108

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

Parameters:

  • method (Symbol)

    the HTTP Verb (get, method, put, post, etc.)

  • path (String)

    URI path of the resource

  • requested_api_key (String)

    (api_key) key set Authorization header.

  • body (Object) (defaults to: nil)

    (nil) object to be dumped to JSON

Returns:

  • (Hash)

    JSON object parsed from the response body

Raises:


120
121
122
# File 'lib/easypost.rb', line 120

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


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/easypost.rb', line 124

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 || !body.nil? && !body.match(/\A\s+\z/) ? JSON.parse(body) : body
rescue JSON::ParserError
  raise "Invalid response object from API, unable to decode.\n#{body}"
end

.reset_http_configObject

Reset the HTTP config.


71
72
73
74
# File 'lib/easypost.rb', line 71

def self.reset_http_config
  http_config.clear
  self.default_connection = nil
end