my_bcycle 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 19c2141c1b52c900e79f6bffd3924558df52476a
4
+ data.tar.gz: 6ed32f4d4f9abfa5e752043275ce9215ed8506d7
5
+ SHA512:
6
+ metadata.gz: 6bff6c1d287312bfa2584c51bfc4c84a20d629864d7b25072eda3d28de9594f105be26e0d6ceaa3633d21e3519158070e4dadd064592366289fca5611e5c961c
7
+ data.tar.gz: 50540ddfbb017b3edbe577d602e42bdf2b80c362a993427aff5fcdb9ad0e42df73b6a0422982ef719e9c41e1117b3473325f80dbfe10898b9550cac0739adf83
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,29 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Exclude:
4
+ - 'db/**/*'
5
+ - 'config/**/*'
6
+ - 'vendor/**/*'
7
+ - 'bin/**/*'
8
+ Style/StringLiterals:
9
+ EnforcedStyle: double_quotes
10
+ SupportedStyles:
11
+ - single_quotes
12
+ - double_quotes
13
+
14
+ Style/StringLiteralsInInterpolation:
15
+ EnforcedStyle: double_quotes
16
+ SupportedStyles:
17
+ - single_quotes
18
+ - double_quotes
19
+ Metrics/LineLength:
20
+ Max: 90
21
+ Documentation:
22
+ Enabled: false
23
+ Style/DoubleNegation:
24
+ Enabled: false
25
+ Style/MultilineBlockChain:
26
+ Enabled: false
27
+ Style/FrozenStringLiteralComment:
28
+ Enabled: false
29
+
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ my-bcycle
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.3.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in my_bcycle.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Gal Tsubery
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
+ # MyBcycle
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
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'my_bcycle'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install my_bcycle
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]/my_bcycle.
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
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "my_bcycle"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/my_bcycle.rb ADDED
@@ -0,0 +1,103 @@
1
+ require "my_bcycle/version"
2
+
3
+ module MyBcycle
4
+ InvalidCredentials = Class.new(StandardError)
5
+ CommunicationProblem = Class.new(StandardError)
6
+
7
+ class User
8
+ attr_reader :username, :password, :city
9
+ def initialize city: :austin, username:, password:
10
+ @city = city
11
+ @username = username
12
+ @password = password
13
+ end
14
+
15
+ def statistics_for(time)
16
+ return unless session_token
17
+ response = Typhoeus.get(
18
+ 'https://portal-den.bcycle.com/1/user/trips',
19
+ params: {
20
+ month: time.month,
21
+ year: time.year
22
+ },
23
+ headers: {'Bcycle-Session-Token': session_token },
24
+ accept_encoding: "gzip"
25
+ )
26
+ unless response.code == 200
27
+ raise "todo"
28
+ end
29
+ month_data = JSON.parse(response.body)
30
+ month_data.map do |entry|
31
+ k = Time.parse entry["checkOutDate"]
32
+ v = {
33
+ miles: entry["miles"],
34
+ duration: entry["duration"],
35
+ money_saved: entry["moneySaved"],
36
+ }
37
+ [ k, v ]
38
+ end.to_h
39
+ end
40
+
41
+ private
42
+ def session_token
43
+ @session_token ||= retrieve_token
44
+ end
45
+
46
+ def retrieve_token
47
+ cookiefile = Tempfile.new('bcycle-cookie')
48
+ cookie_path = cookiefile.path
49
+ begin
50
+ login_req = do_request! Typhoeus::Request.new(
51
+ login_url,
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]
67
+ end
68
+ ensure
69
+ cookiefile.close
70
+ cookiefile.unlink
71
+ end
72
+
73
+ def do_request! request
74
+ request.on_complete do |response|
75
+ next if response.success?
76
+
77
+ if response.timed_out?
78
+ raise CommunicationProblem, "Timeout"
79
+ elsif response.code == 0
80
+ raise CommunicationProblem, response.return_message.to_s
81
+ else
82
+ raise CommunicationProblem, "Got error code #{response.code.to_s}"
83
+ end
84
+ end
85
+ request.run
86
+ end
87
+
88
+ def login_url
89
+ "https://#{city}.bcycle.com/api/Authenticate"
90
+ end
91
+
92
+ def token_url
93
+ "https://#{city}.bcycle.com/api/sessiontoken/get"
94
+ end
95
+
96
+ def login_params
97
+ {
98
+ UserName: username,
99
+ Password: password
100
+ }
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,3 @@
1
+ module MyBcycle
2
+ VERSION = "0.1.0"
3
+ end
data/my_bcycle.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'my_bcycle/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "my_bcycle"
8
+ spec.version = MyBcycle::VERSION
9
+ spec.authors = ["Gal Tsubery"]
10
+ spec.email = ["gal.tsubery@gmail.com"]
11
+
12
+ spec.summary = "Wrapper of bcycle personal usage statistics API"
13
+ spec.description = "This gem retrieves bcycle personal usage statistics from their website, including total miles, duration and money saved."
14
+ spec.homepage = "http://github.com/tsubery/my_bcycle/"
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.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "webmock", "~> 2.0"
26
+ spec.add_development_dependency "vcr", "~> 3.0"
27
+ spec.add_development_dependency "rubocop"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_dependency "typhoeus", "~> 1.0"
30
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: my_bcycle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gal Tsubery
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: vcr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
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: typhoeus
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.0'
125
+ description: This gem retrieves bcycle personal usage statistics from their website,
126
+ including total miles, duration and money saved.
127
+ email:
128
+ - gal.tsubery@gmail.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".rubocop.yml"
136
+ - ".ruby-gemset"
137
+ - ".ruby-version"
138
+ - ".travis.yml"
139
+ - Gemfile
140
+ - LICENSE.txt
141
+ - README.md
142
+ - Rakefile
143
+ - bin/console
144
+ - bin/setup
145
+ - lib/my_bcycle.rb
146
+ - lib/my_bcycle/version.rb
147
+ - my_bcycle.gemspec
148
+ homepage: http://github.com/tsubery/my_bcycle/
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.5.1
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Wrapper of bcycle personal usage statistics API
172
+ test_files: []