Class: Ubr::API

Inherits:
Object
  • Object
show all
Defined in:
lib/ubr/api.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAPI

Returns a new instance of API.



7
8
9
10
11
12
13
14
15
# File 'lib/ubr/api.rb', line 7

def initialize
  token = File.read(API.token_path).strip
  @client = RestClient::Resource.new('https://api.uber.com/v1',
    headers: {
      authorization: "Bearer #{token}",
      content_type: 'application/json'
    }
  )
end

Class Method Details

.authorize(client_id:, code:, secret:, redirect_uri:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/ubr/api.rb', line 28

def self.authorize(client_id:, code:, secret:, redirect_uri:)
  client = RestClient::Resource.new('https://login.uber.com/oauth/token')
  parse_response client.post(
    client_secret: secret,
    client_id: client_id,
    grant_type: 'authorization_code',
    redirect_uri: redirect_uri,
    code: code,
  )
end

.parse_response(response) ⇒ Object



25
26
27
# File 'lib/ubr/api.rb', line 25

def self.parse_response(response)
  JSON.parse response, symbolize_names: true
end

.token_pathObject



38
39
40
# File 'lib/ubr/api.rb', line 38

def self.token_path
  File.expand_path('~/.ubr')
end

Instance Method Details

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



16
17
18
# File 'lib/ubr/api.rb', line 16

def get(path, params={})
  parse_response @client[path].get(params: params)
end

#parse_response(response) ⇒ Object



22
23
24
# File 'lib/ubr/api.rb', line 22

def parse_response(response)
  API.parse_response(response)
end

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



19
20
21
# File 'lib/ubr/api.rb', line 19

def post(path, params={})
  parse_response @client[path].post(params.to_json)
end