Module: EasyPost

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

Defined Under Namespace

Modules: Beta, Util Classes: Address, ApiKey, Batch, Billing, Brand, CarbonOffset, CarrierAccount, CarrierType, CustomsInfo, CustomsItem, EasyPostObject, EndShipper, Error, Event, Insurance, Order, Parcel, PaymentMethod, Pickup, PickupRate, PostageLabel, Rate, Referral, Refund, Report, Resource, ScanForm, Shipment, TaxIdentifier, Tracker, User, Webhook

Constant Summary collapse

DEFAULT_API_BASE =
'https://api.easypost.com'
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.


50
51
52
# File 'lib/easypost.rb', line 50

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key.


50
51
52
# File 'lib/easypost.rb', line 50

def api_key
  @api_key
end

.default_connectionObject


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

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

Class Method Details

.authorization(key) ⇒ Object


77
78
79
# File 'lib/easypost.rb', line 77

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

.default_headersObject


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

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

.default_http_configObject


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/easypost.rb', line 87

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.


105
106
107
# File 'lib/easypost.rb', line 105

def self.http_config
  @http_config ||= default_http_config
end

.http_config=(http_config_params) ⇒ Object

Set the HTTP config.


110
111
112
113
114
# File 'lib/easypost.rb', line 110

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.


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

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:


131
132
133
# File 'lib/easypost.rb', line 131

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


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/easypost.rb', line 135

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.


82
83
84
85
# File 'lib/easypost.rb', line 82

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

.user_agentObject


56
57
58
59
60
61
# File 'lib/easypost.rb', line 56

def self.user_agent
  @user_agent ||=
    "EasyPost/v2 RubyClient/#{EasyPost::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} " \
    "OS/#{EasyPost::Util.os_name} OSVersion/#{EasyPost::Util.os_version} " \
    "OSArch/#{EasyPost::Util.os_arch}"
end