taxy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d9d6569b4dfbecbf0a774d58511350ad449775f6
4
+ data.tar.gz: e901392a84e9ff48d429ba078c4de46ad6fdb88b
5
+ SHA512:
6
+ metadata.gz: 3951976f596410b2d6543854d81aebcf2f1d76923c976cc36b7c1d1b0b9cf0f752f3707289be0de9fab961997981953bf23cc73098a48b36f396e28517cc3167
7
+ data.tar.gz: 6406ff3af3b5d9fbbec61fa1e7936792b746c3e80066d1c55563ebd887ac858b94e18c007ea1a9085fdfcd2efc9045a595e96a11485f2df63bb03bbb3774ab3a
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Rafael Gibim
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.
@@ -0,0 +1,63 @@
1
+ # Taxy
2
+
3
+ Simple API wrapper for Cabify public API, built on top of Faraday.
4
+ As of now, a bearer token is required beforehand
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile, then execute `bundle`:
9
+
10
+ ```ruby
11
+ gem 'taxy'
12
+ ```
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install taxy
17
+
18
+ ## Usage
19
+
20
+ ```ruby
21
+ Taxy::Client.new(bearer_token: 123)
22
+ => #<Taxy::Client:0x00000001ebe280 @bearer_token=123>
23
+ ```
24
+
25
+ ```ruby
26
+ taxy = Taxy::Client.new(bearer_token: 123)
27
+ taxy.user # http://developers.cabify.com/#users
28
+ => #<Taxy::User:0x00000001a31500
29
+ @avatar_url="http://example.com/example.jpg",
30
+ @email="johndoe@example.com",
31
+ @locale="pt-BR",
32
+ @mobile_cc="12",
33
+ @mobile_num="123123123",
34
+ @name="John",
35
+ @surname="Doe">
36
+ ```
37
+
38
+ ```ruby
39
+ taxy = Taxy::Client.new(bearer_token: 123) # http://developers.cabify.com/#estimates
40
+ taxy.estimate(starting_latitude: 1, starting_longitude: 1, ending_latitude: 1, ending_longitude: 1)
41
+ => [#<Taxy::Estimation:0x00000002053eb0
42
+ @currency="EUR",
43
+ @currency_symbol="€",
44
+ @price_formatted="12.34€",
45
+ @total_price=1234,
46
+ @vehicle_type=
47
+ #<Taxy::Estimation::Vehicle:0x00000002053de8
48
+ @_id="executive_id",
49
+ @description="A very large vehicle with comfortable seats",
50
+ @eta=#<Taxy::Estimation::Vehicle::Eta:0x00000002053780 @formatted=">2 min", @low_availability=nil, @max=1000, @min=100>,
51
+ @icon="executive",
52
+ @icons=#<Taxy::Estimation::Vehicle::Icons:0x00000002053988 @regular="https://cabify.com/images/icons/vehicle_type/executive_27.png">,
53
+ @name="Executive Class",
54
+ @short_name="Executive">>]
55
+ ```
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Drowze/taxy.
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,23 @@
1
+ require 'virtus'
2
+
3
+ require 'connection'
4
+ require 'client/estimate'
5
+ require 'client/user'
6
+
7
+ require 'models/estimation'
8
+
9
+ module Taxy
10
+ class Client
11
+ include Taxy::Connection
12
+ include Taxy::Client::Estimate
13
+ include Taxy::Client::User
14
+
15
+ attr_accessor :bearer_token, :debug
16
+
17
+ def initialize(options = {})
18
+ options.each do |key, value|
19
+ send(:"#{key}=", value)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,27 @@
1
+ require 'models/estimation'
2
+
3
+ module Taxy
4
+ class Client
5
+ module Estimate
6
+ def estimate(starting_latitude:, starting_longitude:,
7
+ ending_latitude:, ending_longitude:)
8
+
9
+ params = build_params([starting_latitude, starting_longitude], [ending_latitude, ending_longitude])
10
+
11
+ response = post('/api/v2/estimate', params)
12
+ response.map { |est| Taxy::Estimation.new(est) } if @last_response.success?
13
+ end
14
+
15
+ private
16
+
17
+ def build_params(starting_point, ending_point)
18
+ {
19
+ stops: [
20
+ { loc: starting_point },
21
+ { loc: ending_point }
22
+ ]
23
+ }
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,12 @@
1
+ require 'models/user'
2
+
3
+ module Taxy
4
+ class Client
5
+ module User
6
+ def user(params = {})
7
+ response = get('/api/v2/user', params)
8
+ Taxy::User.new(response) if @last_response.success?
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,63 @@
1
+ require 'json'
2
+ require 'faraday'
3
+ require 'response/parse_json'
4
+
5
+ module Taxy
6
+ module Connection
7
+ BASE_URI = 'https://test.cabify.com'.freeze
8
+
9
+ def get(path, params = {})
10
+ request(:get, path, params)
11
+ end
12
+
13
+ def post(path, params = {})
14
+ request(:post, path, params.to_json)
15
+ end
16
+
17
+ def delete(path, params = {})
18
+ request(:delete, path, params)
19
+ end
20
+
21
+ def put(path, params = {})
22
+ request(:put, path, params.to_json)
23
+ end
24
+
25
+ def patch(path, params = {})
26
+ request(:patch, path, params.to_json)
27
+ end
28
+
29
+ private
30
+
31
+ def request(method, path, params = {})
32
+ @last_response = agent.send(method.to_sym, path, params)
33
+ @last_response.body
34
+ end
35
+
36
+ def agent
37
+ @agent ||= Faraday.new(BASE_URI, connection_options) do |http|
38
+ http.headers[:content_type] = 'application/json'.freeze
39
+ http.headers[:user_agent] = 'Cabify API Ruby Wrapper'.freeze
40
+ http.headers[:accept_language] = 'en'.freeze
41
+ http.authorization 'Bearer', @bearer_token if @bearer_token
42
+ end
43
+ end
44
+
45
+ def connection_options
46
+ @connection_options ||= {
47
+ builder: middleware,
48
+ request: {
49
+ open_timeout: 10,
50
+ timeout: 30
51
+ }
52
+ }
53
+ end
54
+
55
+ def middleware
56
+ @middleware ||= Faraday::RackBuilder.new do |faraday|
57
+ faraday.response :parse_json
58
+ faraday.response :logger if debug.to_s.casecmp('true').zero?
59
+ faraday.adapter Faraday.default_adapter
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,13 @@
1
+ require 'models/estimation/vehicle'
2
+
3
+ module Taxy
4
+ class Estimation
5
+ include Virtus.model
6
+
7
+ attribute :vehicle_type, Vehicle
8
+ attribute :total_price, Integer
9
+ attribute :currency, String
10
+ attribute :currency_symbol, String
11
+ attribute :price_formatted, String
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ require 'models/estimation/vehicle/icons'
2
+ require 'models/estimation/vehicle/eta'
3
+
4
+ module Taxy
5
+ class Estimation
6
+ class Vehicle
7
+ include Virtus.model
8
+
9
+ attribute :_id, String
10
+ attribute :name, String
11
+ attribute :short_name, String
12
+ attribute :description, String
13
+ attribute :icon, String
14
+ attribute :icons, Icons
15
+ attribute :eta, Eta
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ module Taxy
2
+ class Estimation
3
+ class Vehicle
4
+ class Eta
5
+ include Virtus.model
6
+
7
+ attribute :min, Integer
8
+ attribute :max, Integer
9
+ attribute :low_availability, Boolean
10
+ attribute :formatted, String
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module Taxy
2
+ class Estimation
3
+ class Vehicle
4
+ class Icons
5
+ include Virtus.model
6
+
7
+ attribute :regular, String
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module Taxy
2
+ class User
3
+ include Virtus.model
4
+
5
+ attribute :avatar_url, String
6
+ attribute :name, String
7
+ attribute :surname, String
8
+ attribute :email, String
9
+ attribute :mobile_cc, String
10
+ attribute :mobile_num, String
11
+ attribute :locale, String
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module Taxy
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
+ return if unparsable_status_codes.include?(response.status)
20
+ response.body = parse(response.body) if respond_to?(:parse)
21
+ end
22
+
23
+ def unparsable_status_codes
24
+ [204, 301, 302, 304]
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ Faraday::Response.register_middleware parse_json: Taxy::Response::ParseJson
@@ -0,0 +1 @@
1
+ require 'client'
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+ require 'pry'
3
+
4
+ describe Taxy::Client::Estimate do
5
+ let(:client) { Taxy::Client.new(bearer_token: 'CABIFY_BEARER_TOKEN') }
6
+ subject { client.estimate(starting_latitude: 1, starting_longitude: 2, ending_latitude: 3, ending_longitude: 4) }
7
+
8
+ describe '#user' do
9
+ context 'when the request is successful' do
10
+ let(:request_params_hash) {
11
+ { stops: [{ loc: [1, 2] }, { loc: [3, 4] }] }
12
+ }
13
+ let(:response_hash) do
14
+ [
15
+ {
16
+ vehicle_type: {
17
+ _id: 'executive_id',
18
+ name: 'Executive Class',
19
+ short_name: 'Executive',
20
+ description: 'A very large vehicle with comfortable seats',
21
+ icon: 'executive',
22
+ icons: {
23
+ regular: 'https://cabify.com/images/icons/vehicle_type/executive_27.png'
24
+ },
25
+ eta: {
26
+ min: 100,
27
+ max: 1000,
28
+ formatted: '>2 min'
29
+ }
30
+ },
31
+ total_price: 1234,
32
+ price_formatted: '12.34€',
33
+ currency: 'EUR',
34
+ currency_symbol: '€',
35
+ }
36
+ ]
37
+ end
38
+
39
+ before do
40
+ stub_cabify_request(
41
+ verb: :post,
42
+ params: request_params_hash,
43
+ endpoint: '/api/v2/estimate',
44
+ status: 200,
45
+ response: response_hash
46
+ )
47
+ end
48
+
49
+ it 'returns an array of Taxy::Estimation instance' do
50
+ expect(subject.first.class).to eq Taxy::Estimation
51
+ expect(subject.first.vehicle_type.class).to eq Taxy::Estimation::Vehicle
52
+ expect(subject.first.vehicle_type.icons.class).to eq Taxy::Estimation::Vehicle::Icons
53
+ expect(subject.first.vehicle_type.eta.class).to eq Taxy::Estimation::Vehicle::Eta
54
+ end
55
+
56
+ it 'returns estimations' do
57
+ expect(subject.first).to have_attributes({
58
+ total_price: 1234,
59
+ price_formatted: '12.34€',
60
+ currency: 'EUR',
61
+ currency_symbol: '€',
62
+ })
63
+ expect(subject.first.vehicle_type).to have_attributes({
64
+ _id: 'executive_id',
65
+ name: 'Executive Class',
66
+ short_name: 'Executive',
67
+ description: 'A very large vehicle with comfortable seats',
68
+ icon: 'executive',
69
+ })
70
+ expect(subject.first.vehicle_type.icons).to have_attributes({
71
+ regular: 'https://cabify.com/images/icons/vehicle_type/executive_27.png'
72
+ })
73
+ expect(subject.first.vehicle_type.eta).to have_attributes({
74
+ min: 100,
75
+ max: 1000,
76
+ formatted: '>2 min'
77
+ })
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Taxy::Client::User do
4
+ let(:client) { Taxy::Client.new(bearer_token: 'CABIFY_BEARER_TOKEN') }
5
+
6
+ describe '#user' do
7
+ context 'when the request is successful' do
8
+ let(:response_hash) do
9
+ {
10
+ name: 'John',
11
+ surname: 'Doe',
12
+ email: 'johndoe@example.com',
13
+ mobile_cc: '12',
14
+ mobile_num: '123123123',
15
+ locale: 'pt-BR',
16
+ avatar_url: 'http://example.com/example.jpg'
17
+ }
18
+ end
19
+
20
+ before do
21
+ stub_cabify_request(verb: :get, endpoint: '/api/v2/user', status: 200, response: response_hash)
22
+ end
23
+
24
+ it 'returns a Taxy::User instance' do
25
+ expect(client.user.class).to eq Taxy::User
26
+ end
27
+
28
+ it 'returns the current user' do
29
+ expect(client.user).to have_attributes({
30
+ name: 'John',
31
+ surname: 'Doe',
32
+ email: 'johndoe@example.com',
33
+ mobile_cc: '12',
34
+ mobile_num: '123123123',
35
+ locale: 'pt-BR',
36
+ avatar_url: 'http://example.com/example.jpg'
37
+ })
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ require 'bundler/setup'
2
+ require 'taxy'
3
+ require 'webmock/rspec'
4
+
5
+ Dir[File.join(File.dirname(__FILE__), './support/**/*.rb')].each { |f| require f }
6
+
7
+ RSpec.configure do |c|
8
+ c.include HttpMockHelpers
9
+
10
+ c.before :all do
11
+ WebMock.disable_net_connect!(allow_localhost: true)
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ module HttpMockHelpers
2
+ def stub_cabify_request(verb:, endpoint:, params: nil, response: nil, status: nil)
3
+ options = Hash.new.tap do |h|
4
+ h[:headers] = {
5
+ 'Authorization' => 'Bearer CABIFY_BEARER_TOKEN',
6
+ 'Content-Type'=>'application/json',
7
+ 'User-Agent'=>'Cabify API Ruby Wrapper'
8
+ }
9
+ h[:body] = params.to_json unless params.nil?
10
+ end
11
+
12
+ stub_request(verb, "#{Taxy::Connection::BASE_URI}#{endpoint}")
13
+ .with(options)
14
+ .to_return(status: status || 200, body: response ? response.to_json : '')
15
+ end
16
+ end
File without changes
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: taxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rafael G
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-16 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 0.9.0
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '0.9'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 0.9.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: virtus
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.0'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 1.0.5
85
+ type: :runtime
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '1.0'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 1.0.5
95
+ - !ruby/object:Gem::Dependency
96
+ name: json
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '1.8'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '1.8'
109
+ description:
110
+ email:
111
+ - gibim6+taxy@gmail.com
112
+ executables: []
113
+ extensions: []
114
+ extra_rdoc_files: []
115
+ files:
116
+ - LICENSE.txt
117
+ - README.md
118
+ - lib/client.rb
119
+ - lib/client/estimate.rb
120
+ - lib/client/user.rb
121
+ - lib/connection.rb
122
+ - lib/models/estimation.rb
123
+ - lib/models/estimation/vehicle.rb
124
+ - lib/models/estimation/vehicle/eta.rb
125
+ - lib/models/estimation/vehicle/icons.rb
126
+ - lib/models/user.rb
127
+ - lib/response/parse_json.rb
128
+ - lib/taxy.rb
129
+ - spec/client/estimate_spec.rb
130
+ - spec/client/user_spec.rb
131
+ - spec/spec_helper.rb
132
+ - spec/support/http_mock_helpers.rb
133
+ - spec/taxy_spec.rb
134
+ homepage: https://github.com/drowze/taxy
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.6.11
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Cabify API wrapper.
158
+ test_files: []