rlyft 0.1.1 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e30ad205ab2786789b29d6628a7e019b728c4004
4
- data.tar.gz: 52ef840de85b6ab593e847be586a2293dc0c404c
3
+ metadata.gz: c0503db11b90ab5adce78f79a86c849857afb094
4
+ data.tar.gz: bbe410d1d2206df8a3d59e2329a23201a9da6ef8
5
5
  SHA512:
6
- metadata.gz: 7547028ed225fb54fa166120af7901097552b5ea6d5e538389197b63414a45cb4f58d9c75e56d458511d6cb021cdcd3360d576192c9ed7ae5d6c505f6e25cc21
7
- data.tar.gz: 5b9742fff70a7d6e6c47017edda5f35ba3b83eb56cd3726fe7a9139bf9fa0eb8bbe96bbba8075e45843262db6ec0b8bc0e1bccaa60896bc6a858cf05b5912da3
6
+ metadata.gz: 4287be90d72943fc8fa5afe601c54eb81faf53c232701ef30f31600485afcd13ab4e9d90a65e993c23514e506091d6f203bf7fb8ff55f5d7794d574dbc41e538
7
+ data.tar.gz: c3c5d5ecd300568bbb0af195e06a3d6057c5b79e3c9ffa6450d0e40fb50a6f71ac9cc8bfd6b21be4be709cc2b76ae90ba2f847f88e0d237361c3ee040d08c84e
data/.gitignore CHANGED
@@ -8,3 +8,6 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  lyft_setup.rb
11
+
12
+ # Ignore application configuration
13
+ /config/application.yml
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ AllCops:
2
+ Exclude:
3
+ - bin/**/*
4
+ - spec/**/*
5
+ - lyft_setup.rb
6
+ - lyft.gemspec
7
+ - Rakefile
8
+ TargetRubyVersion: 2.3
9
+ Metrics/BlockLength:
10
+ Enabled: false
11
+ Metrics/LineLength:
12
+ Enabled: false
13
+ Style/AccessorMethodName:
14
+ Enabled: false
15
+ Style/ClassVars:
16
+ Enabled: false
17
+ Style/Documentation:
18
+ Enabled: false
19
+ Style/FrozenStringLiteralComment:
20
+ Enabled: false
21
+ Style/MutableConstant:
22
+ Enabled: false
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ rlyft
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.1
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Lyft
2
+ [![CircleCI](https://circleci.com/gh/skukx/rlyft.svg?style=shield)](https://circleci.com/gh/skukx/rlyft)
2
3
 
3
4
  Simple wrapper for interacting with Lyft's public api.
4
5
 
@@ -17,41 +18,117 @@ And then execute:
17
18
  ## Setup
18
19
 
19
20
  ```ruby
20
- client = Lyft::Client.new do |c|
21
- c.client_id = 'client_id'
22
- c.client_secret = 'client_secret'
23
- end
21
+ client = Lyft::Client.new(
22
+ client_id: 'client_id',
23
+ client_secret: 'client_secret',
24
+ debug_output: STDOUT,
25
+ use_sandbox: true
26
+ )
24
27
  ```
25
28
 
26
29
  ## Using Public Api
30
+ Get Access Token:
31
+
32
+ ```ruby
33
+ # Public token
34
+ client.authentication.retrieve_access_token
35
+
36
+ # When using oauth.
37
+ client.authentication.retrieve_access_token authorization_code: 'auth_code'
38
+ ```
39
+
27
40
  Calculate Lyft cost.
28
41
 
29
42
  ```ruby
30
- client.cost start_lat: 37.7772,
31
- start_lng: -122.4233,
32
- end_lat: 37.7972,
33
- end_lng: -122.4533
43
+ client.availability.cost access_token: 'access_token'
44
+ start_lat: 37.7772,
45
+ start_lng: -122.4233,
46
+ end_lat: 37.7972,
47
+ end_lng: -122.4533
34
48
  ```
35
49
 
36
50
  Time for nearest driver to reach location.
37
51
 
38
52
  ```ruby
39
- client.eta lat: 37.7772,
40
- lng: -122.4233
53
+ client.availability.eta access_token: 'token',
54
+ lat: 37.7772,
55
+ lng: -122.4233
41
56
  ```
42
57
 
43
58
  Get the location of nearby drivers.
44
59
 
45
60
  ```ruby
46
- client.nearby_drivers lat: 37.7772,
47
- lng: -122.4233
61
+ client.availability.nearby_drivers access_token: 'token',
62
+ lat: 37.7772,
63
+ lng: -122.4233
48
64
  ```
49
65
 
50
66
  Get available ride types.
51
67
 
52
68
  ```ruby
53
- client.ride_types lat: 37.7772,
54
- lng: -122.4233
69
+ client.availability.ride_types access_token: 'token',
70
+ lat: 37.7772,
71
+ lng: -122.4233
72
+ ```
73
+
74
+ Request a ride
75
+ ```ruby
76
+ client.rides.request access_token: 'token',
77
+ origin: { lat: 37.7772, lng: -122.4233 },
78
+ ride_type: 'lyft'
79
+ ```
80
+
81
+ Cancel a ride
82
+ ```ruby
83
+ client.rides.cancel access_token: 'token',
84
+ ride_id: '123'
85
+
86
+ # When cancel_confirmation_token is needed.
87
+ client.rides.cancel access_token: 'token',
88
+ ride_id: '123',
89
+ cancel_confirmation_token: 'cancellation_token'
90
+ ```
91
+
92
+ ## Using the Sandbox:
93
+
94
+ Set available ride types
95
+ ```ruby
96
+ client.rides.set_ridetypes(
97
+ access_token: 'my_token',
98
+ lat: 37.7833,
99
+ lng: -122.4167,
100
+ ride_types: [Lyft::Ride::Type::LYFT, Lyft::Ride::Type::LYFT_PLUS]
101
+ )
102
+ ```
103
+
104
+ Set ride status
105
+ ```ruby
106
+ client.rides.set_status(
107
+ access_token: 'my_token',
108
+ ride_id: 'my_ride_id',
109
+ status: Lyft::Ride::Status::ACCEPTED
110
+ )
111
+ ```
112
+
113
+ Set driver availability
114
+ ```ruby
115
+ client.rides.set_driver_availability(
116
+ access_token: 'my_token',
117
+ ride_type: Lyft::Ride::Type::LYFT_SUV
118
+ lat: 37.7833,
119
+ lng: -122.4167,
120
+ driver_availability: true
121
+ )
122
+ ```
123
+
124
+ Set primetime percentage
125
+ ```ruby
126
+ client.rides.set_primetime(
127
+ access_token: 'my_token',
128
+ lat: 37.7833,
129
+ lng: -122.4167,
130
+ primetime_percentage: '25%'
131
+ )
55
132
  ```
56
133
 
57
134
  ## Development
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
- task :default => :spec
2
+ require 'rspec/core/rake_task'
3
+
4
+ begin
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
8
+ rescue LoadError
9
+ # no rspec available
10
+ end
@@ -0,0 +1,107 @@
1
+ module Lyft
2
+ class Client
3
+ module Api
4
+ class Availability < Lyft::Client::Api::Base
5
+ ENDPOINTS = {
6
+ cost: "/#{API_VERSION}/cost",
7
+ eta: "/#{API_VERSION}/eta",
8
+ nearby_drivers: "/#{API_VERSION}/drivers",
9
+ ride_types: "/#{API_VERSION}/ridetypes"
10
+ }
11
+
12
+ ##
13
+ # Get the estimated cost of ride.
14
+ #
15
+ # @example Get the estimated cost of a ride.
16
+ # client.availability.cost(
17
+ # start_lat: 37.7772,
18
+ # start_lng: -122.4233,
19
+ # end_lat: 37.7972,
20
+ # end_lng: -122.4533,
21
+ # access_token: 'token'
22
+ # )
23
+ #
24
+ # @param [Hash] args
25
+ # @option args [String] :access_token (*required*)
26
+ # @option args [Float] :start_lat The latitude of starting point. (*required*)
27
+ # @option args [Float] :start_lng The longitude of starting point. (*required*)
28
+ # @option args [Float] :end_lat The latitude of the end point. (*required*)
29
+ # @option args [Float] :end_lng The longitude of the end point. (*required*)
30
+ #
31
+ def cost(args = {})
32
+ make_request(
33
+ http_method: :get,
34
+ endpoint: path_for(:cost),
35
+ access_token: args.delete(:access_token),
36
+ options: { query: args }
37
+ )
38
+ end
39
+
40
+ ##
41
+ # Get eta for lyft driver to reach location
42
+ #
43
+ # @example Get lyft eta to a specified location.
44
+ # client.availability.eta lat: 37.7772,
45
+ # lng: -122.4233
46
+ # access_token: 'token'
47
+ #
48
+ # @param [Hash] args
49
+ # @option args [String] :access_token (*required*)
50
+ # @option args [Float] :lat The latitude of pickup. (*required*)
51
+ # @option args [Float] :lng The longitude of pickup. (*required*)
52
+ #
53
+ def eta(args = {})
54
+ make_request(
55
+ http_method: :get,
56
+ endpoint: path_for(:eta),
57
+ access_token: args.delete(:access_token),
58
+ options: { query: args }
59
+ )
60
+ end
61
+
62
+ ##
63
+ # Get positions of nearby drivers
64
+ #
65
+ # @example Get location of nearby drivers.
66
+ # client.availability.nearby_drivers lat: 37.7772,
67
+ # lng: -122.4233,
68
+ # access_token: 'token'
69
+ #
70
+ # @param [Hash] args
71
+ # @option args [String] :access_token (*required*)
72
+ # @option args [Float] :lat The latitude of pickup. (*required*)
73
+ # @option args [Float] :lng The longitude of pickup. (*required*)
74
+ #
75
+ def nearby_drivers(args = {})
76
+ make_request(
77
+ http_method: :get,
78
+ endpoint: path_for(:nearby_drivers),
79
+ access_token: args.delete(:access_token),
80
+ options: { query: args }
81
+ )
82
+ end
83
+
84
+ ##
85
+ # Get available lyft ride types
86
+ #
87
+ # @example Get available ride types for a location.
88
+ # client.availability.ride_types lat: 37.7772,
89
+ # lng: -122.4233
90
+ #
91
+ # @param [Hash] args
92
+ # @option args [String] :access_token (*required*)
93
+ # @option args [Float] :lat The latitude of pickup. (*required*)
94
+ # @option args [Float] :lng The longitude of pickup. (*required*)
95
+ #
96
+ def ride_types(args = {})
97
+ make_request(
98
+ http_method: :get,
99
+ endpoint: path_for(:nearby_drivers),
100
+ access_token: args.delete(:access_token),
101
+ options: { query: args }
102
+ )
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,95 @@
1
+ module Lyft
2
+ class Client
3
+ module Api
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
55
+
56
+ def initialize(configuration)
57
+ @configuration = configuration
58
+ set_debug_output
59
+ end
60
+
61
+ def set_debug_output
62
+ self.class.default_options[:debug_output] = @configuration.debug_output
63
+ end
64
+
65
+ protected
66
+
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, {})
81
+
82
+ set_authorization_header(access_token)
83
+ self.class.send(http_method, endpoint, options)
84
+ end
85
+
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}"
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,73 @@
1
+ module Lyft
2
+ class Client
3
+ module Api
4
+ ##
5
+ # Client for making authentication related requests to the
6
+ # lyft api
7
+ #
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
+ ##
25
+ # Retrieves access token from the server.
26
+ #
27
+ # @example Get public access token.
28
+ # resp = client.authentication.retrieve_access_token
29
+ # resp.success?
30
+ #
31
+ # @example Get access token from authorization_code.
32
+ # resp = client.authentication.retrieve_access_token authorization_code: 'auth_code'
33
+ # resp.success?
34
+ #
35
+ # @param [String] authorization_code
36
+ # @return [HTTParty::Response]
37
+ #
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)
48
+ end
49
+
50
+ private
51
+
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')
58
+ body = {}
59
+ body[:grant_type] = grant_type
60
+ body[:scope] = scope unless authorization_code.present?
61
+ body[:code] = authorization_code if authorization_code.present?
62
+
63
+ body
64
+ end
65
+
66
+ def set_default_headers
67
+ self.class.default_options[:headers].delete(:Authorization)
68
+ self.class.basic_auth @configuration.client_id, @configuration.client_secret
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end