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

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



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

def self.api_base
  @@api_base
end

.api_base=(api_base) ⇒ Object



55
56
57
# File 'lib/easypost.rb', line 55

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

.api_keyObject



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

def self.api_key
  @@api_key
end

.api_key=(api_key) ⇒ Object



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

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

.api_url(url = '') ⇒ Object



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

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

.api_versionObject



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

def self.api_version
  @@api_version
end

.api_version=(version) ⇒ Object



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

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

.http_configObject



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

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

.http_config=(http_config_params) ⇒ Object



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

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



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
145
146
147
148
# File 'lib/easypost.rb', line 84

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