rlyft 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.rubocop.yml +4 -0
- data/README.md +60 -34
- data/lib/lyft.rb +7 -10
- data/lib/lyft/client.rb +20 -16
- data/lib/lyft/client/api.rb +8 -0
- data/lib/lyft/client/api/availability.rb +55 -66
- data/lib/lyft/client/api/base.rb +26 -76
- data/lib/lyft/client/api/oauth.rb +15 -37
- data/lib/lyft/client/api/oauth/grant_type.rb +12 -0
- data/lib/lyft/client/api/oauth/scope.rb +18 -0
- data/lib/lyft/client/api/rides.rb +143 -140
- data/lib/lyft/client/api/user.rb +21 -28
- data/lib/lyft/client/configuration.rb +3 -2
- data/lib/lyft/response.rb +8 -0
- data/lib/lyft/version.rb +1 -1
- data/lyft.gemspec +4 -5
- metadata +10 -21
- data/lib/lyft/client/mashed_parser.rb +0 -31
data/lib/lyft/client/api/base.rb
CHANGED
@@ -2,92 +2,42 @@ module Lyft
|
|
2
2
|
class Client
|
3
3
|
module Api
|
4
4
|
class Base
|
5
|
-
|
6
|
-
base_uri 'https://api.lyft.com'
|
7
|
-
parser Lyft::Client::MashedParser
|
8
|
-
|
9
|
-
API_VERSION = ENV['LYFT_API_VERSION'] || 'v1'
|
10
|
-
|
11
|
-
ENDPOINTS = {}
|
12
|
-
DEFAULT_VALIDATES = [:access_token]
|
13
|
-
|
14
|
-
##
|
15
|
-
# Constructs path for endpoint.
|
16
|
-
#
|
17
|
-
# @example Get path for product stock changes.
|
18
|
-
# Products.path_for(:stock_changes) # '/products/stock_changes'
|
19
|
-
#
|
20
|
-
# @example Get path for product with sku.
|
21
|
-
# Products.path_for(:product, { sku: '123' })
|
22
|
-
# #=> Converts '/products/{{sku}}' to '/products/123'
|
23
|
-
#
|
24
|
-
# @param [Symbol] key The key for ENDPOINTS.
|
25
|
-
# @param [Hash] args A hash which maps values to variables.
|
26
|
-
#
|
27
|
-
# @return [String] The constructed path.
|
28
|
-
#
|
29
|
-
def self.path_for(key, args = {})
|
30
|
-
# Must call dup because gsub! works with the reference
|
31
|
-
# and will change the constant ENDPOINTS which is bad.
|
32
|
-
path = self::ENDPOINTS.fetch(key.to_sym).dup
|
33
|
-
args.each do |arg_key, value|
|
34
|
-
path.gsub!("{{#{arg_key}}}", value.to_s)
|
35
|
-
end
|
36
|
-
|
37
|
-
path
|
38
|
-
end
|
39
|
-
|
40
|
-
##
|
41
|
-
# Resolve path for ENDPOINTS[key]
|
42
|
-
#
|
43
|
-
# @example Get path for product stock changes.
|
44
|
-
# client.products.path_for(:stock_changes) # '/products/stock_changes'
|
45
|
-
#
|
46
|
-
# @example Get path for product with sku.
|
47
|
-
# client.products.path_for(:product, { sku: '123' })
|
48
|
-
# #=> Converts '/products/{{sku}}' to '/products/123'
|
49
|
-
#
|
50
|
-
# @return [String] The constructed path.
|
51
|
-
#
|
52
|
-
def path_for(key, args = {})
|
53
|
-
self.class.path_for(key, args)
|
54
|
-
end
|
5
|
+
attr_reader :configuration
|
55
6
|
|
56
7
|
def initialize(configuration)
|
57
8
|
@configuration = configuration
|
58
|
-
set_debug_output
|
59
9
|
end
|
60
10
|
|
61
|
-
def
|
62
|
-
|
63
|
-
|
11
|
+
def connection(access_token = nil)
|
12
|
+
Faraday.new(url: BASE_URL) do |conn|
|
13
|
+
# Headers
|
14
|
+
conn.headers = configuration.headers
|
64
15
|
|
65
|
-
|
16
|
+
if access_token
|
17
|
+
conn.authorization :Bearer, access_token
|
18
|
+
else
|
19
|
+
conn.basic_auth(
|
20
|
+
configuration.client_id,
|
21
|
+
configuration.client_secret
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Request
|
26
|
+
conn.request :json
|
66
27
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
# @param [Hash] args
|
71
|
-
# @option args [String, Symbol] :http_method The http request method.
|
72
|
-
# @option args [String] :endpoint The path to send request to.
|
73
|
-
# @option args [String] :access_token The access_token provided by lyft api.
|
74
|
-
# @option args [Hash] :options Options for HTTParty
|
75
|
-
#
|
76
|
-
def make_request(args = {})
|
77
|
-
http_method = args.fetch(:http_method)
|
78
|
-
endpoint = args.fetch(:endpoint, '')
|
79
|
-
access_token = args.fetch(:access_token, '')
|
80
|
-
options = args.fetch(:options, {})
|
28
|
+
# Response
|
29
|
+
conn.response :json
|
30
|
+
conn.response :logger
|
81
31
|
|
82
|
-
|
83
|
-
|
32
|
+
conn.adapter Faraday.default_adapter
|
33
|
+
end
|
84
34
|
end
|
85
35
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
36
|
+
protected
|
37
|
+
|
38
|
+
def handle_response(response)
|
39
|
+
# Wrap in our own response class
|
40
|
+
Lyft::Response.new response
|
91
41
|
end
|
92
42
|
end
|
93
43
|
end
|
@@ -6,21 +6,6 @@ module Lyft
|
|
6
6
|
# lyft api
|
7
7
|
#
|
8
8
|
class Oauth < Lyft::Client::Api::Base
|
9
|
-
##
|
10
|
-
# Authentication Endpoints
|
11
|
-
#
|
12
|
-
ENDPOINTS = {
|
13
|
-
access_token: '/oauth/token'
|
14
|
-
}
|
15
|
-
|
16
|
-
headers 'Content-Type': 'application/json',
|
17
|
-
'Accept': 'application/json'
|
18
|
-
|
19
|
-
def initialize(config)
|
20
|
-
super
|
21
|
-
set_default_headers
|
22
|
-
end
|
23
|
-
|
24
9
|
##
|
25
10
|
# Retrieves access token from the server.
|
26
11
|
#
|
@@ -33,39 +18,32 @@ module Lyft
|
|
33
18
|
# resp.success?
|
34
19
|
#
|
35
20
|
# @param [String] authorization_code
|
21
|
+
# @param [String] scope
|
36
22
|
# @return [HTTParty::Response]
|
37
23
|
#
|
38
|
-
def retrieve_access_token(authorization_code: nil)
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
body = request_body(
|
43
|
-
grant_type: grant_type,
|
44
|
-
authorization_code: authorization_code
|
45
|
-
)
|
46
|
-
|
47
|
-
self.class.post(path, body: body.to_json)
|
24
|
+
def retrieve_access_token(authorization_code: nil, scope: Scope::PUBLIC)
|
25
|
+
body = build_auth_body(authorization_code, scope)
|
26
|
+
resp = connection.post '/oauth/token', body
|
27
|
+
handle_response(resp)
|
48
28
|
end
|
49
29
|
|
50
30
|
private
|
51
31
|
|
52
|
-
def
|
53
|
-
return 'authorization_code' if has_auth_code
|
54
|
-
'client_credentials'
|
55
|
-
end
|
56
|
-
|
57
|
-
def request_body(grant_type:, authorization_code:, scope: 'public')
|
32
|
+
def build_auth_body(authorization_code, scope)
|
58
33
|
body = {}
|
59
|
-
body[:grant_type] = grant_type
|
60
|
-
|
61
|
-
|
34
|
+
body[:grant_type] = grant_type(authorization_code)
|
35
|
+
if authorization_code.present?
|
36
|
+
body[:code] = authorization_code
|
37
|
+
else
|
38
|
+
body[:scope] = scope
|
39
|
+
end
|
62
40
|
|
63
41
|
body
|
64
42
|
end
|
65
43
|
|
66
|
-
def
|
67
|
-
|
68
|
-
|
44
|
+
def grant_type(authorization_code)
|
45
|
+
return GrantType::AUTHORIZATION_CODE if authorization_code
|
46
|
+
GrantType::CLIENT_CREDENTIALS
|
69
47
|
end
|
70
48
|
end
|
71
49
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Lyft
|
2
|
+
class Client
|
3
|
+
module Api
|
4
|
+
class Oauth < Lyft::Client::Api::Base
|
5
|
+
##
|
6
|
+
# @see https://developer.lyft.com/v1/docs/authentication#section-scopes
|
7
|
+
#
|
8
|
+
class Scope
|
9
|
+
PUBLIC = 'public'
|
10
|
+
READ_RIDES = 'rides.read'
|
11
|
+
OFFLINE = 'offline'
|
12
|
+
REQUEST_RIDES = 'rides.request'
|
13
|
+
PROFILE = 'profile'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -2,169 +2,155 @@ module Lyft
|
|
2
2
|
class Client
|
3
3
|
module Api
|
4
4
|
class Rides < Lyft::Client::Api::Base
|
5
|
-
headers 'Content-Type': 'application/json'
|
6
|
-
|
7
|
-
ENDPOINTS = {
|
8
|
-
request: "/#{API_VERSION}/rides",
|
9
|
-
details: "/#{API_VERSION}/rides/{{ride_id}}",
|
10
|
-
cancel: "/#{API_VERSION}/rides/{{ride_id}}/cancel",
|
11
|
-
rating: "/#{API_VERSION}/rides/{{ride_id}}/rating",
|
12
|
-
receipt: "/#{API_VERSION}/rides/{{ride_id}}/receipt",
|
13
|
-
destination: "/#{API_VERSION}/rides/{{ride_id}}/destination",
|
14
|
-
sandbox_primetime: "/#{API_VERSION}/sandbox/primetime",
|
15
|
-
sandbox_rides: "/#{API_VERSION}/sandbox/rides/{{ride_id}}",
|
16
|
-
sandbox_ridetypes: "/#{API_VERSION}/sandbox/ridetypes/{{ride_type}}"
|
17
|
-
}
|
18
|
-
|
19
5
|
##
|
20
6
|
# Cancel a ride request.
|
21
7
|
# @see https://developer.lyft.com/docs/request-cancel
|
22
8
|
#
|
23
|
-
# @param [
|
24
|
-
# @
|
25
|
-
# @option
|
26
|
-
# @option
|
27
|
-
#
|
28
|
-
def cancel(
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
)
|
9
|
+
# @param access_token [String] The access_token (*required*)
|
10
|
+
# @param params [Hash] The lyft parameters.
|
11
|
+
# @option params [String] :ride_id (*required*)
|
12
|
+
# @option params [String] :cancel_confirmation_token
|
13
|
+
#
|
14
|
+
def cancel(access_token:, params: {})
|
15
|
+
ride_id = require_ride_id(params)
|
16
|
+
resp = connection(access_token).post do |req|
|
17
|
+
req.url "/#{Api::VERSION}/rides/#{ride_id}/cancel"
|
18
|
+
req.body = params
|
19
|
+
end
|
20
|
+
handle_response(resp)
|
35
21
|
end
|
36
22
|
|
37
23
|
##
|
38
24
|
# Update the ride destination
|
39
25
|
# @see https://developer.lyft.com/docs/request-destination
|
40
26
|
#
|
41
|
-
# @param [
|
42
|
-
# @
|
43
|
-
# @option
|
44
|
-
# @option
|
45
|
-
# @option
|
46
|
-
# @option
|
47
|
-
#
|
48
|
-
def destination(
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
)
|
27
|
+
# @param access_token [String] The access_token (*required*)
|
28
|
+
# @param params [Hash] The lyft parameters.
|
29
|
+
# @option params [String] :ride_id (*required*)
|
30
|
+
# @option params [Float] :lat (*required*)
|
31
|
+
# @option params [Float] :lng (*required*)
|
32
|
+
# @option params [String] :address
|
33
|
+
#
|
34
|
+
def destination(access_token:, params: {})
|
35
|
+
ride_id = require_ride_id(params)
|
36
|
+
resp = connection(access_token).put do |req|
|
37
|
+
req.url "/#{Api::VERSION}/rides/#{ride_id}/destination"
|
38
|
+
req.body = params
|
39
|
+
end
|
40
|
+
handle_response(resp)
|
55
41
|
end
|
56
42
|
|
57
43
|
##
|
58
44
|
# Get details of a ride.
|
59
45
|
# @see https://developer.lyft.com/docs/ride-request-details
|
60
46
|
#
|
61
|
-
# @param [
|
62
|
-
# @
|
63
|
-
# @option
|
64
|
-
#
|
65
|
-
def details(
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
47
|
+
# @param access_token [String] The access_token (*required*)
|
48
|
+
# @param params [Hash] The lyft parameters.
|
49
|
+
# @option params [String] :ride_id The ride id. (*required*)
|
50
|
+
#
|
51
|
+
def details(access_token:, params: {})
|
52
|
+
ride_id = require_ride_id(params)
|
53
|
+
resp = connection(access_token).get do |req|
|
54
|
+
req.url "/#{Api::VERSION}/rides/#{ride_id}"
|
55
|
+
req.body = params
|
56
|
+
end
|
57
|
+
handle_response(resp)
|
71
58
|
end
|
72
59
|
|
73
60
|
##
|
74
61
|
# Rate a ride and/or provide a tip.
|
75
62
|
# @see https://developer.lyft.com/docs/ride-request-rating-and-tipping
|
76
63
|
#
|
77
|
-
# @param [
|
78
|
-
# @
|
79
|
-
# @option
|
80
|
-
# @option
|
81
|
-
# @option
|
82
|
-
# @option
|
83
|
-
#
|
84
|
-
def rate(
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
)
|
64
|
+
# @param access_token [String] The access_token (*required*)
|
65
|
+
# @param params [Hash] The lyft parameters.
|
66
|
+
# @option params [String] :ride_id (*required*)
|
67
|
+
# @option params [Integer] :rating Rating should be between 1 and 5 inclusive. (*required*)
|
68
|
+
# @option params [Hash] :tip The tip should include :amount and :currency.
|
69
|
+
# @option params [String] :feedback
|
70
|
+
#
|
71
|
+
def rate(access_token:, params: {})
|
72
|
+
ride_id = require_ride_id(params)
|
73
|
+
resp = connection(access_token).put do |req|
|
74
|
+
req.url "/#{Api::VERSION}/rides/#{ride_id}/rating"
|
75
|
+
req.body = params
|
76
|
+
end
|
77
|
+
handle_response(resp)
|
91
78
|
end
|
92
|
-
|
79
|
+
alias tip rate
|
93
80
|
|
94
81
|
##
|
95
82
|
# Get receipt for ride
|
96
83
|
# @see https://developer.lyft.com/docs/request-receipt
|
97
84
|
#
|
98
|
-
# @param [
|
99
|
-
# @
|
100
|
-
# @option
|
101
|
-
#
|
102
|
-
def receipt(
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
85
|
+
# @param access_token [String] The access_token (*required*)
|
86
|
+
# @param params [Hash] The lyft parameters.
|
87
|
+
# @option params [String] :ride_id (*required*)
|
88
|
+
#
|
89
|
+
def receipt(access_token:, params: {})
|
90
|
+
ride_id = require_ride_id(params)
|
91
|
+
resp = connection(access_token).get do |req|
|
92
|
+
req.url "/#{Api::VERSION}/rides/#{ride_id}/receipt"
|
93
|
+
req.params = params
|
94
|
+
end
|
95
|
+
handle_response(resp)
|
108
96
|
end
|
109
97
|
|
110
98
|
##
|
111
99
|
# Request a ride.
|
112
100
|
# @see https://developer.lyft.com/docs/request
|
113
101
|
#
|
114
|
-
# @param [
|
115
|
-
# @
|
116
|
-
# @option
|
117
|
-
# @option
|
118
|
-
# @option
|
119
|
-
# @option
|
120
|
-
#
|
121
|
-
def request(
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
)
|
102
|
+
# @param access_token [String] The access_token (*required*)
|
103
|
+
# @param params [Hash] The lyft parameters.
|
104
|
+
# @option params [Hash] :origin The origin should contain :lat and :lng
|
105
|
+
# @option params [Hash] :destination The destination should contain :lat and :lng.
|
106
|
+
# @option params [String] :ride_type
|
107
|
+
# @option params [String] :primetime_confirmation_token
|
108
|
+
#
|
109
|
+
def request(access_token:, params: {})
|
110
|
+
resp = connection(access_token).post do |req|
|
111
|
+
req.url "/#{Api::VERSION}/rides"
|
112
|
+
req.body = params
|
113
|
+
end
|
114
|
+
handle_response(resp)
|
128
115
|
end
|
129
116
|
|
130
117
|
##
|
131
118
|
# Preset the ridetypes in area.
|
132
119
|
#
|
133
120
|
# @raise [RuntimeError] Raises if not in sandbox mode.
|
134
|
-
# @param [
|
135
|
-
# @
|
136
|
-
# @option
|
137
|
-
# @option
|
138
|
-
# @option
|
121
|
+
# @param access_token [String] The access_token (*required*)
|
122
|
+
# @param params [Hash] The lyft parameters.
|
123
|
+
# @option params [Float] :lat (*required*)
|
124
|
+
# @option params [Float] :lng (*required*)
|
125
|
+
# @option params [Array<String>] :ride_types The ride types to make available.
|
139
126
|
# Accepted values are 'lyft', 'lyft_line', 'lyft_plus', and 'lyft_suv'.
|
140
127
|
#
|
141
|
-
def set_ridetypes(
|
128
|
+
def set_ridetypes(access_token:, params: {})
|
142
129
|
validate_sandboxed
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
)
|
130
|
+
resp = connection(access_token).put do |req|
|
131
|
+
req.url "/#{Api::VERSION}/sandbox/ridetypes"
|
132
|
+
req.body = params
|
133
|
+
end
|
134
|
+
handle_response(resp)
|
149
135
|
end
|
150
136
|
|
151
137
|
##
|
152
138
|
# Propogate ride through different states.
|
153
139
|
#
|
154
140
|
# @raise [RuntimeError] Raises if not in sandbox mode.
|
155
|
-
# @param [
|
156
|
-
# @
|
157
|
-
# @option
|
158
|
-
# @option
|
141
|
+
# @param access_token [String] The access_token (*required*)
|
142
|
+
# @param params [Hash] The lyft parameters.
|
143
|
+
# @option params [String] :ride_id (*required*)
|
144
|
+
# @option params [String] :status The status of the ride
|
159
145
|
#
|
160
|
-
def set_status(
|
146
|
+
def set_status(access_token:, params: {})
|
161
147
|
validate_sandboxed
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
)
|
148
|
+
ride_id = require_ride_id(params)
|
149
|
+
resp = connection(access_token).put do |req|
|
150
|
+
req.url "/#{Api::VERSION}/sandbox/rides/#{ride_id}"
|
151
|
+
req.body = params
|
152
|
+
end
|
153
|
+
handle_response(resp)
|
168
154
|
end
|
169
155
|
|
170
156
|
##
|
@@ -179,20 +165,19 @@ module Lyft
|
|
179
165
|
# )
|
180
166
|
#
|
181
167
|
# @raise [RuntimeError] Raises if not in sandbox mode.
|
182
|
-
# @param [
|
183
|
-
# @
|
184
|
-
# @option
|
185
|
-
# @option
|
186
|
-
# @option
|
168
|
+
# @param access_token [String] The access_token (*required*)
|
169
|
+
# @param params [Hash] The lyft parameters.
|
170
|
+
# @option params [Float] :lat (*required*)
|
171
|
+
# @option params [Float] :lng (*required*)
|
172
|
+
# @option params [String] :primetime_percentage
|
187
173
|
#
|
188
|
-
def set_primetime(
|
174
|
+
def set_primetime(access_token:, params: {})
|
189
175
|
validate_sandboxed
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
)
|
176
|
+
resp = connection(access_token).put do |req|
|
177
|
+
req.url "/#{Api::VERSION}/sandbox/primetime"
|
178
|
+
req.body = params
|
179
|
+
end
|
180
|
+
handle_response(resp)
|
196
181
|
end
|
197
182
|
|
198
183
|
##
|
@@ -201,22 +186,21 @@ module Lyft
|
|
201
186
|
#
|
202
187
|
# @raise [RuntimeError] Raises if not in sandbox mode.
|
203
188
|
# @raise [ArgumentError] Raises if invalid ride type.
|
204
|
-
# @
|
205
|
-
# @
|
206
|
-
# @option
|
207
|
-
# @option
|
208
|
-
# @option
|
209
|
-
#
|
210
|
-
|
189
|
+
# @param access_token [String] The access_token (*required*)
|
190
|
+
# @param params [Hash] The lyft parameters.
|
191
|
+
# @option params [String] :ride_type (*required*)
|
192
|
+
# @option params [Float] :lat (*required*)
|
193
|
+
# @option params [Float] :lng (*required*)
|
194
|
+
# @option params [Boolean] :driver_availability
|
195
|
+
#
|
196
|
+
def set_driver_availability(access_token:, params: {})
|
211
197
|
validate_sandboxed
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
options: { body: args.to_json }
|
219
|
-
)
|
198
|
+
ride_type = require_ride_type(params)
|
199
|
+
resp = connection(access_token).put do |req|
|
200
|
+
req.url "/#{Api::VERSION}/sandbox/ridetypes/#{ride_type}"
|
201
|
+
req.body = params
|
202
|
+
end
|
203
|
+
handle_response(resp)
|
220
204
|
end
|
221
205
|
|
222
206
|
private
|
@@ -226,7 +210,26 @@ module Lyft
|
|
226
210
|
# @raise [RuntimeError] Raises if not in sandbox mode.
|
227
211
|
#
|
228
212
|
def validate_sandboxed
|
229
|
-
raise 'This method is only available in sandbox mode.' unless
|
213
|
+
raise 'This method is only available in sandbox mode.' unless configuration.sandbox?
|
214
|
+
end
|
215
|
+
|
216
|
+
def validate_ride_type(params)
|
217
|
+
return if Lyft::Ride::RIDE_TYPES.include?(params[:ride_type])
|
218
|
+
raise ArgumentError, 'Invalid Ride Type'
|
219
|
+
end
|
220
|
+
|
221
|
+
def require_key(params, key)
|
222
|
+
value = params.delete(key.to_sym)
|
223
|
+
raise ArgumentError, "Missing param :#{key}" if value.blank?
|
224
|
+
value
|
225
|
+
end
|
226
|
+
|
227
|
+
def require_ride_id(params)
|
228
|
+
require_key(params, :ride_id)
|
229
|
+
end
|
230
|
+
|
231
|
+
def require_ride_type(params)
|
232
|
+
require_key(params, :ride_type)
|
230
233
|
end
|
231
234
|
end
|
232
235
|
end
|