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/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, 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.expand_path("../../../VERSION", __FILE__)).read().strip

Class Method Summary collapse

Class Method Details

.api_baseObject



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

def self.api_base
  @api_base
end

.api_base=(api_base) ⇒ Object



48
49
50
# File 'lib/easypost.rb', line 48

def self.api_base=(api_base)
  @api_base = api_base
end

.api_keyObject



44
45
46
# File 'lib/easypost.rb', line 44

def self.api_key
  @api_key
end

.api_key=(api_key) ⇒ Object



40
41
42
# File 'lib/easypost.rb', line 40

def self.api_key=(api_key)
  @api_key = api_key
end

.http_configObject



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

def self.http_config
  @http_config ||= reset_http_config
end

.http_config=(http_config_params) ⇒ Object



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

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

.make_client(uri) ⇒ Object



81
82
83
84
85
86
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
# File 'lib/easypost.rb', line 81

def self.make_client(uri)
  client = if http_config[:proxy]
             proxy_uri = URI(http_config[:proxy])
             Net::HTTP.new(
               uri.host,
               uri.port,
               proxy_uri.host,
               proxy_uri.port,
               proxy_uri.user,
               proxy_uri.password
             )
           else
             Net::HTTP.new(uri.host, uri.port)
           end
  client.use_ssl = true

  http_config.each do |name, value|
    # Discrepancies between RestClient and Net::HTTP.
    if name == :verify_ssl
      name = :verify_mode
    elsif name == :timeout
      name = :read_timeout
    end

    # Handled in the creation of the client.
    if name == :proxy
      next
    end

    client.send("#{name}=", value)
  end

  client
end

.make_request(method, path, api_key = nil, body = nil) ⇒ Object



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
# File 'lib/easypost.rb', line 116

def self.make_request(method, path, api_key=nil, body=nil)
  client = make_client(URI(@api_base))

  request = Net::HTTP.const_get(method.capitalize).new(path)
  if body
    request.body = JSON.dump(Util.objects_to_ids(body))
  end

  request["Content-Type"] = "application/json"
  request["User-Agent"] = "EasyPost/v2 RubyClient/#{VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
  if api_key = api_key || @api_key
    request["Authorization"] = "Basic #{Base64.strict_encode64("#{api_key}:")}"
  end

  response = client.request(request)

  if (400..599).include? response.code.to_i
    error = JSON.parse(response.body)["error"]
    raise Error.new(error["message"], response.code.to_i, error["code"], error["errors"], response.body)
  end

  if response["Content-Type"].include? "application/json"
    JSON.parse(response.body)
  else
    response.body
  end
rescue JSON::ParserError
  raise RuntimeError.new(
    "Invalid response object from API, unable to decode.\n#{response.body}"
  )
end

.reset_http_configObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/easypost.rb', line 56

def self.reset_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
  end

  @http_config
end