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
@@api_key =
nil
@@api_base =
'https://api.easypost.com/v2'
@@api_version =
nil
@@open_timeout =
30
@@timeout =
60

Class Method Summary collapse

Class Method Details

.api_baseObject



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

def self.api_base
  @@api_base
end

.api_base=(api_base) ⇒ Object



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

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

.api_keyObject



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

def self.api_key
  @@api_key
end

.api_key=(api_key) ⇒ Object



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

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

.api_url(url = '') ⇒ Object



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

def self.api_url(url='')
  @@api_base + url
end

.api_versionObject



69
70
71
# File 'lib/easypost.rb', line 69

def self.api_version
  @@api_version
end

.api_version=(version) ⇒ Object



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

def self.api_version=(version)
  @@api_version = version
end

.http_configObject



73
74
75
76
77
78
79
80
# File 'lib/easypost.rb', line 73

def self.http_config
  @@http_config ||= {
    timeout: 60,
    open_timeout: 30,
    verify_ssl: OpenSSL::SSL::VERIFY_PEER,
    ssl_version: :TLSv1_2,
  }
end

.http_config=(http_config_params) ⇒ Object



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

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

.request(method, url, api_key, params = {}, headers = {}, api_key_required = true) ⇒ Object



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
115
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
147
148
149
150
# File 'lib/easypost.rb', line 86

def self.request(method, url, api_key, params={}, headers={}, api_key_required=true)
  api_key ||= @@api_key
  if api_key_required
    raise Error.new('No API key provided.') unless api_key
  end

  params = Util.objects_to_ids(params)
  url = self.api_url(url)
  case method.to_s.downcase.to_sym
  when :get, :head, :delete
    # Make params into GET parameters
    if params && params.count > 0
      query_string = Util.flatten_params(params).collect{|key, value| "#{key}=#{Util.url_encode(value)}"}.join('&')
      url += "#{URI.parse(url).query ? '&' : '?'}#{query_string}"
    end
    payload = nil
  else
    payload = Util.flatten_params(params).collect{|(key, value)| "#{key}=#{Util.url_encode(value)}"}.join('&')
  end

  headers = {
    :user_agent => "EasyPost/v2 RubyClient/#{VERSION}",
    :authorization => "Bearer #{api_key}",
    :content_type => 'application/x-www-form-urlencoded'
  }.merge(headers)

  opts = http_config.merge(
    {
      :method => method,
      :url => url,
      :headers => headers,
      :payload => payload
    }
  )

  begin
    response = execute_request(opts)
  rescue RestClient::ExceptionWithResponse => e
    if response_code = e.http_code and response_body = e.http_body
      begin
        response_json = MultiJson.load(response_body, :symbolize_keys => true)
      rescue MultiJson::DecodeError
        raise Error.new("Invalid response from API, unable to decode.", response_code, response_body)
      end
      begin
        raise NoMethodError if response_json[:error][:message] == nil
        raise Error.new(response_json[:error][:message], response_code, response_body, response_json)
      rescue NoMethodError, TypeError
        raise Error.new(response_json[:error], response_code, response_body, response_json)
      end
    else
      raise Error.new(e.message)
    end
  rescue RestClient::Exception, Errno::ECONNREFUSED => e
    raise Error.new(e.message)
  end

  begin
    response_json = MultiJson.load(response.body, :symbolize_keys => true)
  rescue MultiJson::DecodeError
    raise Error.new("Invalid response object from API, unable to decode.", response.code, response.body)
  end

  return [response_json, api_key]
end