strava-api-v2 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. data/.gitignore +18 -0
  2. data/.rvmrc +60 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +100 -0
  6. data/Rakefile +9 -0
  7. data/lib/strava/base_connection.rb +29 -0
  8. data/lib/strava/exceptions.rb +7 -0
  9. data/lib/strava/strava_response.rb +5 -0
  10. data/lib/strava/v1/clubs_request.rb +20 -0
  11. data/lib/strava/v1/connection.rb +22 -0
  12. data/lib/strava/v1/models/athlete.rb +15 -0
  13. data/lib/strava/v1/models/club.rb +14 -0
  14. data/lib/strava/v1/models/effort.rb +17 -0
  15. data/lib/strava/v1/models/ride.rb +23 -0
  16. data/lib/strava/v1/models/segment.rb +19 -0
  17. data/lib/strava/v1/models/stream.rb +17 -0
  18. data/lib/strava/v1/rides_request.rb +60 -0
  19. data/lib/strava/v1/segments_request.rb +31 -0
  20. data/lib/strava/v1/streams_request.rb +15 -0
  21. data/lib/strava/v2/auth_request.rb +17 -0
  22. data/lib/strava/v2/connection.rb +27 -0
  23. data/lib/strava/v2/efforts_request.rb +21 -0
  24. data/lib/strava/v2/models/athlete.rb +15 -0
  25. data/lib/strava/v2/models/auth.rb +15 -0
  26. data/lib/strava/v2/models/effort.rb +19 -0
  27. data/lib/strava/v2/models/segment.rb +26 -0
  28. data/lib/strava/v2/segments_request.rb +21 -0
  29. data/lib/strava/version.rb +3 -0
  30. data/strava-api-v2.gemspec +26 -0
  31. data/test/auth_test.rb +32 -0
  32. data/test/clubs_test.rb +31 -0
  33. data/test/efforts_v2_test.rb +34 -0
  34. data/test/rides_v1_test.rb +62 -0
  35. data/test/segments_v1_request_test.rb +39 -0
  36. data/test/segments_v2_request_test.rb +47 -0
  37. data/test/stream_test.rb +30 -0
  38. data/test/test_data.rb +52 -0
  39. data/test/v1_connection_test.rb +37 -0
  40. data/test/v2_connection_test.rb +37 -0
  41. metadata +190 -0
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/.rvmrc ADDED
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p392@strava_api"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.19.6 ()" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
27
+ do
28
+ if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
29
+ then \. "${__hook}" || true
30
+ fi
31
+ done
32
+ unset __hook
33
+ if (( ${rvm_use_flag:=1} >= 2 )) # display only when forced
34
+ then
35
+ if [[ $- == *i* ]] # check for interactive shells
36
+ then echo "Using: \E[32m$GEM_HOME\E[0m" # show the user the ruby and gemset they are using in green
37
+ else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
38
+ fi
39
+ fi
40
+ else
41
+ # If the environment file has not yet been created, use the RVM CLI to select.
42
+ rvm --create "$environment_id" || {
43
+ echo "Failed to create RVM environment '${environment_id}'."
44
+ return 1
45
+ }
46
+ fi
47
+
48
+ # If you use bundler, this might be useful to you:
49
+ # if [[ -s Gemfile ]] && {
50
+ # ! builtin command -v bundle >/dev/null ||
51
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
52
+ # }
53
+ # then
54
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
55
+ # gem install bundler
56
+ # fi
57
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
58
+ # then
59
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
60
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in strava-api-v2.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Luke StClair
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # Strava::Api::V2
2
+
3
+ API for accessing the strava V2 API - (http://bit.ly/161qmXg)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'strava-api-v2'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install strava-api-v2
18
+
19
+ ## Usage
20
+
21
+ This module includes both versions of the API (at least the sparse parts I was interested in putting
22
+ together. The confusing thing is that you'll probably have to mix and match to get things done that you care about.
23
+
24
+ ## Getting a connection
25
+
26
+ The connection object is used to interact with Strava's servers.
27
+
28
+ $ connection_v1 = Strava::V1::Connection.new
29
+ $ connection_v2 = Strava::V2::Connection.new
30
+
31
+ ## Fetching rides
32
+
33
+ Get a random sampling of all rides
34
+
35
+ $ rides = connection_v1.rides
36
+
37
+ Or you can pass in parameters to get a more useful subset of rides.
38
+
39
+ For instance, to get the top rides from a club
40
+ $ rides = connection_v1.rides(:club_id => 15)
41
+
42
+ Or get the rides for a given athlete
43
+ $ rides = connection_v1.rides(:athlete_id = > 7679)
44
+
45
+ Other parameters include :athlete_name, :start_date, :end_date, :start_id, and :offset (http://bit.ly/18CrfCa)
46
+
47
+ ## Streams
48
+
49
+ Get the stream information about a ride, including heart rates, coordinates, times, distances, altitudes of points
50
+ sampled on a ride.
51
+
52
+ $ stream = connection_v1.stream(ride_id)
53
+
54
+ ## Efforts for segment
55
+
56
+ You can find all the efforts for a segment in the following way
57
+
58
+ $ segment = connection_v1.segment_with_efforts(segment_id, params)
59
+ $ efforts = segment.efforts
60
+
61
+ The efforts can be search by different parameters, including :club_id, :athlete_id, :athlete_name, :start_date,
62
+ :end_date, :start_id, :best => true
63
+
64
+ ## Getting clubs
65
+
66
+ Search for club by name
67
+
68
+ $ clubs = connection_v1.rides(:name => "Mission Cycling")
69
+
70
+ ## Authenticating
71
+
72
+ See http://bit.ly/18Ctky9 for more details - this also enables you to retrieve tokens and athlete information.
73
+
74
+ $ auth = connection_v2.login("email@example.com", "password")
75
+ $ token = auth.token
76
+ $ athlete = auth.athlete
77
+
78
+ ## Segments for a given ride
79
+
80
+ See http://bit.ly/125MoTh for more details - this allows you to look up the segments for a given ride
81
+
82
+ $ segments = connection_v2.segments_for_ride(ride_id)
83
+ $ first_segment_efforts = segments[0].efforts
84
+
85
+ ## All efforts for a given ride
86
+
87
+ See http://bit.ly/125MoTh - looks up all efforts put out in a given ride.
88
+
89
+ $ segments = connection_v2.segments_and_efforts_for_ride(ride_id)
90
+ $ first_effort = segments[0].efforts[0]
91
+
92
+ ## Issues
93
+
94
+ ## Contributing
95
+
96
+ 1. Fork it
97
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
98
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
99
+ 4. Push to the branch (`git push origin my-new-feature`)
100
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
@@ -0,0 +1,29 @@
1
+ require 'httparty'
2
+ require 'strava/exceptions'
3
+
4
+ module Strava
5
+ module BaseConnection
6
+ def fetch(relative_uri, options)
7
+ response = nil
8
+ begin
9
+ response = self.class.get("/#{relative_uri}", :query => options)
10
+ rescue HTTParty::UnsupportedFormat, HTTParty::UnsupportedURIScheme, HTTParty::ResponseError, HTTParty::RedirectionTooDeep
11
+ # if it's something we understand, it's probably a bad request.
12
+ raise RequestError.new
13
+ rescue Exception => e
14
+ # anything else is presumably not from HTTParty - throw generic network error.
15
+ raise NetworkError.new(e)
16
+ end
17
+
18
+ case response.code
19
+ when 500...600
20
+ raise StravaError.new
21
+ when 404
22
+ raise '404ed'
23
+ when 200
24
+ return response.parsed_response
25
+ end
26
+ raise NetworkError.new('Bad status ' + response.code.to_s)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,7 @@
1
+ module Strava
2
+ class StravaError < StandardError; end
3
+
4
+ class RequestError < StandardError; end
5
+
6
+ class NetworkError < StandardError; end
7
+ end
@@ -0,0 +1,5 @@
1
+ class StravaResponse
2
+ def initialize(key_value_map)
3
+
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ require 'strava/v1/models/club'
2
+
3
+ module Strava
4
+ module V1
5
+ module ClubsRequest
6
+ def clubs_path
7
+ 'clubs'
8
+ end
9
+
10
+ def clubs(name)
11
+ all_clubs = []
12
+ self.fetch(clubs_path, {:name => name})["clubs"].each { |club_json|
13
+ all_clubs.push(Club.new(club_json))
14
+ }
15
+ all_clubs
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,22 @@
1
+ require 'httparty'
2
+ require 'strava/base_connection'
3
+ require 'strava/v1/rides_request'
4
+ require 'strava/v1/streams_request'
5
+ require 'strava/v1/segments_request'
6
+ require 'strava/v1/clubs_request'
7
+
8
+ module Strava
9
+ module V1
10
+ class Connection
11
+ include HTTParty
12
+ include Strava::V1::RidesRequest
13
+ include Strava::V1::StreamRequest
14
+ include Strava::V1::SegmentRequest
15
+ include Strava::V1::ClubsRequest
16
+ include Strava::BaseConnection
17
+
18
+ base_uri 'http://www.strava.com/api/v1'
19
+ format :json
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,15 @@
1
+ require 'strava/strava_response'
2
+
3
+ module Strava
4
+ module V1
5
+ class Athlete < StravaResponse
6
+ attr_reader :id, :username, :name
7
+
8
+ def initialize(jb = {})
9
+ @id = jb["id"]
10
+ @username = jb["username"]
11
+ @name = jb["name"]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ require 'strava/strava_response'
2
+
3
+ module Strava
4
+ module V1
5
+ class Club < StravaResponse
6
+ attr_reader :id, :name
7
+
8
+ def initialize(jb)
9
+ @id = jb["id"]
10
+ @name = jb["name"]
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ require 'strava/strava_response'
2
+ require 'strava/v1/models/segment'
3
+ require 'strava/v1/models/athlete'
4
+
5
+ module Strava
6
+ module V1
7
+ class Effort < StravaResponse
8
+ attr_reader :id, :elapsed_time, :athlete
9
+
10
+ def initialize(js)
11
+ @id = js["id"]
12
+ @elapsed_time = js["elapsed_time"]
13
+ @athlete = Athlete.new(js["athlete"]) if !js["athlete"].nil?
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ require 'strava/strava_response'
2
+ require 'strava/v1/models/athlete'
3
+
4
+ module Strava
5
+ module V1
6
+ class Ride < StravaResponse
7
+ attr_reader :id, :name, :distance, :moving_time, :average_speed, :average_watts, :name, :elevation_gain, :athlete
8
+ attr_accessor :segments
9
+
10
+ def initialize(json)
11
+ @id = json["id"]
12
+ @name = json["name"]
13
+ @distance = json["distance"]
14
+ @moving_time = json["moving_time"]
15
+ @average_speed = json["average_speed"]
16
+ @average_watts = json["average_watts"]
17
+ @name = json["name"]
18
+ @elevation_gain = json["elevationGain"]
19
+ @athlete = Athlete.new(json["athlete"]) if !json["athlete"].nil?
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ require 'strava/strava_response'
2
+
3
+ module Strava
4
+ module V1
5
+ class Segment
6
+ attr_reader :id, :name, :efforts
7
+
8
+ # ejb - efforts json, in list form
9
+ def initialize(jb, ejb)
10
+ @id = jb["id"]
11
+ @name = jb["name"]
12
+ @efforts = []
13
+ ejb.each { |effort_json|
14
+ @efforts.push(Effort.new(effort_json))
15
+ }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ require 'strava/strava_response'
2
+
3
+ module Strava
4
+ module V1
5
+ class Stream < StravaResponse
6
+ attr_reader :coordinates, :heartrates, :times, :distances, :altitudes
7
+
8
+ def initialize(jb)
9
+ @coordinates = jb["latlng"]
10
+ @heartrates = jb["heartrate"]
11
+ @times = jb["time"]
12
+ @distances = jb["distance"]
13
+ @altitudes = jb["altitude"]
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,60 @@
1
+ require 'strava/v1/models/ride'
2
+ require 'strava/v1/models/effort'
3
+
4
+ module Strava
5
+ module V1
6
+ module RidesRequest
7
+ RIDES_PARAMS_MAP = {
8
+ :club_id => :clubId,
9
+ :athlete_id => :athleteId,
10
+ :athlete_name => :athleteName,
11
+ :start_date => :startDate,
12
+ :end_date => :endDate,
13
+ :start_id => :startId,
14
+ :offset => :offset
15
+ }
16
+
17
+ def rides_path
18
+ "rides"
19
+ end
20
+
21
+ def ride_path(id)
22
+ "rides/#{id}"
23
+ end
24
+
25
+ def rides_with_efforts_path(id)
26
+ rides_path + "/#{id}/efforts"
27
+ end
28
+
29
+ def rides(params = {})
30
+ final_params = {}
31
+ params.each_pair do |key, value|
32
+ raise RequestError.new if RIDES_PARAMS_MAP[key].nil?
33
+ final_params[RIDES_PARAMS_MAP[key]] = value
34
+ end
35
+
36
+ all_rides = []
37
+ rides_array = self.fetch(rides_path(), final_params)["rides"]
38
+ rides_array.each { |ride_json|
39
+ all_rides.push(Ride.new(ride_json))
40
+ }
41
+ all_rides
42
+ end
43
+
44
+ def ride_with_efforts(ride_id)
45
+ all_json = self.fetch(rides_with_efforts_path(ride_id), {})
46
+ ride = Ride.new(all_json["ride"])
47
+ all_segments = []
48
+ all_json["efforts"].each { |effort_json|
49
+ all_segments.push(Segment.new(effort_json["segment"], [effort_json]))
50
+ }
51
+ ride.segments = all_segments
52
+ ride
53
+ end
54
+
55
+ def ride(ride_id)
56
+ Ride.new(self.fetch(ride_path(ride_id), {})["ride"])
57
+ end
58
+ end
59
+ end
60
+ end