health_graph 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rvmrc ADDED
@@ -0,0 +1,7 @@
1
+
2
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
3
+ && -s "${rvm_path:-$HOME/.rvm}/environments/ruby-1.9.2-p0@health_graph" ]] ; then
4
+ \. "${rvm_path:-$HOME/.rvm}/environments/ruby-1.9.2-p0@health_graph"
5
+ else
6
+ rvm --create "ruby-1.9.2-p0@health_graph"
7
+ fi
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "oauth2", "= 0.5.1"
7
+ gem "faraday", "= 0.7.4"
8
+ gem "faraday_middleware", "= 0.7.0"
9
+ gem "hashie", "= 1.1.0"
10
+
11
+ # Add dependencies to develop your gem here.
12
+ # Include everything needed to run rake, tests, features, etc.
13
+ group :development do
14
+ gem "shoulda", ">= 0"
15
+ gem "webmock", "= 1.7.4"
16
+ gem "bundler", "~> 1.0.0"
17
+ gem "jeweler", "~> 1.6.4"
18
+ gem "rcov", ">= 0"
19
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.2.6)
5
+ crack (0.3.1)
6
+ faraday (0.7.4)
7
+ addressable (~> 2.2.6)
8
+ multipart-post (~> 1.1.0)
9
+ rack (>= 1.1.0, < 2)
10
+ faraday_middleware (0.7.0)
11
+ faraday (~> 0.7.3)
12
+ git (1.2.5)
13
+ hashie (1.1.0)
14
+ jeweler (1.6.4)
15
+ bundler (~> 1.0)
16
+ git (>= 1.2.5)
17
+ rake
18
+ multi_json (1.0.3)
19
+ multipart-post (1.1.3)
20
+ oauth2 (0.5.1)
21
+ faraday (~> 0.7.4)
22
+ multi_json (~> 1.0.3)
23
+ rack (1.3.4)
24
+ rake (0.9.2)
25
+ rcov (0.9.10)
26
+ shoulda (2.11.3)
27
+ webmock (1.7.4)
28
+ addressable (~> 2.2, > 2.2.5)
29
+ crack (>= 0.1.7)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ bundler (~> 1.0.0)
36
+ faraday (= 0.7.4)
37
+ faraday_middleware (= 0.7.0)
38
+ hashie (= 1.1.0)
39
+ jeweler (~> 1.6.4)
40
+ oauth2 (= 0.5.1)
41
+ rcov
42
+ shoulda
43
+ webmock (= 1.7.4)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Kenny Ma
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = health_graph
2
+
3
+ Ruby gem to work with RunKeeper Health Graph API.
4
+
5
+ == Contributing to health_graph
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Kenny Ma. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "health_graph"
18
+ gem.homepage = "http://github.com/kennyma/health_graph"
19
+ gem.license = "MIT"
20
+ gem.summary = "Ruby gem for RunKeeper Health Graph API"
21
+ gem.description = "This is a wrapper for RunKeeper Health Graph RESTful API."
22
+ gem.email = "kenny@kennyma.me"
23
+ gem.authors = ["Kenny Ma"]
24
+ # dependencies defined in Gemfile
25
+ gem.add_dependency 'oauth2', '= 0.5.1'
26
+ gem.add_dependency 'faraday', '= 0.7.4'
27
+ gem.add_dependency 'faraday_middleware', '= 0.7.0'
28
+ gem.add_dependency 'hashie', '= 1.1.0'
29
+ gem.add_dependency 'webmock', '= 1.7.6'
30
+ end
31
+ Jeweler::RubygemsDotOrgTasks.new
32
+
33
+ require 'rake/testtask'
34
+ Rake::TestTask.new(:test) do |test|
35
+ test.libs << 'lib' << 'test'
36
+ test.pattern = 'test/**/test_*.rb'
37
+ test.verbose = true
38
+ end
39
+
40
+ require 'rcov/rcovtask'
41
+ Rcov::RcovTask.new do |test|
42
+ test.libs << 'test'
43
+ test.pattern = 'test/**/test_*.rb'
44
+ test.verbose = true
45
+ test.rcov_opts << '--exclude "gems/*"'
46
+ end
47
+
48
+ task :default => :test
49
+
50
+ require 'rake/rdoctask'
51
+ Rake::RDocTask.new do |rdoc|
52
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
53
+
54
+ rdoc.rdoc_dir = 'rdoc'
55
+ rdoc.title = "health_graph #{version}"
56
+ rdoc.rdoc_files.include('README*')
57
+ rdoc.rdoc_files.include('lib/**/*.rb')
58
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.5.1
@@ -0,0 +1,16 @@
1
+ require 'oauth2'
2
+ require 'faraday_middleware'
3
+ require 'health_graph/configuration'
4
+ require 'health_graph/authentication'
5
+ require 'health_graph/api'
6
+ require 'health_graph/model'
7
+ require 'health_graph/models/user'
8
+ require 'health_graph/models/profile'
9
+ require 'health_graph/models/weight_feed'
10
+ require 'health_graph/models/sleep_feed'
11
+ require 'health_graph/models/fitness_activities_feed'
12
+
13
+ module HealthGraph
14
+ extend Configuration
15
+ extend Authentication
16
+ end
@@ -0,0 +1,52 @@
1
+ module HealthGraph
2
+ module API
3
+ attr_accessor :access_token
4
+
5
+ def get(path, accept_header, params = {})
6
+ request(:get, accept_header, path, params)
7
+ end
8
+
9
+ def post(path, accept_header, params = {})
10
+ request(:post, accept_header, path, params)
11
+ end
12
+
13
+ def put(path, accept_header, params = {})
14
+ request(:put, accept_header, path, params)
15
+ end
16
+
17
+ def delete(path, accept_header, params = {})
18
+ request(:delete, accept_header, path, params)
19
+ end
20
+
21
+ private
22
+
23
+ def request(method, accept_header, path, params)
24
+ response = connection.send(method) do |request|
25
+ request.headers['Authorization'] = "Bearer #{access_token}"
26
+ request.headers['Accept'] = accept_header
27
+
28
+ case method.to_sym
29
+ when :get, :delete
30
+ request.url(path, params)
31
+ when :put, :post
32
+ request.path = path
33
+ request.body = params unless params.empty?
34
+ end
35
+ end
36
+ response.body
37
+ end
38
+
39
+ def connection
40
+ merged_options = HealthGraph.faraday_options.merge({
41
+ :url => HealthGraph.endpoint
42
+ })
43
+
44
+ Faraday.new(merged_options) do |builder|
45
+ builder.use Faraday::Request::UrlEncoded
46
+ builder.use Faraday::Response::Mashify
47
+ builder.use Faraday::Response::ParseJson
48
+ builder.adapter(HealthGraph.adapter)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,15 @@
1
+ module HealthGraph
2
+ module Authentication
3
+ def authorize_url
4
+ client.auth_code.authorize_url :redirect_uri => authorization_redirect_url
5
+ end
6
+
7
+ def access_token(code)
8
+ client.auth_code.get_token(code, :redirect_uri => authorization_redirect_url).token
9
+ end
10
+
11
+ def client
12
+ OAuth2::Client.new client_id, client_secret, {:authorize_url => authorization_url, :token_url => access_token_url}
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,74 @@
1
+ module HealthGraph
2
+ module Configuration
3
+ VALID_OPTIONS_KEYS = [
4
+ :client_id,
5
+ :client_secret,
6
+ :authorization_url,
7
+ :access_token_url,
8
+ :authorization_redirect_url,
9
+ :endpoint,
10
+ :adapter,
11
+ :faraday_options,
12
+ :accept_headers].freeze
13
+
14
+ DEFAULT_CLIENT_ID = nil
15
+ DEFAULT_CLIENT_SECRET = nil
16
+ DEFAULT_AUTHORIZATION_URL = "http://runkeeper.com/apps/authorize".freeze
17
+ DEFAULT_ACCESS_TOKEN_URL = "http://runkeeper.com/apps/token".freeze
18
+ DEFAULT_AUTHORIZATION_REDIRECT_URL = nil
19
+ DEFAULT_ENDPOINT = "http://api.runkeeper.com".freeze
20
+ DEFAULT_ADAPTER = :net_http
21
+ DEFAULT_FARADAY_OPTIONS = {}.freeze
22
+ DEFAULT_ACCEPT_HEADERS = {
23
+ :user => "application/vnd.com.runkeeper.User+json",
24
+ :fitness_activity_feed => "application/vnd.com.runkeeper.FitnessActivityFeed+json",
25
+ :fitness_activity => "application/vnd.com.runkeeper.FitnessActivity+json",
26
+ :strength_training_activity_feed => "application/vnd.com.runkeeper.StrengthTrainingActivityFeed+json",
27
+ :strength_training_activity => "application/vnd.com.runkeeper.StrengthTrainingActivity+json",
28
+ :background_activity_feed => "application/vnd.com.runkeeper.BackgroundActivityFeed+json",
29
+ :background_activity => "application/vnd.com.runkeeper.BackgroundActivity+json",
30
+ :sleep_feed => "application/vnd.com.runkeeper.SleepFeed+json",
31
+ :sleep => "application/vnd.com.runkeeper.Sleep+json",
32
+ :nutrition_feed => "application/vnd.com.runkeeper.NutritionFeed+json",
33
+ :nutrition => "application/vnd.com.runkeeper.Nutrition+json",
34
+ :weight_feed => "application/vnd.com.runkeeper.WeightFeed+json",
35
+ :weight => "application/vnd.com.runkeeper.Weight+json",
36
+ :general_measurement_feed => "application/vnd.com.runkeeper.GeneralMeasurementFeed+json",
37
+ :general_measurement => "application/vnd.com.runkeeper.GeneralMeasurement+json",
38
+ :diabetes_feed => "application/vnd.com.runkeeper.DiabetesFeed+json",
39
+ :diatetes_measurement => "application/vnd.com.runkeeper.DiabetesMeasurement+json",
40
+ :records => "application/vnd.com.runkeeper.Records+json",
41
+ :profile => "application/vnd.com.runkeeper.Profile+json",
42
+ :new_activity => "application/vnd.com.runkeeper.NewFitnessActivity+json"
43
+ }.freeze
44
+
45
+ attr_accessor *VALID_OPTIONS_KEYS
46
+
47
+ def self.extended(base)
48
+ base.reset
49
+ end
50
+
51
+ def configure
52
+ yield self
53
+ end
54
+
55
+ def options
56
+ options = {}
57
+ VALID_OPTIONS_KEYS.each{|k| options[k] = send(k)}
58
+ options
59
+ end
60
+
61
+ def reset
62
+ self.client_id = DEFAULT_CLIENT_ID
63
+ self.client_secret = DEFAULT_CLIENT_SECRET
64
+ self.authorization_url = DEFAULT_AUTHORIZATION_URL
65
+ self.access_token_url = DEFAULT_ACCESS_TOKEN_URL
66
+ self.authorization_redirect_url = DEFAULT_AUTHORIZATION_REDIRECT_URL
67
+ self.endpoint = DEFAULT_ENDPOINT
68
+ self.adapter = DEFAULT_ADAPTER
69
+ self.faraday_options = DEFAULT_FARADAY_OPTIONS
70
+ self.accept_headers = DEFAULT_ACCEPT_HEADERS
71
+ self
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,40 @@
1
+ module HealthGraph
2
+ module Model
3
+ include API
4
+
5
+ attr_accessor :body
6
+
7
+ def self.included(includer)
8
+ includer.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+ def from_hash(hash)
13
+ instance = new(hash)
14
+ yield instance if block_given?
15
+ instance
16
+ end
17
+
18
+ def hash_attr_accessor(*symbols)
19
+ attr_writer(*symbols)
20
+ symbols.each do |symbol|
21
+ define_method(symbol) do
22
+ instance_variable_get("@#{symbol}")
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def populate_from_hash!(hash)
29
+ hash.each do |key, value|
30
+ set_attr_method = "#{key}="
31
+ unless value.nil?
32
+ if respond_to?(set_attr_method)
33
+ self.__send__(set_attr_method, value)
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,23 @@
1
+ module HealthGraph
2
+ class FitnessActivitiesFeed
3
+ include Model
4
+
5
+ hash_attr_accessor :items, :next, :previous, :size
6
+
7
+ class Item
8
+ include Model
9
+
10
+ hash_attr_accessor :type, :start_time, :total_distance, :duration, :uri
11
+
12
+ def initialize(hash)
13
+ populate_from_hash! hash
14
+ end
15
+ end
16
+
17
+ def initialize(access_token, path)
18
+ self.access_token = access_token
19
+ @body = get path, HealthGraph.accept_headers[:fitness_activity_feed]
20
+ populate_from_hash! @body
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ module HealthGraph
2
+ class Profile
3
+ include Model
4
+
5
+ hash_attr_accessor :location, :name, :gender, :athlete_type, :profile
6
+
7
+ def initialize(access_token, path)
8
+ self.access_token = access_token
9
+ @body = get path, HealthGraph.accept_headers[:profile]
10
+ populate_from_hash! @body
11
+ end
12
+
13
+ def elite?
14
+ @body["elite"] == "true"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ module HealthGraph
2
+ class SleepFeed
3
+ include Model
4
+
5
+ hash_attr_accessor :items
6
+
7
+ class Item
8
+ include Model
9
+
10
+ hash_attr_accessor :timestamp, :total_sleep, :times_woken, :rem, :deep, :light, :awake, :uri
11
+
12
+ def initialize(hash)
13
+ populate_from_hash! hash
14
+ end
15
+ end
16
+
17
+ def initialize(access_token, path)
18
+ self.access_token = access_token
19
+ @body = get path, HealthGraph.accept_headers[:sleep_feed]
20
+ populate_from_hash! @body
21
+ end
22
+ end
23
+ end