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, Payload, 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.



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

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

.default_connectionObject



72
73
74
75
76
77
# File 'lib/easypost.rb', line 72

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

Class Method Details

.authorization(key) ⇒ Object



79
80
81
# File 'lib/easypost.rb', line 79

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

.default_headersObject



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

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

.default_http_configObject



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

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.



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

def self.http_config
  @http_config ||= default_http_config
end

.http_config=(http_config_params) ⇒ Object

Set the HTTP config.



112
113
114
115
116
# File 'lib/easypost.rb', line 112

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.



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

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:



133
134
135
# File 'lib/easypost.rb', line 133

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



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

def self.parse_response(status:, body:, json:)
  if status < 200 || status >= 300
    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.



84
85
86
87
# File 'lib/easypost.rb', line 84

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

.user_agentObject



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

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