class Uber::Client

Constants

ENDPOINT
SANDBOX_ENDPOINT

Attributes

bearer_token[RW]
client_id[RW]
client_secret[RW]
connection_options[W]
debug[RW]
middleware[W]
sandbox[RW]
server_token[RW]

Public Class Methods

new(options = {}) { |self| ... } click to toggle source
# File lib/uber/client.rb, line 21
def initialize(options = {})
  options.each do |key, value|
    send(:"#{key}=", value)
  end
  yield(self) if block_given?
  validate_credential_type!
end

Public Instance Methods

bearer_token=(token) click to toggle source
# File lib/uber/client.rb, line 29
def bearer_token=(token)
  @bearer_token = Token.new(access_token: token, token_type: Token::BEARER_TYPE)
end
bearer_token?() click to toggle source

@return [Boolean]

# File lib/uber/client.rb, line 102
def bearer_token?
  !!bearer_token
end
connection_options() click to toggle source
# File lib/uber/client.rb, line 33
def connection_options
  @connection_options ||= {
    :builder => middleware,
    :headers => {
      :accept => 'application/json',
      :user_agent => user_agent,
    },
    :request => {
      :open_timeout => 10,
      :timeout => 30,
    },
  }
end
credentials() click to toggle source

@return [Hash]

# File lib/uber/client.rb, line 107
def credentials
  {
    server_token:  server_token,
    client_id:     client_id,
    client_secret: client_secret
  }
end
credentials?() click to toggle source

@return [Boolean]

# File lib/uber/client.rb, line 116
def credentials?
  credentials.values.all?
end
delete(path, params = {}) click to toggle source

Perform an HTTP DELETE request

# File lib/uber/client.rb, line 90
def delete(path, params = {})
  headers = request_headers(:delete, path, params)
  request(:delete, path, params, headers)
end
deliveries() click to toggle source
# File lib/uber/client.rb, line 124
def deliveries
  @deliveries ||= Uber::Delivery::Client.new self
end
get(path, params = {}) click to toggle source

Perform an HTTP GET request

# File lib/uber/client.rb, line 72
def get(path, params = {})
  headers = request_headers(:get, path, params)
  request(:get, path, params, headers)
end
middleware() click to toggle source
# File lib/uber/client.rb, line 57
def middleware
  @middleware ||= Faraday::RackBuilder.new do |faraday|
    # Encodes as "application/x-www-form-urlencoded" if not already encoded
    faraday.request :url_encoded
    # Parse JSON response bodies
    faraday.response :parse_json
    faraday.response :logger if self.debug
    # Use instrumentation if available
    faraday.use :instrumentation if defined?(FaradayMiddleware::Instrumentation)
    # Set default HTTP adapter
    faraday.adapter Faraday.default_adapter
  end
end
partners() click to toggle source
# File lib/uber/client.rb, line 120
def partners
  @partners ||= Uber::Partner::Client.new self
end
patch(path, params={}) click to toggle source

Perform an HTTP PATCH request

# File lib/uber/client.rb, line 96
def patch(path, params={})
  headers = params.values.any? { |value| value.respond_to?(:to_io) } ? request_headers(:post, path, params, {}) : request_headers(:patch, path, params)
  request(:patch, path, params.to_json, headers)
end
post(path, params = {}) click to toggle source

Perform an HTTP POST request

# File lib/uber/client.rb, line 78
def post(path, params = {})
  headers = params.values.any? { |value| value.respond_to?(:to_io) } ? request_headers(:post, path, params, {}) : request_headers(:post, path, params)
  request(:post, path, params.to_json, headers)
end
put(path, params = {}) click to toggle source

Perform an HTTP PUT request

# File lib/uber/client.rb, line 84
def put(path, params = {})
  headers = params.values.any? { |value| value.respond_to?(:to_io) } ? request_headers(:post, path, params, {}) : request_headers(:put, path, params)
  request(:put, path, params.to_json, headers)
end
user_agent() click to toggle source

@return [String]

# File lib/uber/client.rb, line 53
def user_agent
  @user_agent ||= "Uber Ruby Gem #{Uber::Version}"
end
user_token?() click to toggle source

@return [Boolean]

# File lib/uber/client.rb, line 48
def user_token?
  !!(client_id && client_secret)
end

Private Instance Methods

bearer_auth_header() click to toggle source
# File lib/uber/client.rb, line 168
def bearer_auth_header
  token = bearer_token.is_a?(Uber::Token) && bearer_token.bearer? ? bearer_token.access_token : bearer_token
  "Bearer #{token}"
end
connection() click to toggle source

Returns a Faraday::Connection object

@return [Faraday::Connection]

# File lib/uber/client.rb, line 144
def connection
  @connection ||= Faraday.new(self.sandbox ? SANDBOX_ENDPOINT : ENDPOINT, connection_options)
end
request(method, path, params = {}, headers = {}) click to toggle source
# File lib/uber/client.rb, line 148
def request(method, path, params = {}, headers = {})
  connection.send(method.to_sym, path, params) { |request| request.headers.update(headers) }.env
rescue Faraday::Error::TimeoutError, Timeout::Error => error
  raise(Uber::Error::RequestTimeout.new(error))
rescue Faraday::Error::ClientError, JSON::ParserError => error
  fail(Uber::Error.new(error))
end
request_headers(method, path, params = {}, signature_params = params) click to toggle source
# File lib/uber/client.rb, line 156
def request_headers(method, path, params = {}, signature_params = params)
  headers = {}
  headers[:accept]        = '*/*'
  headers[:content_type]  = 'application/json; charset=UTF-8'
  if bearer_token?
    headers[:authorization] = bearer_auth_header
  else
    headers[:authorization] = server_auth_header
  end
  headers
end
server_auth_header() click to toggle source
# File lib/uber/client.rb, line 173
def server_auth_header
  "Token #{@server_token}"
end
validate_credential_type!() click to toggle source

Ensures that all credentials set during configuration are of a valid type. Valid types are String and Symbol.

@raise [Uber::Error::ConfigurationError] Error is raised when

supplied uber credentials are not a String or Symbol.
# File lib/uber/client.rb, line 134
def validate_credential_type!
  credentials.each do |credential, value|
    next if value.nil?
    fail(Uber::Error::ConfigurationError.new("Invalid #{credential} specified: #{value.inspect} must be a string or symbol.")) unless value.is_a?(String) || value.is_a?(Symbol)
  end
end