stravatt 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 +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +74 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/stravatt/effort_finder.rb +49 -0
- data/lib/stravatt/leaderboard.rb +29 -0
- data/lib/stravatt/time_trial.rb +32 -0
- data/lib/stravatt/user.rb +3 -0
- data/lib/stravatt/version.rb +3 -0
- data/lib/stravatt.rb +12 -0
- data/stravatt.gemspec +28 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 91f05775737ace0f36af6b1d3619beabae212a79
|
4
|
+
data.tar.gz: fdcc8c322ba0c0c975cfbcb69b2c587dd3f2b249
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 101b9e6e8860ddbe1132d43adae69fb084b256ab55387de2ad8b2787ba75b2aebde751747da2fa57a2d8214211df36c4c7343f5f2868f36468f3729a796daf32
|
7
|
+
data.tar.gz: e7745c48ca8a81fa89912c2d3efca3cf82cc4c7b06885781f7d0885975c3da3c86123e72209b83a0a79ef6cd9de5e7570e8421a5140023f51ed8da10dca652e8
|
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 Andrea Franz
|
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,74 @@
|
|
1
|
+
# StravaTT
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/pilu/stravatt.png)](https://travis-ci.org/pilu/stravatt)
|
4
|
+
|
5
|
+
Given a Strava segment, a time range, and some users, it takes the first effort of each user on the selected
|
6
|
+
segment and creates a leaderboard based on efforts moving times.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'stravatt'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install stravatt
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'time'
|
28
|
+
require 'stravatt'
|
29
|
+
|
30
|
+
segment_id = SEGMENT_ID
|
31
|
+
start_time = Time.parse DATE_STRING
|
32
|
+
end_time = Time.parse DATE_STRING
|
33
|
+
|
34
|
+
tt = StravaTT.new segment_id, start_time, end_time
|
35
|
+
tt.leaderboard [
|
36
|
+
StravaTT::User.new(YOUR_INTERNAL_ID, STRAVA_USER_ACCESS_TOKEN),
|
37
|
+
StravaTT::User.new(YOUR_INTERNAL_ID, STRAVA_USER_ACCESS_TOKEN),
|
38
|
+
StravaTT::User.new(YOUR_INTERNAL_ID, STRAVA_USER_ACCESS_TOKEN)
|
39
|
+
]
|
40
|
+
|
41
|
+
=> [
|
42
|
+
{
|
43
|
+
:user_id=>1,
|
44
|
+
:effort=>
|
45
|
+
#<MiniStrava::Models::SegmentEffort:0x007f83a32a4fd0
|
46
|
+
...
|
47
|
+
},
|
48
|
+
{
|
49
|
+
:user_id=>2,
|
50
|
+
:effort=>
|
51
|
+
#<MiniStrava::Models::SegmentEffort:0x007f83a32a4fd0
|
52
|
+
...
|
53
|
+
},
|
54
|
+
...
|
55
|
+
]
|
56
|
+
```
|
57
|
+
|
58
|
+
More info about the SegmentEffort object in the [mini-strava repository](https://github.com/pilu/mini-strava).
|
59
|
+
|
60
|
+
## Development
|
61
|
+
|
62
|
+
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.
|
63
|
+
|
64
|
+
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).
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/pilu/stravatt.
|
69
|
+
|
70
|
+
|
71
|
+
## License
|
72
|
+
|
73
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
74
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "stravatt"
|
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,49 @@
|
|
1
|
+
module StravaTT
|
2
|
+
class EffortFinder
|
3
|
+
attr_reader :client, :start_time_limit, :end_time_limit, :segment_id
|
4
|
+
|
5
|
+
def initialize user_access_token, start_time, end_time, segment_id
|
6
|
+
validate_time_params start_time: start_time, end_time: end_time
|
7
|
+
|
8
|
+
@client = MiniStrava::Client.new user_access_token
|
9
|
+
@start_time_limit = start_time
|
10
|
+
@end_time_limit = end_time
|
11
|
+
@segment_id = segment_id
|
12
|
+
end
|
13
|
+
|
14
|
+
def find_first
|
15
|
+
efforts = client.activities(after: start_time_limit.to_i, before: end_time_limit.to_i).map do |a|
|
16
|
+
find_effort_on_activity a.id
|
17
|
+
end.compact
|
18
|
+
|
19
|
+
efforts.sort! do |a, b|
|
20
|
+
start_time_a = Time.parse a.start_date
|
21
|
+
start_time_b = Time.parse b.start_date
|
22
|
+
start_time_a <=> start_time_b
|
23
|
+
end
|
24
|
+
|
25
|
+
efforts.first
|
26
|
+
end
|
27
|
+
|
28
|
+
def find_effort_on_activity activity_id
|
29
|
+
activity = client.activity activity_id
|
30
|
+
effort = activity.segment_efforts.find do |se|
|
31
|
+
se.segment.id == segment_id
|
32
|
+
end
|
33
|
+
return nil unless effort
|
34
|
+
|
35
|
+
start_time = Time.parse effort.start_date
|
36
|
+
end_time = start_time + effort.moving_time
|
37
|
+
end_time > end_time_limit ? nil : effort
|
38
|
+
rescue MiniStrava::Client::ResourceNotFound
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def validate_time_params params={}
|
44
|
+
params.each do |k, v|
|
45
|
+
raise TypeError.new "#{k} must be of type Time" unless v.is_a? Time
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module StravaTT
|
2
|
+
class Leaderboard
|
3
|
+
attr_reader :store
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@store = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def add user_id, effort
|
10
|
+
store[user_id] = effort
|
11
|
+
end
|
12
|
+
|
13
|
+
def sort
|
14
|
+
results = store.sort do |(a_id, a_effort), (b_id, b_effort)|
|
15
|
+
return a unless b_effort
|
16
|
+
return b unless a_effort
|
17
|
+
|
18
|
+
a_effort.moving_time <=> b_effort.moving_time
|
19
|
+
end
|
20
|
+
|
21
|
+
results.map do |(id, effort)|
|
22
|
+
{
|
23
|
+
user_id: id,
|
24
|
+
effort: effort
|
25
|
+
}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module StravaTT
|
2
|
+
class TimeTrial
|
3
|
+
attr_reader :segment_id, :start_time, :end_time
|
4
|
+
|
5
|
+
def initialize segment_id, start_time, end_time
|
6
|
+
@segment_id = segment_id
|
7
|
+
@start_time = start_time
|
8
|
+
@end_time = end_time
|
9
|
+
end
|
10
|
+
|
11
|
+
def leaderboard users
|
12
|
+
leaderboard = Leaderboard.new
|
13
|
+
|
14
|
+
users.each do |user|
|
15
|
+
raise TypeError.new unless user.is_a? User
|
16
|
+
effort = first_effort_for user
|
17
|
+
if effort
|
18
|
+
leaderboard.add user.id, effort
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
leaderboard.sort
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def first_effort_for user
|
28
|
+
finder = EffortFinder.new user.access_token, start_time, end_time, segment_id
|
29
|
+
finder.find_first
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/stravatt.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'mini_strava'
|
2
|
+
require 'stravatt/version'
|
3
|
+
require 'stravatt/effort_finder'
|
4
|
+
require 'stravatt/time_trial'
|
5
|
+
require 'stravatt/user'
|
6
|
+
require 'stravatt/leaderboard'
|
7
|
+
|
8
|
+
module StravaTT
|
9
|
+
def self.new segment_id, start_time, end_time
|
10
|
+
TimeTrial.new segment_id, start_time, end_time
|
11
|
+
end
|
12
|
+
end
|
data/stravatt.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'stravatt/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "stravatt"
|
8
|
+
spec.version = Stravatt::VERSION
|
9
|
+
spec.authors = ["Andrea Franz"]
|
10
|
+
spec.email = ["andrea@gravityblast.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Create TimeTrials on Strava segments}
|
13
|
+
spec.description = %q{Create TimeTrials on Strava segments}
|
14
|
+
spec.homepage = "https://github.com/pilu/stravatt"
|
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_dependency 'mini_strava', '0.2.1'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "pry"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stravatt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrea Franz
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mini_strava
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.2.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.2.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Create TimeTrials on Strava segments
|
84
|
+
email:
|
85
|
+
- andrea@gravityblast.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- lib/stravatt.rb
|
100
|
+
- lib/stravatt/effort_finder.rb
|
101
|
+
- lib/stravatt/leaderboard.rb
|
102
|
+
- lib/stravatt/time_trial.rb
|
103
|
+
- lib/stravatt/user.rb
|
104
|
+
- lib/stravatt/version.rb
|
105
|
+
- stravatt.gemspec
|
106
|
+
homepage: https://github.com/pilu/stravatt
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.5.1
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Create TimeTrials on Strava segments
|
130
|
+
test_files: []
|