Class: Uber::Client

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/uber/client.rb

Constant Summary collapse

ENDPOINT =
"https://api.uber.com"
SANDBOX_ENDPOINT =
"https://sandbox-api.uber.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API::Requests

#trip_cancel, #trip_current, #trip_details, #trip_estimate, #trip_map, #trip_request, #trip_update

Methods included from API::Promotions

#promotion

Methods included from API::Me

#me

Methods included from API::Activities

#history

Methods included from API::TimeEstimates

#time_estimations

Methods included from API::PriceEstimates

#price_estimations

Methods included from API::Products

#apply_availability, #apply_surge, #products

Methods included from Utils

#perform_with_object, #perform_with_objects

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:

  • _self (Uber::Client)

    the object that the method was called on



21
22
23
24
25
26
27
# 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

Instance Attribute Details

#bearer_tokenObject

Returns the value of attribute bearer_token.



14
15
16
# File 'lib/uber/client.rb', line 14

def bearer_token
  @bearer_token
end

#client_idObject

Returns the value of attribute client_id.



13
14
15
# File 'lib/uber/client.rb', line 13

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



13
14
15
# File 'lib/uber/client.rb', line 13

def client_secret
  @client_secret
end

#connection_optionsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

#middlewareObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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

#sandboxObject

Returns the value of attribute sandbox.



15
16
17
# File 'lib/uber/client.rb', line 15

def sandbox
  @sandbox
end

#server_tokenObject

Returns the value of attribute server_token.



13
14
15
# File 'lib/uber/client.rb', line 13

def server_token
  @server_token
end

Instance Method Details

#bearer_token?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/uber/client.rb', line 112

def bearer_token?
  !!bearer_token
end

#credentialsHash

Returns:

  • (Hash)


117
118
119
120
121
122
123
# File 'lib/uber/client.rb', line 117

def credentials
  {
    server_token:  server_token,
    client_id:     client_id,
    client_secret: client_secret
  }
end

#credentials?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/uber/client.rb', line 126

def credentials?
  credentials.values.all?
end

#delete(path, params = {}) ⇒ Object

Perform an HTTP DELETE request



106
107
108
109
# 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 = {}) ⇒ Object

Perform an HTTP GET request



76
77
78
79
# File 'lib/uber/client.rb', line 76

def get(path, params = {})
  headers = request_headers(:get, path, params)
  request(:get, path, params, headers)
end

#post(path, params = {}) ⇒ Object

Perform an HTTP POST request



82
83
84
85
86
87
88
89
90
91
# 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 = {}) ⇒ Object

Perform an HTTP PUT request



94
95
96
97
98
99
100
101
102
103
# 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_agentString

Returns:

  • (String)


56
57
58
# File 'lib/uber/client.rb', line 56

def user_agent
  @user_agent ||= "Uber Ruby Gem #{Uber::Version}"
end

#user_token?Boolean

Returns:

  • (Boolean)


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

def user_token?
  !!(client_id && client_secret)
end