lyft 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/lyft.rb +44 -0
- data/lib/lyft/access_token.rb +24 -0
- data/lib/lyft/api.rb +53 -0
- data/lib/lyft/api_resource.rb +87 -0
- data/lib/lyft/client.rb +30 -0
- data/lib/lyft/configuration.rb +25 -0
- data/lib/lyft/connection.rb +31 -0
- data/lib/lyft/errors.rb +48 -0
- data/lib/lyft/mash.rb +68 -0
- data/lib/lyft/oauth2.rb +138 -0
- data/lib/lyft/raise_error.rb +16 -0
- data/lib/lyft/rides.rb +15 -0
- data/lib/lyft/version.rb +3 -0
- data/lyft.gemspec +40 -0
- metadata +236 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 49d75bc253d08e220b55825a2a3e08683bc14c33
|
4
|
+
data.tar.gz: 8341d4442e5b6342c564fd2f488fbe21689053eb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 561929b70c6f26186d8bfcb6ff92eb251e75551d7eec7865c08f119121106c54bfd847944be400f990f4c7f9dd6acb75a577a2bc033dea48f01ec0d82ae8859d
|
7
|
+
data.tar.gz: d5d3930c4dd5b0e90910072b6e4f3b662a206c12ce2d8e57329234a9770715c7d54cdb16f44279fb0ab268b9e610a98862a6e7240caeabe39ad02b30b8ef6fb2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Ivan Santos
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Lyft
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/lyft`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'lyft'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install lyft
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lyft.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "lyft"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/lyft.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require "oauth2"
|
2
|
+
|
3
|
+
require 'lyft/errors'
|
4
|
+
require 'lyft/raise_error'
|
5
|
+
require 'lyft/version'
|
6
|
+
require "lyft/configuration"
|
7
|
+
|
8
|
+
require 'lyft/oauth2'
|
9
|
+
|
10
|
+
# Coerces Lyft JSON to a nice Ruby hash
|
11
|
+
# Lyft::Mash inherits from Hashie::Mash
|
12
|
+
require "hashie"
|
13
|
+
require "lyft/mash"
|
14
|
+
|
15
|
+
# Wraps a Lyft-specifc API connection
|
16
|
+
# Lyft::Connection inherits from Faraday::Connection
|
17
|
+
require "faraday"
|
18
|
+
require "lyft/connection"
|
19
|
+
|
20
|
+
# Data object to wrap API access token
|
21
|
+
require "lyft/access_token"
|
22
|
+
|
23
|
+
# Endpoints inherit from APIResource
|
24
|
+
require "lyft/api_resource"
|
25
|
+
|
26
|
+
# All of the endpoints
|
27
|
+
require "lyft/rides"
|
28
|
+
|
29
|
+
# The primary API object that makes requests.
|
30
|
+
# It composes in all of the endpoints
|
31
|
+
require "lyft/api"
|
32
|
+
|
33
|
+
|
34
|
+
module Lyft
|
35
|
+
@config = Configuration.new
|
36
|
+
|
37
|
+
class << self
|
38
|
+
attr_accessor :config
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.configure
|
42
|
+
yield self.config
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Lyft
|
2
|
+
# A simple data object to contain the token string and expiration data.
|
3
|
+
class AccessToken
|
4
|
+
attr_accessor :token, :expires_in, :expires_at
|
5
|
+
|
6
|
+
# Creates a simple data wrapper for an access token.
|
7
|
+
#
|
8
|
+
# Lyft returns only an `expires_in` value. This calculates and
|
9
|
+
# sets and `expires_at` field for convenience.
|
10
|
+
#
|
11
|
+
# @param [String] token the access token
|
12
|
+
# @param [FixNum] expires_in number of seconds the token lasts for
|
13
|
+
# @param [Time] expires_at when the token will expire.
|
14
|
+
def initialize(token=nil, expires_in=nil, expires_at=nil)
|
15
|
+
self.token = token
|
16
|
+
self.expires_in = expires_in
|
17
|
+
if expires_at.nil? and not self.expires_in.nil?
|
18
|
+
self.expires_at = Time.now + expires_in
|
19
|
+
else
|
20
|
+
self.expires_at = expires_at
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/lyft/api.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
module Lyft
|
2
|
+
class API
|
3
|
+
|
4
|
+
attr_accessor :access_token
|
5
|
+
|
6
|
+
def initialize(access_token=nil)
|
7
|
+
access_token = parse_access_token(access_token)
|
8
|
+
verify_access_token!(access_token)
|
9
|
+
@access_token = access_token
|
10
|
+
|
11
|
+
@connection = Lyft::Connection.new params: default_params,
|
12
|
+
headers: default_headers
|
13
|
+
|
14
|
+
initialize_endpoints
|
15
|
+
end
|
16
|
+
|
17
|
+
extend Forwardable # Composition over inheritance
|
18
|
+
def_delegators :@rides, :rides
|
19
|
+
|
20
|
+
private ##############################################################
|
21
|
+
|
22
|
+
def initialize_endpoints
|
23
|
+
@rides = Lyft::Rides.new(@connection)
|
24
|
+
end
|
25
|
+
|
26
|
+
def default_params
|
27
|
+
return {oauth2_access_token: @access_token.token}
|
28
|
+
end
|
29
|
+
|
30
|
+
def default_headers
|
31
|
+
return {"x-li-format" => "json"}
|
32
|
+
end
|
33
|
+
|
34
|
+
def verify_access_token!(access_token)
|
35
|
+
if not access_token.is_a? Lyft::AccessToken
|
36
|
+
raise no_access_token_error
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def parse_access_token(access_token)
|
41
|
+
if access_token.is_a? Lyft::AccessToken
|
42
|
+
return access_token
|
43
|
+
elsif access_token.is_a? String
|
44
|
+
return Lyft::AccessToken.new(access_token)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def no_access_token_error
|
49
|
+
msg = Lyft::ErrorMessages.no_access_token
|
50
|
+
Lyft::InvalidRequest.new(msg)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module Lyft
|
2
|
+
|
3
|
+
class APIResource
|
4
|
+
|
5
|
+
def initialize(connection)
|
6
|
+
@connection = connection
|
7
|
+
end
|
8
|
+
|
9
|
+
protected ############################################################
|
10
|
+
|
11
|
+
def get(path, options={})
|
12
|
+
url, params, headers = prepare_connection_params(path, options)
|
13
|
+
puts url
|
14
|
+
puts params
|
15
|
+
puts headers
|
16
|
+
response = @connection.get(url, params, headers)
|
17
|
+
|
18
|
+
return Mash.from_json(response.body)
|
19
|
+
end
|
20
|
+
|
21
|
+
def post(path=nil, body=nil, headers=nil, &block)
|
22
|
+
@connection.post(prepend_prefix(path), body, headers, &block)
|
23
|
+
end
|
24
|
+
|
25
|
+
def put(path=nil, body=nil, headers=nil, &block)
|
26
|
+
@connection.put(prepend_prefix(path), body, headers, &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
def delete(path=nil, params=nil, headers=nil, &block)
|
30
|
+
@connection.delete(prepend_prefix(path), params, headers, &block)
|
31
|
+
end
|
32
|
+
|
33
|
+
def deprecated
|
34
|
+
Lyft::Deprecated.new(Lyft::ErrorMessages.deprecated)
|
35
|
+
end
|
36
|
+
|
37
|
+
private ##############################################################
|
38
|
+
|
39
|
+
def prepend_prefix(path)
|
40
|
+
return @connection.path_prefix + path
|
41
|
+
end
|
42
|
+
|
43
|
+
def prepare_connection_params(path, options)
|
44
|
+
path = prepend_prefix(path)
|
45
|
+
path += generate_field_selectors(options)
|
46
|
+
|
47
|
+
headers = options.delete(:headers) || {}
|
48
|
+
|
49
|
+
params = format_options_for_query(options)
|
50
|
+
|
51
|
+
return [path, params, headers]
|
52
|
+
end
|
53
|
+
|
54
|
+
# Dasherizes the param keys
|
55
|
+
def format_options_for_query(options)
|
56
|
+
options.reduce({}) do |list, kv|
|
57
|
+
key, value = kv.first.to_s.gsub("_","-"), kv.last
|
58
|
+
list[key] = value
|
59
|
+
list
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def generate_field_selectors(options)
|
64
|
+
default = Lyft.config.default_profile_fields || {}
|
65
|
+
fields = options.delete(:fields) || default
|
66
|
+
|
67
|
+
if options.delete(:public)
|
68
|
+
return ":public"
|
69
|
+
elsif fields.empty?
|
70
|
+
return ""
|
71
|
+
else
|
72
|
+
return ":(#{build_fields_params(fields)})"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def build_fields_params(fields)
|
77
|
+
if fields.is_a?(Hash) && !fields.empty?
|
78
|
+
fields.map {|i,v| "#{i}:(#{build_fields_params(v)})" }.join(',')
|
79
|
+
elsif fields.respond_to?(:each)
|
80
|
+
fields.map {|field| build_fields_params(field) }.join(',')
|
81
|
+
else
|
82
|
+
fields.to_s.gsub("_", "-")
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
data/lib/lyft/client.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module Lyft
|
2
|
+
class Client < NetHTTPClient
|
3
|
+
def initialize(options={})
|
4
|
+
[ :api_key, :api_secret ].each do |opt|
|
5
|
+
raise unless options.has_key? opt
|
6
|
+
end
|
7
|
+
@api_key = options[:api_key]
|
8
|
+
@api_secret = options[:api_secret]
|
9
|
+
@api_uri = URI.parse(options[:api_url] || Lyft::BASE_URL)
|
10
|
+
super(@api_uri, options)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
class OAuthClient < NetHTTPClient
|
14
|
+
attr_accessor :access_token, :refresh_token
|
15
|
+
def initialize(options={})
|
16
|
+
raise unless options.has_key? :access_token
|
17
|
+
@access_token = options[:access_token]
|
18
|
+
@oauth_uri = URI.parse(options[:api_url] || Lyft::BASE_URL)
|
19
|
+
super(@oauth_uri, options)
|
20
|
+
end
|
21
|
+
|
22
|
+
def auth_headers(method, path, body)
|
23
|
+
{ 'Authorization' => "Bearer #{@access_token}"}
|
24
|
+
end
|
25
|
+
|
26
|
+
def authorize!(redirect_url, params = {})
|
27
|
+
raise NotImplementedError
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Lyft
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :api,
|
4
|
+
:site,
|
5
|
+
:scope,
|
6
|
+
:client_id,
|
7
|
+
:token_url,
|
8
|
+
:api_version,
|
9
|
+
:redirect_uri,
|
10
|
+
:authorize_url,
|
11
|
+
:client_secret,
|
12
|
+
:default_profile_fields
|
13
|
+
|
14
|
+
alias_method :api_key, :client_id
|
15
|
+
alias_method :secret_key, :client_secret
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@api = "https://api.lyft.com"
|
19
|
+
@api_version = "/v1"
|
20
|
+
@site = "https://api.lyft.com"
|
21
|
+
@token_url = "/oauth/token"
|
22
|
+
@authorize_url = "/oauth/authorize"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Lyft
|
2
|
+
# Used to perform requests against Lyft's API.
|
3
|
+
class Connection < ::Faraday::Connection
|
4
|
+
|
5
|
+
def initialize(url=nil, options=nil, &block)
|
6
|
+
|
7
|
+
if url.is_a? Hash
|
8
|
+
options = url
|
9
|
+
url = options[:url]
|
10
|
+
end
|
11
|
+
|
12
|
+
url = default_url if url.nil?
|
13
|
+
|
14
|
+
super url, options, &block
|
15
|
+
|
16
|
+
# We need to use the FlatParamsEncoder so we can pass multiple of
|
17
|
+
# the same param to certain endpoints (like the search API).
|
18
|
+
self.options.params_encoder = ::Faraday::FlatParamsEncoder
|
19
|
+
|
20
|
+
self.response :lyft_raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
private ##############################################################
|
25
|
+
|
26
|
+
|
27
|
+
def default_url
|
28
|
+
Lyft.config.api + Lyft.config.api_version
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/lyft/errors.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
module Lyft
|
2
|
+
|
3
|
+
# Raised when users call a deprecated function
|
4
|
+
class Deprecated < StandardError; end
|
5
|
+
|
6
|
+
# Raised when we know requests will be malformed
|
7
|
+
class InvalidRequest < StandardError; end
|
8
|
+
|
9
|
+
# Raised when we get a throttle error from the API
|
10
|
+
class ThrottleError < StandardError; end
|
11
|
+
|
12
|
+
# Raised when Lyft returns a non 400+ status code during an OAuth
|
13
|
+
# request.
|
14
|
+
class OAuthError < OAuth2::Error; end
|
15
|
+
|
16
|
+
# Raised when Lyft returns a non 400+ status code during an API
|
17
|
+
# request
|
18
|
+
class APIError < OAuth2::Error; end
|
19
|
+
|
20
|
+
module ErrorMessages
|
21
|
+
class << self
|
22
|
+
attr_reader :deprecated,
|
23
|
+
:redirect_uri,
|
24
|
+
:no_auth_code,
|
25
|
+
:no_access_token,
|
26
|
+
:credentials_missing,
|
27
|
+
:redirect_uri_mismatch,
|
28
|
+
:throttled
|
29
|
+
end
|
30
|
+
|
31
|
+
@deprecated = "This has been deprecated by Lyft. Check https://developer.lyft.com/docs to see the latest available API calls"
|
32
|
+
|
33
|
+
@redirect_uri = "You must provide a redirect_uri. Set it in Lyft.configure or pass it in as the redirect_uri option. It must exactly match the redirect_uri you set on your application's settings page on Lyft's website."
|
34
|
+
|
35
|
+
@no_auth_code = "You must provide the authorization code passed to your redirect uri in the url params"
|
36
|
+
|
37
|
+
@no_access_token = "You must have an access token to use Lyft's API. Use the Lyft::OAuth2 module to obtain an access token"
|
38
|
+
|
39
|
+
@credentials_missing = "Client credentials do not exist. Please either pass your client_id and client_secret to the Lyft::Oauth.new constructor or set them via Lyft.configure"
|
40
|
+
|
41
|
+
@redirect_uri_mismatch = "Throttle limit for calls to this resource is reached"
|
42
|
+
|
43
|
+
def klass
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
data/lib/lyft/mash.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
module Lyft
|
2
|
+
# Coerces Lyft JSON to a nice Ruby hash
|
3
|
+
# Lyft::Mash inherits from Hashie::Mash
|
4
|
+
class Mash < ::Hashie::Mash
|
5
|
+
|
6
|
+
# a simple helper to convert a json string to a Mash
|
7
|
+
def self.from_json(json_string)
|
8
|
+
result_hash = JSON.load(json_string)
|
9
|
+
new(result_hash)
|
10
|
+
end
|
11
|
+
|
12
|
+
# returns a Date if we have year, month and day, and no conflicting key
|
13
|
+
def to_date
|
14
|
+
if !self.has_key?('to_date') && contains_date_fields?
|
15
|
+
Date.civil(self.year, self.month, self.day)
|
16
|
+
else
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def timestamp
|
22
|
+
value = self['timestamp']
|
23
|
+
if value.kind_of? Integer
|
24
|
+
value = value / 1000 if value > 9999999999
|
25
|
+
Time.at(value)
|
26
|
+
else
|
27
|
+
value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
protected ############################################################
|
33
|
+
|
34
|
+
|
35
|
+
def contains_date_fields?
|
36
|
+
self.year? && self.month? && self.day?
|
37
|
+
end
|
38
|
+
|
39
|
+
# overload the convert_key mash method so that the Lyft
|
40
|
+
# keys are made a little more ruby-ish
|
41
|
+
def convert_key(key)
|
42
|
+
case key.to_s
|
43
|
+
when '_key'
|
44
|
+
'id'
|
45
|
+
when '_total'
|
46
|
+
'total'
|
47
|
+
when 'values'
|
48
|
+
'all'
|
49
|
+
when 'numResults'
|
50
|
+
'total_results'
|
51
|
+
else
|
52
|
+
underscore(key)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# borrowed from ActiveSupport
|
57
|
+
# no need require an entire lib when we only need one method
|
58
|
+
def underscore(camel_cased_word)
|
59
|
+
word = camel_cased_word.to_s.dup
|
60
|
+
word.gsub!(/::/, '/')
|
61
|
+
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
62
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
63
|
+
word.tr!("-", "_")
|
64
|
+
word.downcase!
|
65
|
+
word
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/lib/lyft/oauth2.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
module Lyft
|
2
|
+
class OAuth2 < ::OAuth2::Client
|
3
|
+
attr_accessor :access_token
|
4
|
+
|
5
|
+
def initialize(client_id=Lyft.config.client_id,
|
6
|
+
client_secret=Lyft.config.client_secret,
|
7
|
+
options = {}, &block)
|
8
|
+
|
9
|
+
if client_id.is_a? Hash
|
10
|
+
options = client_id
|
11
|
+
client_id = Lyft.config.client_id
|
12
|
+
end
|
13
|
+
|
14
|
+
options = default_oauth_options(options)
|
15
|
+
|
16
|
+
super client_id, client_secret, options, &block
|
17
|
+
|
18
|
+
@redirect_uri = options[:redirect_uri]
|
19
|
+
|
20
|
+
if self.options[:raise_errors]
|
21
|
+
check_credentials!(client_id, client_secret)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def auth_code_url(options={})
|
26
|
+
options = default_auth_code_url_options(options)
|
27
|
+
|
28
|
+
if self.options[:raise_errors]
|
29
|
+
check_redirect_uri!(options)
|
30
|
+
end
|
31
|
+
|
32
|
+
@redirect_uri = options[:redirect_uri]
|
33
|
+
|
34
|
+
self.auth_code.authorize_url(options)
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_access_token(code=nil, options={})
|
38
|
+
check_for_code!(code)
|
39
|
+
options = default_access_code_options(options)
|
40
|
+
|
41
|
+
if self.options[:raise_errors]
|
42
|
+
check_access_code_url!(options)
|
43
|
+
end
|
44
|
+
|
45
|
+
tok = self.auth_code.get_token(code, options)
|
46
|
+
self.access_token = Lyft::AccessToken.new(tok.token,
|
47
|
+
tok.expires_in,
|
48
|
+
tok.expires_at)
|
49
|
+
return self.access_token
|
50
|
+
rescue ::OAuth2::Error => e
|
51
|
+
raise OAuthError.new(e.response)
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def default_access_code_options(custom_options={})
|
57
|
+
custom_options ||= {}
|
58
|
+
options = {raise_errors: true}
|
59
|
+
|
60
|
+
@redirect_uri = Lyft.config.redirect_uri if @redirect_uri.nil?
|
61
|
+
options[:redirect_uri] = @redirect_uri
|
62
|
+
|
63
|
+
options = options.merge custom_options
|
64
|
+
return options
|
65
|
+
end
|
66
|
+
|
67
|
+
def default_auth_code_url_options(custom_options={})
|
68
|
+
custom_options ||= {}
|
69
|
+
options = {raise_errors: true}
|
70
|
+
|
71
|
+
if not Lyft.config.redirect_uri.nil?
|
72
|
+
options[:redirect_uri] = Lyft.config.redirect_uri
|
73
|
+
end
|
74
|
+
if not Lyft.config.scope.nil?
|
75
|
+
options[:scope] = Lyft.config.scope
|
76
|
+
end
|
77
|
+
|
78
|
+
options = options.merge custom_options
|
79
|
+
|
80
|
+
if options[:state].nil?
|
81
|
+
options[:state] = generate_csrf_token
|
82
|
+
end
|
83
|
+
|
84
|
+
return options
|
85
|
+
end
|
86
|
+
|
87
|
+
def generate_csrf_token
|
88
|
+
SecureRandom.base64(32)
|
89
|
+
end
|
90
|
+
|
91
|
+
def check_access_code_url!(options={})
|
92
|
+
check_redirect_uri!(options)
|
93
|
+
if options[:redirect_uri] != @redirect_uri
|
94
|
+
raise redirect_uri_mismatch
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def check_for_code!(code)
|
99
|
+
if code.nil?
|
100
|
+
msg = ErrorMessages.no_auth_code
|
101
|
+
raise InvalidRequest.new(msg)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def check_redirect_uri!(options={})
|
106
|
+
if options[:redirect_uri].nil?
|
107
|
+
raise redirect_uri_error
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def default_oauth_options(custom_options={})
|
112
|
+
custom_options ||= {}
|
113
|
+
options = {}
|
114
|
+
options[:site] = Lyft.config.site
|
115
|
+
options[:token_url] = Lyft.config.token_url
|
116
|
+
options[:authorize_url] = Lyft.config.authorize_url
|
117
|
+
return options.merge custom_options
|
118
|
+
end
|
119
|
+
|
120
|
+
def check_credentials!(client_id, client_secret)
|
121
|
+
if client_id.nil? or client_secret.nil?
|
122
|
+
raise credential_error
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def redirect_uri_error
|
127
|
+
InvalidRequest.new ErrorMessages.redirect_uri
|
128
|
+
end
|
129
|
+
|
130
|
+
def credential_error
|
131
|
+
InvalidRequest.new ErrorMessages.credentials_missing
|
132
|
+
end
|
133
|
+
|
134
|
+
def redirect_uri_mismatch
|
135
|
+
InvalidRequest.new ErrorMessages.redirect_uri_mismatch
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Lyft
|
4
|
+
class RaiseError < Faraday::Response::RaiseError
|
5
|
+
def on_complete(response)
|
6
|
+
status_code = response.status.to_i
|
7
|
+
if status_code == 403 && response.body =~ /throttle/i
|
8
|
+
raise Lyft::ThrottleError
|
9
|
+
else
|
10
|
+
super
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
Faraday::Response.register_middleware :lyft_raise_error => Lyft::RaiseError
|
data/lib/lyft/rides.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module Lyft
|
2
|
+
# Rides API
|
3
|
+
class Rides < APIResource
|
4
|
+
def rides(options = {})
|
5
|
+
path = rides_path
|
6
|
+
get(path, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
private ##############################################################
|
10
|
+
|
11
|
+
def rides_path
|
12
|
+
path = "/rides"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/lyft/version.rb
ADDED
data/lyft.gemspec
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lyft/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "lyft"
|
8
|
+
spec.version = Lyft::VERSION
|
9
|
+
spec.authors = ["Ivan Santos"]
|
10
|
+
spec.email = ["pragmaticivan@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{API wrapper for the Lyft API}
|
13
|
+
spec.description = spec.summary
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.required_ruby_version = '>= 1.9.3'
|
23
|
+
spec.add_dependency "oauth2", "~> 1.0"
|
24
|
+
spec.add_dependency "hashie", "~> 3.2"
|
25
|
+
spec.add_dependency "faraday", "~> 0.9"
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
spec.add_development_dependency "awesome_print"
|
30
|
+
|
31
|
+
# We use YARD for documentation
|
32
|
+
# Extra gems for GitHub flavored MarkDown in YARD
|
33
|
+
spec.add_development_dependency "yard"
|
34
|
+
spec.add_development_dependency "redcarpet"
|
35
|
+
spec.add_development_dependency "github-markdown"
|
36
|
+
|
37
|
+
# We use VCR to mock API calls
|
38
|
+
spec.add_development_dependency "vcr"
|
39
|
+
spec.add_development_dependency "webmock"
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,236 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lyft
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ivan Santos
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: oauth2
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: hashie
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: faraday
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.9'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.11'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.11'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: awesome_print
|
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: yard
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: redcarpet
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: github-markdown
|
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: vcr
|
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'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: webmock
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
181
|
+
description: API wrapper for the Lyft API
|
182
|
+
email:
|
183
|
+
- pragmaticivan@gmail.com
|
184
|
+
executables: []
|
185
|
+
extensions: []
|
186
|
+
extra_rdoc_files: []
|
187
|
+
files:
|
188
|
+
- ".gitignore"
|
189
|
+
- ".rspec"
|
190
|
+
- ".travis.yml"
|
191
|
+
- Gemfile
|
192
|
+
- LICENSE.txt
|
193
|
+
- README.md
|
194
|
+
- Rakefile
|
195
|
+
- bin/console
|
196
|
+
- bin/setup
|
197
|
+
- lib/lyft.rb
|
198
|
+
- lib/lyft/access_token.rb
|
199
|
+
- lib/lyft/api.rb
|
200
|
+
- lib/lyft/api_resource.rb
|
201
|
+
- lib/lyft/client.rb
|
202
|
+
- lib/lyft/configuration.rb
|
203
|
+
- lib/lyft/connection.rb
|
204
|
+
- lib/lyft/errors.rb
|
205
|
+
- lib/lyft/mash.rb
|
206
|
+
- lib/lyft/oauth2.rb
|
207
|
+
- lib/lyft/raise_error.rb
|
208
|
+
- lib/lyft/rides.rb
|
209
|
+
- lib/lyft/version.rb
|
210
|
+
- lyft.gemspec
|
211
|
+
homepage: ''
|
212
|
+
licenses:
|
213
|
+
- MIT
|
214
|
+
metadata: {}
|
215
|
+
post_install_message:
|
216
|
+
rdoc_options: []
|
217
|
+
require_paths:
|
218
|
+
- lib
|
219
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
220
|
+
requirements:
|
221
|
+
- - ">="
|
222
|
+
- !ruby/object:Gem::Version
|
223
|
+
version: 1.9.3
|
224
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
225
|
+
requirements:
|
226
|
+
- - ">="
|
227
|
+
- !ruby/object:Gem::Version
|
228
|
+
version: '0'
|
229
|
+
requirements: []
|
230
|
+
rubyforge_project:
|
231
|
+
rubygems_version: 2.4.5.1
|
232
|
+
signing_key:
|
233
|
+
specification_version: 4
|
234
|
+
summary: API wrapper for the Lyft API
|
235
|
+
test_files: []
|
236
|
+
has_rdoc:
|