my_bcycle 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +9 -2
- data/Gemfile +1 -1
- data/README.md +15 -8
- data/Rakefile +1 -1
- data/lib/my_bcycle.rb +52 -44
- data/lib/my_bcycle/version.rb +1 -1
- data/my_bcycle.gemspec +6 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa39e4e6cd30525b88ebf6e8390ec26efde02937
|
4
|
+
data.tar.gz: 6e06c3ce1586491272465218f5194c4aa52bf123
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4747c39c4ad110ec5e7ce3b286d7a8e9c97c2b40f0397191d5c39391c6d09156a1d198dfde8916a938e0f282504f7068276e736575e278ead3448b6234e225bf
|
7
|
+
data.tar.gz: c56db5c896135271bf58b3ecc87455c727bf0ec8f2f68e27f4546fdae632df61deded66b7a2ee26c5108a76f3a50c3f2e31aa89c68f9e2a7e09a5df5bd2d0cff
|
data/.rubocop.yml
CHANGED
@@ -17,7 +17,9 @@ Style/StringLiteralsInInterpolation:
|
|
17
17
|
- single_quotes
|
18
18
|
- double_quotes
|
19
19
|
Metrics/LineLength:
|
20
|
-
|
20
|
+
Max: 90
|
21
|
+
Metrics/MethodLength:
|
22
|
+
Max: 15
|
21
23
|
Documentation:
|
22
24
|
Enabled: false
|
23
25
|
Style/DoubleNegation:
|
@@ -26,4 +28,9 @@ Style/MultilineBlockChain:
|
|
26
28
|
Enabled: false
|
27
29
|
Style/FrozenStringLiteralComment:
|
28
30
|
Enabled: false
|
29
|
-
|
31
|
+
Style/GuardClause:
|
32
|
+
Enabled: false
|
33
|
+
TrailingCommaInLiteral:
|
34
|
+
EnforcedStyleForMultiline: comma
|
35
|
+
Style/IfUnlessModifier:
|
36
|
+
Enabled: false
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,5 @@
|
|
1
|
-
|
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/my_bcycle`. 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
|
1
|
+
## Description
|
2
|
+
This gem wraps bcycle personal statistics api to provider statistics for specific user. Currently only Austin is tested and supported but it might work for other cities where bcycle is operating.
|
6
3
|
|
7
4
|
## Installation
|
8
5
|
|
@@ -22,8 +19,18 @@ Or install it yourself as:
|
|
22
19
|
|
23
20
|
## Usage
|
24
21
|
|
25
|
-
|
26
|
-
|
22
|
+
```ruby
|
23
|
+
me = MyBcycle::User.new(
|
24
|
+
username: "me",
|
25
|
+
password: "secret"
|
26
|
+
)
|
27
|
+
me.statistics_for(Time.parse('2016-05-01')) =>
|
28
|
+
"2001-01-01 01:01:01.01Z" => {
|
29
|
+
miles: 999.80,
|
30
|
+
duration: 22,
|
31
|
+
money_saved: 1.5
|
32
|
+
}
|
33
|
+
```
|
27
34
|
## Development
|
28
35
|
|
29
36
|
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.
|
@@ -32,7 +39,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
39
|
|
33
40
|
## Contributing
|
34
41
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
42
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tsubery/my_bcycle.
|
36
43
|
|
37
44
|
|
38
45
|
## License
|
data/Rakefile
CHANGED
data/lib/my_bcycle.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "my_bcycle/version"
|
2
|
+
require "typhoeus"
|
2
3
|
|
3
4
|
module MyBcycle
|
4
5
|
InvalidCredentials = Class.new(StandardError)
|
@@ -6,7 +7,7 @@ module MyBcycle
|
|
6
7
|
|
7
8
|
class User
|
8
9
|
attr_reader :username, :password, :city
|
9
|
-
def initialize
|
10
|
+
def initialize(city: :austin, username:, password:)
|
10
11
|
@city = city
|
11
12
|
@username = username
|
12
13
|
@password = password
|
@@ -14,63 +15,70 @@ module MyBcycle
|
|
14
15
|
|
15
16
|
def statistics_for(time)
|
16
17
|
return unless session_token
|
17
|
-
response = Typhoeus.
|
18
|
-
|
18
|
+
response = do_request! Typhoeus::Request.new(
|
19
|
+
"https://portal-den.bcycle.com/1/user/trips",
|
19
20
|
params: {
|
20
21
|
month: time.month,
|
21
|
-
year: time.year
|
22
|
+
year: time.year,
|
22
23
|
},
|
23
|
-
headers: {'Bcycle-Session-Token': session_token },
|
24
|
+
headers: { 'Bcycle-Session-Token': session_token },
|
24
25
|
accept_encoding: "gzip"
|
25
26
|
)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
parse_statistics(response.body)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def parse_statistics(body)
|
33
|
+
JSON.parse(body).map do |entry|
|
31
34
|
k = Time.parse entry["checkOutDate"]
|
32
35
|
v = {
|
33
36
|
miles: entry["miles"],
|
34
37
|
duration: entry["duration"],
|
35
38
|
money_saved: entry["moneySaved"],
|
36
39
|
}
|
37
|
-
[
|
40
|
+
[k, v]
|
38
41
|
end.to_h
|
39
42
|
end
|
40
43
|
|
41
|
-
private
|
42
44
|
def session_token
|
43
45
|
@session_token ||= retrieve_token
|
44
46
|
end
|
45
47
|
|
46
48
|
def retrieve_token
|
47
|
-
cookiefile = Tempfile.new(
|
49
|
+
cookiefile = Tempfile.new("bcycle-cookie")
|
48
50
|
cookie_path = cookiefile.path
|
49
51
|
begin
|
50
|
-
|
51
|
-
|
52
|
-
method: :post,
|
53
|
-
cookiefile: cookie_path,
|
54
|
-
cookiejar: cookie_path,
|
55
|
-
body: login_params
|
56
|
-
)
|
57
|
-
if login_req.body.match(/Invalid UserName/i)
|
58
|
-
raise InvalidCredentials
|
59
|
-
end
|
60
|
-
|
61
|
-
token_req = do_request! Typhoeus::Request.new(
|
62
|
-
token_url,
|
63
|
-
cookiefile: cookie_path,
|
64
|
-
cookiejar: cookie_path,
|
65
|
-
)
|
66
|
-
token_req.body[/\A"([a-z0-9\-]{36})"/i, 1]
|
52
|
+
login_request!(cookie_path)
|
53
|
+
token_request!(cookie_path).body[/\A"([a-z0-9\-]{36})"/i, 1]
|
67
54
|
end
|
68
55
|
ensure
|
69
56
|
cookiefile.close
|
70
57
|
cookiefile.unlink
|
71
58
|
end
|
72
59
|
|
73
|
-
def
|
60
|
+
def login_request!(cookie_path)
|
61
|
+
login_req = do_request! Typhoeus::Request.new(
|
62
|
+
login_url,
|
63
|
+
method: :post,
|
64
|
+
cookiefile: cookie_path,
|
65
|
+
cookiejar: cookie_path,
|
66
|
+
body: login_params
|
67
|
+
)
|
68
|
+
if login_req.body =~ /Invalid UserName/i
|
69
|
+
raise InvalidCredentials
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def token_request!(cookie_path)
|
74
|
+
do_request! Typhoeus::Request.new(
|
75
|
+
token_url,
|
76
|
+
cookiefile: cookie_path,
|
77
|
+
cookiejar: cookie_path
|
78
|
+
)
|
79
|
+
end
|
80
|
+
|
81
|
+
def do_request!(request)
|
74
82
|
request.on_complete do |response|
|
75
83
|
next if response.success?
|
76
84
|
|
@@ -79,25 +87,25 @@ module MyBcycle
|
|
79
87
|
elsif response.code == 0
|
80
88
|
raise CommunicationProblem, response.return_message.to_s
|
81
89
|
else
|
82
|
-
raise CommunicationProblem, "Got error code #{response.code
|
90
|
+
raise CommunicationProblem, "Got error code #{response.code}"
|
83
91
|
end
|
84
92
|
end
|
85
93
|
request.run
|
86
94
|
end
|
87
95
|
|
88
|
-
|
89
|
-
|
90
|
-
|
96
|
+
def login_url
|
97
|
+
"https://#{city}.bcycle.com/api/Authenticate"
|
98
|
+
end
|
91
99
|
|
92
|
-
|
93
|
-
|
94
|
-
|
100
|
+
def token_url
|
101
|
+
"https://#{city}.bcycle.com/api/sessiontoken/get"
|
102
|
+
end
|
95
103
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
end
|
104
|
+
def login_params
|
105
|
+
{
|
106
|
+
UserName: username,
|
107
|
+
Password: password,
|
108
|
+
}
|
102
109
|
end
|
110
|
+
end
|
103
111
|
end
|
data/lib/my_bcycle/version.rb
CHANGED
data/my_bcycle.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
4
|
+
require "my_bcycle/version"
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "my_bcycle"
|
@@ -10,11 +10,13 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["gal.tsubery@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = "Wrapper of bcycle personal usage statistics API"
|
13
|
-
spec.description = "This gem retrieves bcycle personal usage statistics
|
13
|
+
spec.description = "This gem retrieves bcycle personal usage statistics"\
|
14
|
+
" from their website, including total miles, duration and money saved."
|
14
15
|
spec.homepage = "http://github.com/tsubery/my_bcycle/"
|
15
16
|
spec.license = "MIT"
|
16
17
|
|
17
|
-
spec.files = `git ls-files -z
|
18
|
+
spec.files = `git ls-files -z`
|
19
|
+
.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
20
|
spec.bindir = "exe"
|
19
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
22
|
spec.require_paths = ["lib"]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: my_bcycle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gal Tsubery
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|