rlyft 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,92 +2,42 @@ module Lyft
2
2
  class Client
3
3
  module Api
4
4
  class Base
5
- include HTTParty
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 set_debug_output
62
- self.class.default_options[:debug_output] = @configuration.debug_output
63
- end
11
+ def connection(access_token = nil)
12
+ Faraday.new(url: BASE_URL) do |conn|
13
+ # Headers
14
+ conn.headers = configuration.headers
64
15
 
65
- protected
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
- # Make a request
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
- set_authorization_header(access_token)
83
- self.class.send(http_method, endpoint, options)
32
+ conn.adapter Faraday.default_adapter
33
+ end
84
34
  end
85
35
 
86
- def set_authorization_header(access_token)
87
- self.class.default_options[:headers] ||= {}
88
- self.class
89
- .default_options[:headers]
90
- .merge! Authorization: "Bearer #{access_token}"
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
- path = path_for(:access_token)
40
-
41
- grant_type = get_grant_type(authorization_code.present?)
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 get_grant_type(has_auth_code)
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
- body[:scope] = scope unless authorization_code.present?
61
- body[:code] = authorization_code if authorization_code.present?
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 set_default_headers
67
- self.class.default_options[:headers].delete(:Authorization)
68
- self.class.basic_auth @configuration.client_id, @configuration.client_secret
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,12 @@
1
+ module Lyft
2
+ class Client
3
+ module Api
4
+ class Oauth < Lyft::Client::Api::Base
5
+ class GrantType
6
+ AUTHORIZATION_CODE = 'authorization_code'
7
+ CLIENT_CREDENTIALS = 'client_credentials'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ 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 [Hash] args
24
- # @option args [String] :access_token (*required*)
25
- # @option args [String] :ride_id (*required*)
26
- # @option args [String] :cancel_confirmation_token
27
- #
28
- def cancel(args = {})
29
- make_request(
30
- http_method: :post,
31
- endpoint: path_for(:cancel, ride_id: args.delete(:ride_id)),
32
- access_token: args.delete(:access_token),
33
- options: { body: args.to_json }
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 [Hash] args
42
- # @option args [String] :access_token (*required*)
43
- # @option args [String] :ride_id (*required*)
44
- # @option args [Float] :lat (*required*)
45
- # @option args [Float] :lng (*required*)
46
- # @option args [String] :address
47
- #
48
- def destination(args = {})
49
- make_request(
50
- http_method: :put,
51
- endpoint: path_for(:destination, ride_id: args.delete(:ride_id)),
52
- access_token: args.delete(:access_token),
53
- options: { body: args.to_json }
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 [Hash] args
62
- # @option args [String] :access_token (*required*)
63
- # @option args [String] :ride_id The ride id. (*required*)
64
- #
65
- def details(args = {})
66
- make_request(
67
- http_method: :get,
68
- endpoint: path_for(:details, ride_id: args.delete(:ride_id)),
69
- access_token: args.delete(:access_token)
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 [Hash] args
78
- # @option args [String] :access_token (*required*)
79
- # @option args [String] :ride_id (*required*)
80
- # @option args [Integer] :rating Rating should be between 1 and 5 inclusive. (*required*)
81
- # @option args [Hash] :tip The tip should include :amount and :currency.
82
- # @option args [String] :feedback
83
- #
84
- def rate(args = {})
85
- make_request(
86
- http_method: :put,
87
- endpoint: path_for(:rating, ride_id: args.delete(:ride_id)),
88
- access_token: args.delete(:access_token),
89
- options: { body: args.to_json }
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
- alias_method :tip, :rate
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 [Hash] args
99
- # @option args [String] :access_token (*required*)
100
- # @option args [String] :ride_id (*required*)
101
- #
102
- def receipt(args = {})
103
- make_request(
104
- http_method: :get,
105
- endpoint: path_for(:receipt, ride_id: args.delete(:ride_id)),
106
- access_token: args.delete(:access_token)
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 [Hash] args
115
- # @option args [String] :access_token (*required*)
116
- # @option args [Hash] :origin The origin should contain :lat and :lng
117
- # @option args [Hash] :destination The destination should contain :lat and :lng.
118
- # @option args [String] :ride_type
119
- # @option args [String] :primetime_confirmation_token
120
- #
121
- def request(args = {})
122
- make_request(
123
- http_method: :post,
124
- endpoint: path_for(:request),
125
- access_token: args.delete(:access_token),
126
- options: { body: args.to_json }
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 [Hash] args
135
- # @option args [String] :access_token (*required*)
136
- # @option args [Float] :lat (*required*)
137
- # @option args [Float] :lng (*required*)
138
- # @option args [Array<String>] :ride_types The ride types to make available.
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(args = {})
128
+ def set_ridetypes(access_token:, params: {})
142
129
  validate_sandboxed
143
- make_request(
144
- http_method: :put,
145
- endpoint: path_for(:sandbox_ridetypes),
146
- access_token: args.delete(:access_token),
147
- options: { body: args.to_json }
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 [Hash] args
156
- # @option args [String] :access_token (*required*)
157
- # @option args [String] :ride_id (*required*)
158
- # @option args [String] :status The status of the ride
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(args = {})
146
+ def set_status(access_token:, params: {})
161
147
  validate_sandboxed
162
- make_request(
163
- http_method: :put,
164
- endpoint: path_for(:sandbox_rides, ride_id: args.delete(:ride_id)),
165
- access_token: args.delete(:access_token),
166
- options: { body: args.to_json }
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 [Hash] args
183
- # @option args [String] :access_token (*required*)
184
- # @option args [Float] :lat (*required*)
185
- # @option args [Float] :lng (*required*)
186
- # @option args [String] :primetime_percentage
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(args = {})
174
+ def set_primetime(access_token:, params: {})
189
175
  validate_sandboxed
190
- make_request(
191
- http_method: :put,
192
- endpoint: path_for(:sandbox_primetime),
193
- access_token: args.delete(:access_token),
194
- options: { body: args.to_json }
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
- # @option args [String] :access_token (*required*)
205
- # @option args [String] :ride_type (*required*)
206
- # @option args [Float] :lat (*required*)
207
- # @option args [Float] :lng (*required*)
208
- # @option args [Boolean] :driver_availability
209
- #
210
- def set_driver_availability(args = {})
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
- raise ArgumentError, 'Invalid Ride Type' unless Lyft::Ride::RIDE_TYPES.include? args[:ride_type]
213
-
214
- make_request(
215
- http_method: :put,
216
- endpoint: path_for(:sandbox_ridetypes, ride_type: args.delete(:ride_type)),
217
- access_token: args.delete(:access_token),
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 @configuration.sandbox?
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