uber-ruby 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +20 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/lib/uber.rb +7 -0
- data/lib/uber/activities.rb +14 -0
- data/lib/uber/activity.rb +25 -0
- data/lib/uber/api.rb +16 -0
- data/lib/uber/arguments.rb +13 -0
- data/lib/uber/base.rb +27 -0
- data/lib/uber/client.rb +144 -0
- data/lib/uber/error.rb +110 -0
- data/lib/uber/me.rb +14 -0
- data/lib/uber/parse_json.rb +29 -0
- data/lib/uber/price.rb +5 -0
- data/lib/uber/price_estimates.rb +14 -0
- data/lib/uber/product.rb +5 -0
- data/lib/uber/products.rb +14 -0
- data/lib/uber/rate_limit.rb +26 -0
- data/lib/uber/request.rb +38 -0
- data/lib/uber/time.rb +5 -0
- data/lib/uber/time_estimates.rb +14 -0
- data/lib/uber/token.rb +15 -0
- data/lib/uber/user.rb +5 -0
- data/lib/uber/utils.rb +24 -0
- data/lib/uber/version.rb +14 -0
- data/uber-ruby.gemspec +32 -0
- metadata +192 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2324fd9c73d2168231140fe965422216613fc11d
|
4
|
+
data.tar.gz: a385f12ffa2b11f1be8f2af001bd1a1851e282d2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cd50721c71e64aa9b3585a0eaa4e3e3b29b70ad963f94bae2b0003ddda39b7452b6ff455ea80df814a6f9bd520ebec368c7410a666729423f5b55f9e45f461d2
|
7
|
+
data.tar.gz: 1ff97c56bcaa40777f5db89bc04350557bbe996cf81e405c1b59ff5df7ecbad76026bef798ce9e5ba9d7dd094ec8606ce960f484756605478ad51efc53608781
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 Dingding Ye
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# The Uber Ruby Gem
|
2
|
+
|
3
|
+
A Ruby interface to the Uber API.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'uber-ruby', require: 'uber'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install uber-ruby
|
18
|
+
|
19
|
+
## Configuration
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
client = Uber::Client.new do |config|
|
23
|
+
config.server_token = "YOUR_SERVER_TOKEN"
|
24
|
+
config.client_id = "YOUR_CLIENT_ID"
|
25
|
+
config.client_secret = "YOUR_CLIENT_SECRET"
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
TODO: Write usage instructions here
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( http://github.com/<my-github-username>/uber-ruby/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/uber.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'uber/arguments'
|
2
|
+
require 'uber/request'
|
3
|
+
require 'uber/activity'
|
4
|
+
|
5
|
+
module Uber
|
6
|
+
module API
|
7
|
+
module Activities
|
8
|
+
def history(*args)
|
9
|
+
arguments = Uber::Arguments.new(args)
|
10
|
+
perform_with_object(:get, "/v1/history", arguments.options, Activity)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Uber
|
2
|
+
class Activity < Base
|
3
|
+
attr_accessor :offset, :limit, :count, :history
|
4
|
+
|
5
|
+
def history=(value)
|
6
|
+
@history = History.new(value)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class History < Base
|
11
|
+
attr_accessor :uuid, :request_time, :product_id, :status, :distance, :start_time, :end_time, :start_location, :end_location
|
12
|
+
|
13
|
+
def start_location=(value)
|
14
|
+
@start_location = Location.new(value)
|
15
|
+
end
|
16
|
+
|
17
|
+
def end_location=(value)
|
18
|
+
@end_location = Location.new(value)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Location < Base
|
23
|
+
attr_accessor :address, :latitude, :longitude
|
24
|
+
end
|
25
|
+
end
|
data/lib/uber/api.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'uber/products'
|
2
|
+
require 'uber/price_estimates'
|
3
|
+
require 'uber/time_estimates'
|
4
|
+
require 'uber/activities'
|
5
|
+
require 'uber/me'
|
6
|
+
|
7
|
+
module Uber
|
8
|
+
module API
|
9
|
+
include Uber::Utils
|
10
|
+
include Uber::API::Products
|
11
|
+
include Uber::API::PriceEstimates
|
12
|
+
include Uber::API::TimeEstimates
|
13
|
+
include Uber::API::Activities
|
14
|
+
include Uber::API::Me
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Uber
|
2
|
+
class Arguments < Array
|
3
|
+
attr_reader :options
|
4
|
+
|
5
|
+
# Initializes a new Arguments object
|
6
|
+
#
|
7
|
+
# @return [Uber::Arguments]
|
8
|
+
def initialize(args)
|
9
|
+
@options = args.last.is_a?(::Hash) ? args.pop : {}
|
10
|
+
super(args.flatten)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/uber/base.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module Uber
|
2
|
+
class Base
|
3
|
+
attr_reader :attrs
|
4
|
+
alias_method :to_h, :attrs
|
5
|
+
|
6
|
+
# Initializes a new object
|
7
|
+
#
|
8
|
+
# @param attrs [Hash]
|
9
|
+
# @return [Uber::Base]
|
10
|
+
def initialize(attrs = {})
|
11
|
+
attrs.each do |key, value|
|
12
|
+
if respond_to?(:"#{key}=")
|
13
|
+
send(:"#{key}=", value)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Fetches an attribute of an object using hash notation
|
19
|
+
#
|
20
|
+
# @param method [String, Symbol] Message to send to the object
|
21
|
+
def [](method)
|
22
|
+
send(method.to_sym)
|
23
|
+
rescue NoMethodError
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/uber/client.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
require 'uber/version'
|
2
|
+
require 'uber/error'
|
3
|
+
require 'base64'
|
4
|
+
require 'faraday'
|
5
|
+
require 'faraday/request/multipart'
|
6
|
+
require 'uber/parse_json'
|
7
|
+
|
8
|
+
module Uber
|
9
|
+
class Client
|
10
|
+
include Uber::API
|
11
|
+
|
12
|
+
attr_accessor :server_token, :client_id, :client_secret
|
13
|
+
attr_accessor :bearer_token
|
14
|
+
attr_writer :connection_options, :middleware
|
15
|
+
ENDPOINT = 'https://api.uber.com'
|
16
|
+
|
17
|
+
def initialize(options = {})
|
18
|
+
options.each do |key, value|
|
19
|
+
send(:"#{key}=", value)
|
20
|
+
end
|
21
|
+
yield(self) if block_given?
|
22
|
+
validate_credential_type!
|
23
|
+
end
|
24
|
+
|
25
|
+
def connection_options
|
26
|
+
@connection_options ||= {
|
27
|
+
:builder => middleware,
|
28
|
+
:headers => {
|
29
|
+
:accept => 'application/json',
|
30
|
+
:user_agent => user_agent,
|
31
|
+
},
|
32
|
+
:request => {
|
33
|
+
:open_timeout => 10,
|
34
|
+
:timeout => 30,
|
35
|
+
},
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [Boolean]
|
40
|
+
def user_token?
|
41
|
+
!!(client_id && client_secret)
|
42
|
+
end
|
43
|
+
|
44
|
+
# @return [String]
|
45
|
+
def user_agent
|
46
|
+
@user_agent ||= "Uber Ruby Gem #{Uber::Version}"
|
47
|
+
end
|
48
|
+
|
49
|
+
def middleware
|
50
|
+
@middleware ||= Faraday::RackBuilder.new do |faraday|
|
51
|
+
# Encodes as "application/x-www-form-urlencoded" if not already encoded
|
52
|
+
faraday.request :url_encoded
|
53
|
+
# Handle error responses
|
54
|
+
faraday.response :raise_error
|
55
|
+
# Parse JSON response bodies
|
56
|
+
faraday.response :parse_json
|
57
|
+
# Set default HTTP adapter
|
58
|
+
faraday.adapter :net_http
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Perform an HTTP GET request
|
63
|
+
def get(path, params = {})
|
64
|
+
headers = request_headers(:get, path, params)
|
65
|
+
request(:get, path, params, headers)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Perform an HTTP POST request
|
69
|
+
def post(path, params = {})
|
70
|
+
headers = params.values.any? { |value| value.respond_to?(:to_io) } ? request_headers(:post, path, params, {}) : request_headers(:post, path, params)
|
71
|
+
request(:post, path, params, headers)
|
72
|
+
end
|
73
|
+
|
74
|
+
# @return [Boolean]
|
75
|
+
def bearer_token?
|
76
|
+
!!bearer_token
|
77
|
+
end
|
78
|
+
|
79
|
+
# @return [Hash]
|
80
|
+
def credentials
|
81
|
+
{
|
82
|
+
server_token: server_token,
|
83
|
+
client_id: client_id,
|
84
|
+
client_secret: client_secret
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
# @return [Boolean]
|
89
|
+
def credentials?
|
90
|
+
credentials.values.all?
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
# Ensures that all credentials set during configuration are
|
96
|
+
# of a valid type. Valid types are String and Symbol.
|
97
|
+
#
|
98
|
+
# @raise [Uber::Error::ConfigurationError] Error is raised when
|
99
|
+
# supplied uber credentials are not a String or Symbol.
|
100
|
+
def validate_credential_type!
|
101
|
+
credentials.each do |credential, value|
|
102
|
+
next if value.nil?
|
103
|
+
fail(Uber::Error::ConfigurationError.new("Invalid #{credential} specified: #{value.inspect} must be a string or symbol.")) unless value.is_a?(String) || value.is_a?(Symbol)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Returns a Faraday::Connection object
|
108
|
+
#
|
109
|
+
# @return [Faraday::Connection]
|
110
|
+
def connection
|
111
|
+
@connection ||= Faraday.new(ENDPOINT, connection_options)
|
112
|
+
end
|
113
|
+
|
114
|
+
def request(method, path, params = {}, headers = {})
|
115
|
+
connection.send(method.to_sym, path, params) { |request| request.headers.update(headers) }.env
|
116
|
+
rescue Faraday::Error::TimeoutError, Timeout::Error => error
|
117
|
+
raise(Uber::Error::RequestTimeout.new(error))
|
118
|
+
rescue Faraday::Error::ClientError, JSON::ParserError => error
|
119
|
+
fail(Uber::Error.new(error))
|
120
|
+
end
|
121
|
+
|
122
|
+
def request_headers(method, path, params = {}, signature_params = params)
|
123
|
+
bearer_token_request = params.delete(:bearer_token_request)
|
124
|
+
headers = {}
|
125
|
+
headers[:accept] = '*/*'
|
126
|
+
headers[:content_type] = 'application/x-www-form-urlencoded; charset=UTF-8'
|
127
|
+
if bearer_token_request
|
128
|
+
headers[:authorization] = bearer_auth_header
|
129
|
+
else
|
130
|
+
headers[:authorization] = server_auth_header
|
131
|
+
end
|
132
|
+
headers
|
133
|
+
end
|
134
|
+
|
135
|
+
def bearer_auth_header
|
136
|
+
token = bearer_token.is_a?(Uber::Token) && bearer_token.bearer? ? bearer_token.access_token : bearer_token
|
137
|
+
"Bearer #{token}"
|
138
|
+
end
|
139
|
+
|
140
|
+
def server_auth_header
|
141
|
+
"Token #{@server_token}"
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
data/lib/uber/error.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'uber/rate_limit'
|
2
|
+
|
3
|
+
module Uber
|
4
|
+
# Custom error class for rescuing from all Uber errors
|
5
|
+
class Error < StandardError
|
6
|
+
attr_reader :code, :rate_limit
|
7
|
+
|
8
|
+
module Code
|
9
|
+
AUTHENTICATION_PROBLEM = 32
|
10
|
+
MALFORMED_REQUEST = 400
|
11
|
+
UNAUTHORIZED_REQUEST = 401
|
12
|
+
REQUEST_FORBIDDEN = 403
|
13
|
+
RESOURCE_NOT_FOUND = 404
|
14
|
+
UNACCEPTABLE_CONTENT_TYPE = 406
|
15
|
+
INVALID_REQUEST = 422
|
16
|
+
RATE_LIMIT_EXCEEDED = 429
|
17
|
+
INTERVAL_ERROR = 500
|
18
|
+
end
|
19
|
+
Codes = Code # rubocop:disable ConstantName
|
20
|
+
|
21
|
+
class << self
|
22
|
+
# Create a new error from an HTTP response
|
23
|
+
#
|
24
|
+
# @param response [Faraday::Response]
|
25
|
+
# @return [Uber::Error]
|
26
|
+
def from_response(response)
|
27
|
+
message, code = parse_error(response.body)
|
28
|
+
new(message, response.response_headers, code)
|
29
|
+
end
|
30
|
+
|
31
|
+
# @return [Hash]
|
32
|
+
def errors
|
33
|
+
@errors ||= {
|
34
|
+
400 => Uber::Error::BadRequest,
|
35
|
+
401 => Uber::Error::Unauthorized,
|
36
|
+
403 => Uber::Error::Forbidden,
|
37
|
+
404 => Uber::Error::NotFound,
|
38
|
+
406 => Uber::Error::NotAcceptable,
|
39
|
+
422 => Uber::Error::UnprocessableEntity,
|
40
|
+
429 => Uber::Error::RateLimited,
|
41
|
+
500 => Uber::Error::InternalServerError
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def parse_error(body)
|
48
|
+
if body.nil?
|
49
|
+
['', nil]
|
50
|
+
elsif body[:error]
|
51
|
+
[body[:error], nil]
|
52
|
+
elsif body[:errors]
|
53
|
+
extract_message_from_errors(body)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def extract_message_from_errors(body)
|
58
|
+
first = Array(body[:errors]).first
|
59
|
+
if first.is_a?(Hash)
|
60
|
+
[first[:message].chomp, first[:code]]
|
61
|
+
else
|
62
|
+
[first.chomp, nil]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Initializes a new Error object
|
68
|
+
#
|
69
|
+
# @param exception [Exception, String]
|
70
|
+
# @param rate_limit [Hash]
|
71
|
+
# @param code [Integer]
|
72
|
+
# @return [Uber::Error]
|
73
|
+
def initialize(message = '', rate_limit = {}, code = nil)
|
74
|
+
super(message)
|
75
|
+
@rate_limit = Uber::RateLimit.new(rate_limit)
|
76
|
+
@code = code
|
77
|
+
end
|
78
|
+
|
79
|
+
class ClientError < self; end
|
80
|
+
|
81
|
+
class ConfigurationError < ::ArgumentError; end
|
82
|
+
|
83
|
+
# Raised when Uber returns the HTTP status code 400
|
84
|
+
class BadRequest < ClientError; end
|
85
|
+
|
86
|
+
# Raised when Uber returns the HTTP status code 401
|
87
|
+
class Unauthorized < ClientError; end
|
88
|
+
|
89
|
+
# Raised when Uber returns the HTTP status code 403
|
90
|
+
class Forbidden < ClientError; end
|
91
|
+
|
92
|
+
# Raised when Uber returns the HTTP status code 404
|
93
|
+
class NotFound < ClientError; end
|
94
|
+
|
95
|
+
# Raised when Uber returns the HTTP status code 406
|
96
|
+
class NotAcceptable < ClientError; end
|
97
|
+
|
98
|
+
# Raised when Uber returns the HTTP status code 422
|
99
|
+
class UnprocessableEntity < ClientError; end
|
100
|
+
|
101
|
+
# Raised when Uber returns the HTTP status code 429
|
102
|
+
class RateLimited < ClientError; end
|
103
|
+
|
104
|
+
# Raised when Uber returns a 5xx HTTP status code
|
105
|
+
class ServerError < self; end
|
106
|
+
|
107
|
+
# Raised when Uber returns the HTTP status code 500
|
108
|
+
class InternalServerError < ServerError; end
|
109
|
+
end
|
110
|
+
end
|
data/lib/uber/me.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'uber/arguments'
|
2
|
+
require 'uber/request'
|
3
|
+
require 'uber/user'
|
4
|
+
|
5
|
+
module Uber
|
6
|
+
module API
|
7
|
+
module Me
|
8
|
+
def me(*args)
|
9
|
+
arguments = Uber::Arguments.new(args)
|
10
|
+
perform_with_object(:get, "/v1/me", arguments.options, User)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Uber
|
5
|
+
module Response
|
6
|
+
class ParseJson < Faraday::Response::Middleware
|
7
|
+
WHITESPACE_REGEX = /\A^\s*$\z/
|
8
|
+
|
9
|
+
def parse(body)
|
10
|
+
case body
|
11
|
+
when WHITESPACE_REGEX, nil
|
12
|
+
nil
|
13
|
+
else
|
14
|
+
JSON.parse(body, :symbolize_names => true)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def on_complete(response)
|
19
|
+
response.body = parse(response.body) if respond_to?(:parse) && !unparsable_status_codes.include?(response.status)
|
20
|
+
end
|
21
|
+
|
22
|
+
def unparsable_status_codes
|
23
|
+
[204, 301, 302, 304]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
Faraday::Response.register_middleware :parse_json => Uber::Response::ParseJson
|
data/lib/uber/price.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'uber/arguments'
|
2
|
+
require 'uber/request'
|
3
|
+
require 'uber/price'
|
4
|
+
|
5
|
+
module Uber
|
6
|
+
module API
|
7
|
+
module PriceEstimates
|
8
|
+
def price_estimations(*args)
|
9
|
+
arguments = Uber::Arguments.new(args)
|
10
|
+
perform_with_objects(:get, "/v1/estimates/price", arguments.options, Price)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/uber/product.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'uber/arguments'
|
2
|
+
require 'uber/request'
|
3
|
+
require 'uber/product'
|
4
|
+
|
5
|
+
module Uber
|
6
|
+
module API
|
7
|
+
module Products
|
8
|
+
def products(*args)
|
9
|
+
arguments = Uber::Arguments.new(args)
|
10
|
+
perform_with_objects(:get, "/v1/products", arguments.options, Product)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Uber
|
2
|
+
class RateLimit < Uber::Base
|
3
|
+
# @return [Integer]
|
4
|
+
def limit
|
5
|
+
limit = @attrs['x-rate-limit-limit']
|
6
|
+
limit.to_i if limit
|
7
|
+
end
|
8
|
+
|
9
|
+
# @return [Integer]
|
10
|
+
def remaining
|
11
|
+
remaining = @attrs['x-rate-limit-remaining']
|
12
|
+
remaining.to_i if remaining
|
13
|
+
end
|
14
|
+
|
15
|
+
# @return [Time]
|
16
|
+
def reset_at
|
17
|
+
reset = @attrs['x-rate-limit-reset']
|
18
|
+
Time.at(reset.to_i) if reset
|
19
|
+
end
|
20
|
+
|
21
|
+
# @return [Integer]
|
22
|
+
def reset_in
|
23
|
+
[(reset_at - Time.now).ceil, 0].max if reset_at
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/uber/request.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
module Uber
|
2
|
+
class Request
|
3
|
+
attr_accessor :client, :request_method, :path, :options
|
4
|
+
alias_method :verb, :request_method
|
5
|
+
|
6
|
+
# @param client [Uber::Client]
|
7
|
+
# @param request_method [String, Symbol]
|
8
|
+
# @param path [String]
|
9
|
+
# @param options [Hash]
|
10
|
+
# @return [Uber::Request]
|
11
|
+
def initialize(client, request_method, path, options = {})
|
12
|
+
@client = client
|
13
|
+
@request_method = request_method.to_sym
|
14
|
+
@path = path
|
15
|
+
@options = options
|
16
|
+
end
|
17
|
+
|
18
|
+
# @return [Hash]
|
19
|
+
def perform
|
20
|
+
@client.send(@request_method, @path, @options).body
|
21
|
+
end
|
22
|
+
|
23
|
+
# @param klass [Class]
|
24
|
+
# @param request [Uber::Request]
|
25
|
+
# @return [Object]
|
26
|
+
def perform_with_object(klass)
|
27
|
+
klass.new(perform)
|
28
|
+
end
|
29
|
+
|
30
|
+
# @param klass [Class]
|
31
|
+
# @return [Array]
|
32
|
+
def perform_with_objects(klass)
|
33
|
+
perform.values.flatten.collect do |element|
|
34
|
+
klass.new(element)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/uber/time.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'uber/arguments'
|
2
|
+
require 'uber/request'
|
3
|
+
require 'uber/time'
|
4
|
+
|
5
|
+
module Uber
|
6
|
+
module API
|
7
|
+
module TimeEstimates
|
8
|
+
def time_estimations(*args)
|
9
|
+
arguments = Uber::Arguments.new(args)
|
10
|
+
perform_with_objects(:get, "/v1/estimates/time", arguments.options, Time)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/uber/token.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'uber/base'
|
2
|
+
|
3
|
+
module Uber
|
4
|
+
class Token < Uber::Base
|
5
|
+
attr_reader :access_token, :token_type
|
6
|
+
alias_method :to_s, :access_token
|
7
|
+
|
8
|
+
BEARER_TYPE = 'bearer'
|
9
|
+
|
10
|
+
# @return [Boolean]
|
11
|
+
def bearer?
|
12
|
+
@attrs[:token_type] == BEARER_TYPE
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/uber/user.rb
ADDED
data/lib/uber/utils.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'uber/arguments'
|
2
|
+
require 'uber/request'
|
3
|
+
|
4
|
+
module Uber
|
5
|
+
module Utils
|
6
|
+
# @param request_method [Symbol]
|
7
|
+
# @param path [String]
|
8
|
+
# @param options [Hash]
|
9
|
+
# @param klass [Class]
|
10
|
+
def perform_with_object(request_method, path, options, klass)
|
11
|
+
request = Uber::Request.new(self, request_method, path, options)
|
12
|
+
request.perform_with_object(klass)
|
13
|
+
end
|
14
|
+
|
15
|
+
# @param request_method [Symbol]
|
16
|
+
# @param path [String]
|
17
|
+
# @param options [Hash]
|
18
|
+
# @param klass [Class]
|
19
|
+
def perform_with_objects(request_method, path, options, klass)
|
20
|
+
request = Uber::Request.new(self, request_method, path, options)
|
21
|
+
request.perform_with_objects(klass)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/uber/version.rb
ADDED
data/uber-ruby.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'uber/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "uber-ruby"
|
8
|
+
spec.version = Uber::Version
|
9
|
+
spec.authors = ["Dingding Ye"]
|
10
|
+
spec.email = ["yedingding@gmail.com"]
|
11
|
+
spec.summary = %q{The Uber Ruby Gem.}
|
12
|
+
spec.description = %q{A Ruby interface to the Uber API.}
|
13
|
+
spec.homepage = "https://github.com/sishen/uber-ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = %w(LICENSE.txt README.md Rakefile uber-ruby.gemspec)
|
17
|
+
spec.files += Dir.glob('lib/**/*.rb')
|
18
|
+
spec.files += Dir.glob('test/**/*')
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = Dir.glob('test/**/*')
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
spec.required_rubygems_version = '>= 1.3.5'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
25
|
+
spec.add_development_dependency "rake", '~> 0'
|
26
|
+
|
27
|
+
spec.add_dependency 'oauth2', '~> 1.0.0', '>= 1.0.0'
|
28
|
+
spec.add_dependency 'faraday', '~> 0.9.0', '>= 0.9.0'
|
29
|
+
spec.add_dependency 'http', '~> 0.5.0', '>= 0.5.0'
|
30
|
+
spec.add_dependency 'http_parser.rb', '~> 0.6.0', '>= 0.6.0'
|
31
|
+
spec.add_dependency 'json', '~> 1.8'
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,192 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uber-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dingding Ye
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: oauth2
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.0.0
|
48
|
+
- - '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 1.0.0
|
51
|
+
type: :runtime
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ~>
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 1.0.0
|
58
|
+
- - '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 1.0.0
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: faraday
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 0.9.0
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 0.9.0
|
71
|
+
type: :runtime
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 0.9.0
|
78
|
+
- - '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 0.9.0
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: http
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ~>
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: 0.5.0
|
88
|
+
- - '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: 0.5.0
|
91
|
+
type: :runtime
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ~>
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: 0.5.0
|
98
|
+
- - '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 0.5.0
|
101
|
+
- !ruby/object:Gem::Dependency
|
102
|
+
name: http_parser.rb
|
103
|
+
requirement: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ~>
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: 0.6.0
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.6.0
|
111
|
+
type: :runtime
|
112
|
+
prerelease: false
|
113
|
+
version_requirements: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.6.0
|
118
|
+
- - '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: 0.6.0
|
121
|
+
- !ruby/object:Gem::Dependency
|
122
|
+
name: json
|
123
|
+
requirement: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ~>
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '1.8'
|
128
|
+
type: :runtime
|
129
|
+
prerelease: false
|
130
|
+
version_requirements: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ~>
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '1.8'
|
135
|
+
description: A Ruby interface to the Uber API.
|
136
|
+
email:
|
137
|
+
- yedingding@gmail.com
|
138
|
+
executables: []
|
139
|
+
extensions: []
|
140
|
+
extra_rdoc_files: []
|
141
|
+
files:
|
142
|
+
- LICENSE.txt
|
143
|
+
- README.md
|
144
|
+
- Rakefile
|
145
|
+
- lib/uber.rb
|
146
|
+
- lib/uber/activities.rb
|
147
|
+
- lib/uber/activity.rb
|
148
|
+
- lib/uber/api.rb
|
149
|
+
- lib/uber/arguments.rb
|
150
|
+
- lib/uber/base.rb
|
151
|
+
- lib/uber/client.rb
|
152
|
+
- lib/uber/error.rb
|
153
|
+
- lib/uber/me.rb
|
154
|
+
- lib/uber/parse_json.rb
|
155
|
+
- lib/uber/price.rb
|
156
|
+
- lib/uber/price_estimates.rb
|
157
|
+
- lib/uber/product.rb
|
158
|
+
- lib/uber/products.rb
|
159
|
+
- lib/uber/rate_limit.rb
|
160
|
+
- lib/uber/request.rb
|
161
|
+
- lib/uber/time.rb
|
162
|
+
- lib/uber/time_estimates.rb
|
163
|
+
- lib/uber/token.rb
|
164
|
+
- lib/uber/user.rb
|
165
|
+
- lib/uber/utils.rb
|
166
|
+
- lib/uber/version.rb
|
167
|
+
- uber-ruby.gemspec
|
168
|
+
homepage: https://github.com/sishen/uber-ruby
|
169
|
+
licenses:
|
170
|
+
- MIT
|
171
|
+
metadata: {}
|
172
|
+
post_install_message:
|
173
|
+
rdoc_options: []
|
174
|
+
require_paths:
|
175
|
+
- lib
|
176
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - '>='
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
181
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
182
|
+
requirements:
|
183
|
+
- - '>='
|
184
|
+
- !ruby/object:Gem::Version
|
185
|
+
version: 1.3.5
|
186
|
+
requirements: []
|
187
|
+
rubyforge_project:
|
188
|
+
rubygems_version: 2.4.1
|
189
|
+
signing_key:
|
190
|
+
specification_version: 4
|
191
|
+
summary: The Uber Ruby Gem.
|
192
|
+
test_files: []
|