rlyft 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,234 @@
1
+ module Lyft
2
+ class Client
3
+ module Api
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
+ ##
20
+ # Cancel a ride request.
21
+ # @see https://developer.lyft.com/docs/request-cancel
22
+ #
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
+ )
35
+ end
36
+
37
+ ##
38
+ # Update the ride destination
39
+ # @see https://developer.lyft.com/docs/request-destination
40
+ #
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
+ )
55
+ end
56
+
57
+ ##
58
+ # Get details of a ride.
59
+ # @see https://developer.lyft.com/docs/ride-request-details
60
+ #
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
+ )
71
+ end
72
+
73
+ ##
74
+ # Rate a ride and/or provide a tip.
75
+ # @see https://developer.lyft.com/docs/ride-request-rating-and-tipping
76
+ #
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
+ )
91
+ end
92
+ alias_method :tip, :rate
93
+
94
+ ##
95
+ # Get receipt for ride
96
+ # @see https://developer.lyft.com/docs/request-receipt
97
+ #
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
+ )
108
+ end
109
+
110
+ ##
111
+ # Request a ride.
112
+ # @see https://developer.lyft.com/docs/request
113
+ #
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
+ )
128
+ end
129
+
130
+ ##
131
+ # Preset the ridetypes in area.
132
+ #
133
+ # @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.
139
+ # Accepted values are 'lyft', 'lyft_line', 'lyft_plus', and 'lyft_suv'.
140
+ #
141
+ def set_ridetypes(args = {})
142
+ 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
+ )
149
+ end
150
+
151
+ ##
152
+ # Propogate ride through different states.
153
+ #
154
+ # @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
159
+ #
160
+ def set_status(args = {})
161
+ 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
+ )
168
+ end
169
+
170
+ ##
171
+ # Set the primetime percentage in area
172
+ #
173
+ # @example Set the prime time percentage.
174
+ # client.rides.set_primetime(
175
+ # access_token: 'my_token',
176
+ # lat: 37.7833,
177
+ # lng: -122.4167,
178
+ # primetime_percentage: '25%'
179
+ # )
180
+ #
181
+ # @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
187
+ #
188
+ def set_primetime(args = {})
189
+ 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
+ )
196
+ end
197
+
198
+ ##
199
+ # Set driver availability for a ride type in an
200
+ # area.
201
+ #
202
+ # @raise [RuntimeError] Raises if not in sandbox mode.
203
+ # @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 = {})
211
+ 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
+ )
220
+ end
221
+
222
+ private
223
+
224
+ ##
225
+ # Validates if the current configuration is using lyft sandbox.
226
+ # @raise [RuntimeError] Raises if not in sandbox mode.
227
+ #
228
+ def validate_sandboxed
229
+ raise 'This method is only available in sandbox mode.' unless @configuration.sandbox?
230
+ end
231
+ end
232
+ end
233
+ end
234
+ end
@@ -0,0 +1,48 @@
1
+ module Lyft
2
+ class Client
3
+ module Api
4
+ class User < Lyft::Client::Api::Base
5
+ ENDPOINTS = {
6
+ history: "/#{API_VERSION}/rides",
7
+ profile: "/#{API_VERSION}/profile"
8
+ }
9
+
10
+ ##
11
+ # Get ride history
12
+ #
13
+ # @param [Hash] args
14
+ # @option args [String] :access_token (*required*)
15
+ # @option args [DateTime] :start_time (*required*)
16
+ # @option args [DateTime] :end_time
17
+ # @option args [Integer] :limit
18
+ #
19
+ def ride_history(args = {})
20
+ args.delete(:end_time) if args[:end_time].blank?
21
+ args.delete(:limit) if args[:limit].blank?
22
+
23
+ make_request(
24
+ http_method: :get,
25
+ endpoint: path_for(:history),
26
+ access_token: args.delete(:access_token),
27
+ options: { query: args }
28
+ )
29
+ end
30
+
31
+ ##
32
+ # Get user's profile
33
+ #
34
+ # @param [Hash] args
35
+ # @option args [String] :access_token (*required*)
36
+ #
37
+ def profile(args = {})
38
+ make_request(
39
+ http_method: :get,
40
+ endpoint: path_for(:profile),
41
+ access_token: args.delete(:access_token),
42
+ options: { query: args }
43
+ )
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,23 @@
1
+ module Lyft
2
+ class Client
3
+ class Configuration
4
+ attr_reader :client_id, :client_secret, :debug_output, :use_sandbox
5
+
6
+ def initialize(args = {})
7
+ raise ArgumentError, ':client_id is missing' if args[:client_id].blank?
8
+ raise ArgumentError, ':client_secret is missing' if args[:client_secret].blank?
9
+
10
+ @client_id = args.fetch(:client_id)
11
+ @client_secret = args.fetch(:client_secret)
12
+ @use_sandbox = args.fetch(:use_sandbox, false)
13
+ @debug_output = args[:debug_output]
14
+
15
+ @client_secret = "SANDBOX-#{@client_secret}" if sandbox?
16
+ end
17
+
18
+ def sandbox?
19
+ @use_sandbox
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ module Lyft
2
+ class Client
3
+ class MashedParser < HTTParty::Parser
4
+ protected
5
+
6
+ def xml
7
+ mashed super
8
+ end
9
+
10
+ def json
11
+ mashed super
12
+ end
13
+
14
+ def yaml
15
+ mashed super
16
+ end
17
+
18
+ private
19
+
20
+ def mashed(body)
21
+ if body.is_a? Hash
22
+ Hashie::Mash.new body
23
+ elsif body.is_a? Array
24
+ body.map(&method(:mashed))
25
+ else
26
+ body
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
data/lib/lyft/client.rb CHANGED
@@ -2,20 +2,72 @@ module Lyft
2
2
  ##
3
3
  # Client for making Lyft api requests
4
4
  #
5
- # @attr [String] client_id The client id for lyft app.
6
- # @attr [String] client_secret The client secret for lyft app.
7
- #
8
5
  class Client
9
- include Api::Oauth
10
- include Api::Availability
11
- include Api::Rides
12
- include Api::Users
6
+ ##
7
+ # Keep track of what we are namespacing.
8
+ #
9
+ @@namespaces = []
10
+
11
+ attr_reader :configuration
12
+
13
+ ##
14
+ # Defines a method to access class instance.
15
+ #
16
+ # @example Create a namespace for availability
17
+ # namespace :availability #=> Lyft::Client::Availability.new
18
+ #
19
+ def self.namespace(name)
20
+ converted = name.to_s.split('_').map(&:capitalize).join
21
+ klass = Lyft::Client::Api.const_get(converted)
22
+ create_instance(klass)
23
+ end
24
+
25
+ ##
26
+ # Dynamically creates an attr_reader for each client space
27
+ # and sets it to the initalized values
28
+ #
29
+ def self.create_instance(klass)
30
+ reader = klass.to_s.split('::').last.underscore
31
+ @@namespaces << reader
32
+
33
+ define_method(reader.to_sym) { klass.new @@config }
34
+ end
35
+
36
+ ##
37
+ # Class to handle availability api calls.
38
+ # @return Lyft::Client::Api::Oauth
39
+ #
40
+ namespace :oauth
41
+
42
+ ##
43
+ # Class to handle availability api calls.
44
+ # @return Lyft::Client::Api::Availability
45
+ #
46
+ namespace :availability
47
+
48
+ ##
49
+ # Class to handle availability api calls.
50
+ # @return Lyft::Client::Api::Rides
51
+ #
52
+ namespace :rides
13
53
 
14
- attr_accessor :client_id,
15
- :client_secret
54
+ ##
55
+ # Class to handle availability api calls.
56
+ # @return Lyft::Client::Api::User
57
+ #
58
+ namespace :user
16
59
 
17
- def initialize
18
- yield self
60
+ ##
61
+ # The initializer
62
+ #
63
+ # @param [Hash] args
64
+ # @option args [String] :client_id
65
+ # @option args [String] :client_secret
66
+ # @option args [Boolean] :debug_output
67
+ #
68
+ def initialize(args = {})
69
+ @configuration = Lyft::Client::Configuration.new args
70
+ @@config = @configuration
19
71
  end
20
72
  end
21
73
  end
@@ -0,0 +1,13 @@
1
+ module Lyft
2
+ module Ride
3
+ module Status
4
+ PENDING = 'pending'.freeze
5
+ ACCEPTED = 'accepted'.freeze
6
+ ARRIVED = 'arrived'.freeze
7
+ PICKED_UP = 'pickedUp'.freeze
8
+ DROPPED_OFF = 'droppedOff'.freeze
9
+ CANCELED = 'canceled'.freeze
10
+ UNKNOWN = 'unknown'.freeze
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module Lyft
2
+ module Ride
3
+ module Type
4
+ LYFT = 'lyft'.freeze
5
+ LYFT_LINE = 'lyft_line'.freeze
6
+ LYFT_PLUS = 'lyft_plus'.freeze
7
+ LYFT_SUV = 'lyft_suv'.freeze
8
+ end
9
+ end
10
+ end
data/lib/lyft/ride.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'lyft/ride/status'
2
+ require 'lyft/ride/type'
3
+
4
+ module Lyft
5
+ module Ride
6
+ ##
7
+ # List of values for ride types
8
+ # @return [Array<String>] A list of ride type values
9
+ #
10
+ RIDE_TYPES = Lyft::Ride::Type.constants.map { |c| Lyft::Ride::Type.const_get(c) }
11
+
12
+ ##
13
+ # List of values for ride status
14
+ # @return [Array<String>] A list of ride status values
15
+ #
16
+ RIDE_STATUSES = Lyft::Ride::Status.constants.map { |c| Lyft::Ride::Status.const_get(c) }
17
+ end
18
+ end
data/lib/lyft/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Lyft
2
- VERSION = "0.1.1"
2
+ VERSION = '1.0.0'
3
3
  end
data/lib/lyft.rb CHANGED
@@ -1,14 +1,23 @@
1
+ # Development gems
2
+ require 'pry'
3
+
1
4
  # 3rd party Libraries
2
5
  require 'httparty'
3
6
  require 'active_support/all'
7
+ require 'hashie'
8
+
9
+ require 'lyft/ride'
10
+ require 'lyft/version'
11
+
12
+ require 'lyft/client/configuration'
13
+ require 'lyft/client/mashed_parser'
4
14
 
5
- # Gem libraries
6
- Dir[File.dirname(__FILE__) + '/lyft/**/*.rb'].each { |file| require file }
15
+ require 'lyft/client/api/base'
16
+ require 'lyft/client/api/availability'
17
+ require 'lyft/client/api/oauth'
18
+ require 'lyft/client/api/rides'
19
+ require 'lyft/client/api/user'
7
20
 
8
- module Lyft
9
- API_URI = 'https://api.lyft.com'
21
+ require 'lyft/client'
10
22
 
11
- def self.endpoint(path)
12
- API_URI + path
13
- end
14
- end
23
+ module Lyft; end
data/lyft.gemspec CHANGED
@@ -29,10 +29,15 @@ Gem::Specification.new do |spec|
29
29
 
30
30
  spec.add_runtime_dependency 'httparty'
31
31
  spec.add_runtime_dependency 'activesupport'
32
+ spec.add_runtime_dependency 'hashie'
32
33
 
33
34
  spec.add_development_dependency "bundler", "~> 1.11"
34
35
  spec.add_development_dependency "rake", "~> 10.0"
35
36
  spec.add_development_dependency "rspec"
37
+ spec.add_development_dependency "rspec-its"
38
+ spec.add_development_dependency "rubocop", "~> 0.47.1"
36
39
  spec.add_development_dependency "pry"
40
+ spec.add_development_dependency "vcr"
41
+ spec.add_development_dependency "webmock"
37
42
  spec.add_development_dependency "yard"
38
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rlyft
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Scott
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-05 00:00:00.000000000 Z
11
+ date: 2017-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: hashie
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,34 @@ dependencies:
80
94
  - - ">="
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-its
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.47.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.47.1
83
125
  - !ruby/object:Gem::Dependency
84
126
  name: pry
85
127
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +136,34 @@ dependencies:
94
136
  - - ">="
95
137
  - !ruby/object:Gem::Version
96
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: vcr
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: webmock
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
97
167
  - !ruby/object:Gem::Dependency
98
168
  name: yard
99
169
  requirement: !ruby/object:Gem::Requirement
@@ -116,6 +186,10 @@ extensions: []
116
186
  extra_rdoc_files: []
117
187
  files:
118
188
  - ".gitignore"
189
+ - ".rspec"
190
+ - ".rubocop.yml"
191
+ - ".ruby-gemset"
192
+ - ".ruby-version"
119
193
  - Gemfile
120
194
  - LICENSE.txt
121
195
  - README.md
@@ -123,19 +197,17 @@ files:
123
197
  - bin/console
124
198
  - bin/setup
125
199
  - lib/lyft.rb
126
- - lib/lyft/api/availability/cost.rb
127
- - lib/lyft/api/availability/eta.rb
128
- - lib/lyft/api/availability/nearby_drivers.rb
129
- - lib/lyft/api/availability/ride_types.rb
130
- - lib/lyft/api/oauth/public_token.rb
131
- - lib/lyft/api/rides/ride.rb
132
- - lib/lyft/api/rides/ride_cancel.rb
133
- - lib/lyft/api/rides/ride_details.rb
134
- - lib/lyft/api/rides/ride_rating_and_tipping.rb
135
- - lib/lyft/api/rides/ride_receipt.rb
136
- - lib/lyft/api/users/profile.rb
137
- - lib/lyft/api/users/ride_history.rb
138
200
  - lib/lyft/client.rb
201
+ - lib/lyft/client/api/availability.rb
202
+ - lib/lyft/client/api/base.rb
203
+ - lib/lyft/client/api/oauth.rb
204
+ - lib/lyft/client/api/rides.rb
205
+ - lib/lyft/client/api/user.rb
206
+ - lib/lyft/client/configuration.rb
207
+ - lib/lyft/client/mashed_parser.rb
208
+ - lib/lyft/ride.rb
209
+ - lib/lyft/ride/status.rb
210
+ - lib/lyft/ride/type.rb
139
211
  - lib/lyft/version.rb
140
212
  - lyft.gemspec
141
213
  homepage: https://github.com/skukx/lyft
@@ -163,4 +235,3 @@ signing_key:
163
235
  specification_version: 4
164
236
  summary: Ruby wrapper for Lyft API.
165
237
  test_files: []
166
- has_rdoc: