class Uber::Client

Constants

ENDPOINT
SANDBOX_ENDPOINT

Attributes

bearer_token[RW]
client_id[RW]
client_secret[RW]
connection_options[W]
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 112
def bearer_token?
  !!bearer_token
end
connection_options() click to toggle source
# File lib/uber/client.rb, line 36
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 117
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 126
def credentials?
  credentials.values.all?
end
delete(path, params = {}) click to toggle source

Perform an HTTP DELETE request

# File lib/uber/client.rb, line 106
def delete(path, params = {})
  headers = request_headers(:delete, path, params)
  request(:delete, path, params, headers)
end
get(path, params = {}) click to toggle source

Perform an HTTP GET request

# File lib/uber/client.rb, line 76
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 60
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
    # Use instrumentation if available
    if defined?(FaradayMiddleware::Instrumentation)
      faraday.use :instrumentation
    end
    # Set default HTTP adapter
    faraday.adapter Faraday.default_adapter
  end
end
post(path, params = {}) click to toggle source

Perform an HTTP POST request

# File lib/uber/client.rb, line 82
def post(path, params = {})
  respond = params.values.any? { |value| value.respond_to?(:to_io) }
  response = if respond
               request_headers(:post, path, params, {})
             else
               request_headers(:post, path, params)
             end
  headers = response
  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 94
def put(path, params = {})
  respond = params.values.any? { |value| value.respond_to?(:to_io) }
  response = if respond
               request_headers(:post, path, params, {})
             else
               request_headers(:put, path, params)
             end
  headers = response
  request(:put, path, params.to_json, headers)
end
user_agent() click to toggle source

@return [String]

# File lib/uber/client.rb, line 56
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 51
def user_token?
  !!(client_id && client_secret)
end

Private Instance Methods

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

Returns a Faraday::Connection object

@return [Faraday::Connection]

# File lib/uber/client.rb, line 152
def connection
  @connection ||= Faraday.new(
    sandbox ? SANDBOX_ENDPOINT : ENDPOINT,
    connection_options
  )
end
request(method, path, params = {}, headers = {}) click to toggle source
# File lib/uber/client.rb, line 159
def request(method, path, params = {}, headers = {})
  connection.send(method.to_sym, path, params) do |request|
    request.headers.update(headers)
  end.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 169
def request_headers(_method, _path, params = {}, _signature_params = params)
  headers = {}
  headers[:accept]        = "*/*"
  headers[:content_type]  = "application/json; charset=UTF-8"
  headers[:authorization] = if bearer_token?
                              bearer_auth_header
                            else
                              server_auth_header
                            end
  headers
end
server_auth_header() click to toggle source
# File lib/uber/client.rb, line 190
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 137
def validate_credential_type!
  credentials.each do |credential, value|
    next if value.nil?

    unless value.is_a?(String) || value.is_a?(Symbol) # rubocop:disable Style/Next, Metrics/LineLength
      msg = "Invalid #{credential} specified: #{value.inspect}
      must be a string or symbol."
      fail(Uber::Error::ConfigurationError.new(msg))
    end
  end
end