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/address.rb,
lib/easypost/tracker.rb,
lib/easypost/version.rb,
lib/easypost/resource.rb,
lib/easypost/shipment.rb,
lib/easypost/container.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, Container, CustomsInfo, CustomsItem, EasyPostObject, Error, Event, Item, Order, Parcel, Pickup, PickupRate, PostageLabel, Rate, Refund, Resource, ScanForm, Shipment, Tracker, User

Constant Summary collapse

VERSION =
File.open(File.expand_path("../../../VERSION", __FILE__)).read()
@@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



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

def self.api_base
  @@api_base
end

.api_base=(api_base) ⇒ Object



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

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

.api_keyObject



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

def self.api_key
  @@api_key
end

.api_key=(api_key) ⇒ Object



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

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

.api_url(url = '') ⇒ Object



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

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

.api_versionObject



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

def self.api_version
  @@api_version
end

.api_version=(version) ⇒ Object



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

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

.http_configObject



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

def self.http_config
  @@http_config ||= {
    timeout: 60,
    open_timeout: 30,
    verify_ssl: false,
    ssl_version: "TLSv1",
  }
end

.http_config=(http_config_params) ⇒ Object



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

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

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

Raises:



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

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

  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