fitgem 0.3.6 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,4 +2,6 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ .yardoc/*
5
6
  test*.rb
7
+ *.dmp
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm 1.9.2@fitbit --create
1
+ rvm ruby-1.9.3-p0@fitbit --create
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
@@ -0,0 +1,7 @@
1
+ --no-private
2
+ --protected
3
+ --exclude /spec
4
+ --exclude /pkg
5
+ -
6
+ changelog.md
7
+ LICENSE
data/README.md CHANGED
@@ -1,101 +1,39 @@
1
- # Fitgem #
1
+ # Fitgem [![Build Status](https://secure.travis-ci.org/whazzmaster/fitgem.png)](http://travis-ci.org/whazzmaster/fitgem)
2
2
 
3
- Provides access to fitbit.com data through their OAuth/REST API. Without user authentication, any data that the a fitbit.com user has denoted as 'public' can be gathered. If a user logs in via OAuth then all exposed data can be gathered.
3
+ Provides access to fitbit.com data through their OAuth/REST API. Fitgem can pull data with or without user authentication. Without user authentication, any data that the a fitbit.com user has denoted as 'public' can be gathered. If a user logs in via OAuth then all exposed data can be gathered.
4
4
 
5
- The fitbit.com API is currently (March 2011) in BETA and is under development to extend its reach. Since it is early in the lifecycle of the API I expect this gem to go through a number of revisions as we attempt to match the functionality of their platform.
5
+ The fitbit.com API is currently (Late 2011) in BETA and is under development to extend its reach. Since it is early in the lifecycle of the API I expect this gem to go through a number of revisions as we attempt to match the functionality of their platform.
6
6
 
7
7
  # Usage #
8
8
 
9
- If you've ever done any oauth client programming then the model will appear familiar. Your first step, if haven't already, is to visit [https://dev.fitbit.com/](https://dev.fitbit.com/) and register your application to get your __consumer key__ and __consumer secret__ and set your __callback URL__, if appropriate for your app. There's more documentation at the site so I won't belabor it here.
9
+ Install fitgem:
10
+ ```
11
+ $ gem install fitgem
12
+ ```
10
13
 
11
- Below I've included two sample scripts that use Fitgem::Client to retrieve data. One shows how to do the initial authorization _without_ doing the callback; the other shows how to use saved values from that initial authorization to reconnect with the API and get subsequent information.
12
-
13
- require 'fitgem'
14
-
15
- consumer_key = 'your-app-consumer-key'
16
- consumer_secret = 'your-app-consumer-secret'
17
-
18
- client = Fitgem::Client.new({:consumer_key => consumer_key, :consumer_secret => consumer_secret})
19
-
20
- request_token = client.request_token
21
- token = request_token.token
22
- secret = request_token.secret
23
-
24
- puts "Go to http://www.fitbit.com/oauth/authorize?oauth_token=#{token} and then enter the verifier code below and hit Enter"
25
- verifier = gets.chomp
26
-
27
- access_token = client.authorize(token, secret, { :oauth_verifier => verifier })
28
-
29
- puts "Verifier is: "+verifier
30
- puts "Token is: "+access_token.token
31
- puts "Secret is: "+access_token.secret
32
-
33
- After running this and successfully connecting with verifier string that is displayed by the Fitgem site, you can reconnect using the script below. To do so, take the token and secret that were printed out from the script above and paste them in where appropriate. In this example we are using the client to get the
34
-
35
- require 'fitgem'
36
-
37
- consumer_key = 'your-app-consumer-key'
38
- consumer_secret = 'your-app-consumer-secret'
39
- token = 'token-received-in-above-script'
40
- secret = 'secret-received-in-above-script'
41
- user_id = 'your-user-id' # may be similar to '12345N'
42
-
43
- client = Fitgem::Client.new({:consumer_key => consumer_key, :consumer_secret => consumer_secret, :token => token, :secret => secret, :user_id => user_id})
44
- access_token = client.reconnect(token, secret)
45
- p client.user_info
46
-
47
- You can use this script to learn about the data structures that are returned from different API calls. Since this library always retrieves JSON responses and then parses it into Ruby structures, you can interrogate the response data with simple calls to hashes.
14
+ [The wiki has information](https://github.com/whazzmaster/fitgem/wiki/The-OAuth-Process) on how to use fitgem in the OAuth handshake with [fitbit.com](http://www.fitbit.com)
48
15
 
49
16
  ## Usage in a Rails Application ##
50
17
 
51
- We've started to develop an example app using the fitgem client. See [https://github.com/whazzmaster/fitgem-client](https://github.com/whazzmaster/fitgem-client) for more information.
18
+ We've started to develop an example app using the fitgem client. See [https://github.com/whazzmaster/fitgem-client](https://github.com/whazzmaster/fitgem-client) for more information or check out [the hosted version](http://www.fitbit-client.com). The fitgem-client project is evolving more slowly than the library itself, but work continues.
52
19
 
53
20
  # Subscriptions #
54
21
 
55
22
  The Fitbit API allows for you to set up notification subscription so that when values change (via automatic syncs with the fitbit device) your applications can be notified automatically. You can set up a default subscription callback URL via the [Fitbit dev site](https://dev.fitbit.com/ 'Fitbit Developer Site') and then use the Subscriptions API to add or remove subscriptions for individual users.
56
23
 
57
- __Currently, notification management is experimental in this gem__. We've built up code to add and remove specific types of subscriptions (foods, activities, sleep, body) but there seems to be some server-side issues with creating general, all-purpose subscriptions.
58
-
59
- The docs are very fuzzy on subscription support at the moment; we definitely plan on extending this support once the backend has matured (or the online docs have improved).
60
-
61
- # FAQs #
62
-
63
- ## What About ruby-fitbit? ##
24
+ __Currently, notification management is experimental in this gem__. There is currently code to add, remove, and list subscriptions (foods, activities, sleep, body, and all collections).
64
25
 
65
- There is a good looking gem called [ruby-fitbit](https://github.com/danmayer/ruby-fitbit "ruby-fitbit") that
66
- also aims to collect data from the site. It was created before they released their REST API and uses screen-scraping to gather the data rather than through their API. I looked into forking it and refactoring
67
- to use the new API but after looking through the code I felt it would be more of a total rewrite and so decided
68
- to create a new gem that I could design from scratch.
69
26
 
70
- ## Why the Name Change? ##
27
+ # FAQs
28
+ ## What About ruby-fitbit?
71
29
 
72
- It turns out that Fitbit.com does not want people to create libraries or applications that incorporate the name 'fitbit'. So, on May 12th I changed the name of the project/gem from 'fitbit' to 'fitgem'.
30
+ There is a good looking gem called [ruby-fitbit](https://github.com/danmayer/ruby-fitbit "ruby-fitbit") that also aims to collect data from the site. It was created before they released their REST API and uses screen-scraping to gather the data rather than through their API. I looked into forking it and refactoring to use the new API but after looking through the code I felt it would be more of a total rewrite and so decided to create a new gem that I could design from scratch.
73
31
 
74
- # Changelog #
32
+ ## Why the Name Change?
75
33
 
76
- * 12 July, 2011:
77
- * Added friends support (get friends, friend leaderboard, create invites, accept/reject invites)
78
- * Added water support (get water data per date)
79
- * Added sleep support (get sleep data per date)
80
- * Added ability to update user profile information
81
- * 12 May, 2011:
82
- * Changed name and all references of this project from 'fitbit' to 'fitgem'
83
- * 11 April, 2011:
84
- * Fixed an issue where blank user id's are used and an error is thrown.
85
- * Added support for creating/removing subscriptions (this support is experimental for now, more tests coming)
86
- * 24 March, 2011:
87
- * Added logging of activities and foods
88
- * Added ability to add favorite activities and foods
89
- * Added ability to delete logged activities and foods, and remove favorite activities and foods
90
- * Refactored data_by_time_range for more testability
91
- * Added ability to query devices
92
- * 19 March, 2011:
93
- * Updated auth client to support first-time auth and reconnections (if you have previously been authorized and received token/secret).
94
- * Added 'named' retrieval of activities and foods (recent_, favorite_, frequent_)
95
- * Added ability to log weight back to the site using the API
96
- * 18 March, 2001: First revision. Supports the auth process via oauth, and retrieval of user info and activities.
34
+ It turns out that Fitbit.com does not want people to create libraries or applications that incorporate the name 'fitbit'. So, on May 12th, 2011 I changed the name of the project/gem from 'fitbit' to 'fitgem'.
97
35
 
98
- # Notice #
36
+ # Notice
99
37
 
100
38
  To be clear: __I am not employed by fitbit.com__. I created this library to assist other ruby developers in creating interesting applications on top of fitbit.com's data store and device data stream.
101
39
 
@@ -113,4 +51,4 @@ The Fitbit REST API is in BETA right now, and so it will quite likely change ove
113
51
 
114
52
  ### Copyright ###
115
53
 
116
- Copyright (c) 2011 Zachery Moneypenny. See LICENSE.txt for further details.
54
+ Copyright (c) 2011 Zachery Moneypenny. See LICENSE for further details.
data/Rakefile CHANGED
@@ -1,2 +1,13 @@
1
1
  require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+
2
4
  Bundler::GemHelper.install_tasks
5
+
6
+ desc 'Default: run specs.'
7
+ task :default => :spec
8
+
9
+ desc "Run specs"
10
+ RSpec::Core::RakeTask.new do |t|
11
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
12
+ # Put spec opts in a file named .rspec in root
13
+ end
@@ -0,0 +1,65 @@
1
+ # @markup markdown
2
+ # @author Zachery Moneypenny
3
+
4
+ # fitgem changelog
5
+
6
+ ## v0.4.0
7
+
8
+ #### 2011-11-29 Zachery Moneypenny <fitgem@whazzmaster.com>
9
+
10
+ * Added YARD documentation to thoroughly document code
11
+ * DEPRECATED: <tt>Fitgem::Client#log_weight</tt> method, use <tt>Fitgem::Client#log_body_measurements</tt> instead.
12
+ The new method allows you to log more than weight (bicep size, body fat %, etc.)
13
+ * Added <tt>Fitgem::FoodFormType</tt> to be used in calls to <tt>Fitgem::Client#create_food</tt>
14
+ * Added <tt>Fitgem::Client#log_sleep</tt> to log sleep data to fitbit
15
+ * Added <tt>Fitgem::Client#delete_sleep_log</tt> to delete previously logged sleep data
16
+ * Added <tt>Fitgem::Client#activities</tt> method to get a list of all activities
17
+ * Added <tt>Fitgem::Client#activity_statistics</tt> method to get statistics for all logged activities
18
+ * Added to documentation of supported endpoints for <tt>Fitgem::Client#data_by_time_range</tt>
19
+ * Added unit tests for parameter validation for many methods
20
+ * Overhauled notifications methods, including extensive documentation,
21
+ unit tests, refactoring, and a couple of bug fixes. These methods now
22
+ return both the HTTP status code and the JSON response body. See https://wiki.fitbit.com/display/API/Subscriptions-API
23
+ for information on how to interpret each of the error codes.
24
+ * Added fitgem to travis-ci for continuous integration (http://travis-ci.org/#!/whazzmaster/fitgem)
25
+ * Added fitgem to rubydoc.info (http://rubydoc.info/github/whazzmaster/fitgem/master/frames)
26
+ * Updated README
27
+ * Moved OAuth documentation from the README to the [fitgem wiki](https://github.com/whazzmaster/fitgem/wiki/The-OAuth-Process)
28
+
29
+ ## v0.3.6
30
+
31
+ #### 2011-07-12 Zachery Moneypenny <fitgem@whazzmaster.com>
32
+
33
+ * Added friends support (get friends, friend leaderboard, create invites, accept/reject invites)
34
+ * Added water support (get water data per date)
35
+ * Added sleep support (get sleep data per date)
36
+ * Added ability to update user profile information
37
+
38
+ ## Previous Releases
39
+
40
+ #### 2011-05-11 Zachery Moneypenny <fitgem@whazzmaster.com>
41
+
42
+ * Changed name and all references of this project from 'fitbit' to 'fitgem'
43
+
44
+ #### 2011-04-11 Zachery Moneypenny <fitgem@whazzmaster.com>
45
+
46
+ * Fixed an issue where blank user id's are used and an error is thrown.
47
+ * Added support for creating/removing subscriptions (this support is experimental for now, more tests coming)
48
+
49
+ #### 2011-03-24 Zachery Moneypenny <fitgem@whazzmaster.com>
50
+
51
+ * Added logging of activities and foods
52
+ * Added ability to add favorite activities and foods
53
+ * Added ability to delete logged activities and foods, and remove favorite activities and foods
54
+ * Refactored data_by_time_range for more testability
55
+ * Added ability to query devices
56
+
57
+ #### 2011-03-19 Zachery Moneypenny <fitgem@whazzmaster.com>
58
+
59
+ * Updated auth client to support first-time auth and reconnections (if you have previously been authorized and received token/secret).
60
+ * Added 'named' retrieval of activities and foods (recent_, favorite_, frequent_)
61
+ * Added ability to log weight back to the site using the API
62
+
63
+ #### 2011-03-18 Zachery Moneypenny <fitgem@whazzmaster.com>
64
+
65
+ * First revision. Supports the auth process via oauth, and retrieval of user info and activities.
@@ -12,24 +12,31 @@ Gem::Specification.new do |s|
12
12
  s.email = ["fitgem@whazzmaster.com"]
13
13
  s.homepage = "http://github.com/whazzmaster/fitgem"
14
14
  s.summary = %q{OAuth client library to the data on fitbit.com}
15
- s.description = %q{A client library to send and retrieve workout/weight data from fitbit.com}
15
+ s.description = %q{A client library to send and retrieve workout, weight, and diet data from fitbit.com}
16
16
 
17
17
  s.rubyforge_project = "fitgem"
18
+
18
19
  s.add_dependency "oauth"
20
+ s.add_development_dependency "rake"
19
21
  s.add_development_dependency "rspec"
20
22
  s.add_development_dependency "guard"
21
23
  s.add_development_dependency "guard-rspec"
22
24
  s.add_development_dependency "rb-fsevent"
23
25
  s.add_development_dependency "growl"
26
+ s.add_development_dependency "yard"
27
+ s.add_development_dependency "rdiscount"
24
28
 
25
29
  s.files = [
26
30
  '.gitignore',
27
31
  '.rvmrc',
32
+ '.yardopts',
33
+ '.travis.yml',
28
34
  'Gemfile',
29
35
  'Guardfile',
30
36
  'LICENSE',
31
37
  'README.md',
32
38
  'Rakefile',
39
+ 'changelog.md',
33
40
  'fitgem.gemspec',
34
41
  'lib/fitgem.rb',
35
42
  'lib/fitgem/version.rb',
@@ -38,6 +45,7 @@ Gem::Specification.new do |s|
38
45
  'lib/fitgem/client.rb',
39
46
  'lib/fitgem/devices.rb',
40
47
  'lib/fitgem/errors.rb',
48
+ 'lib/fitgem/food_form.rb',
41
49
  'lib/fitgem/foods.rb',
42
50
  'lib/fitgem/friends.rb',
43
51
  'lib/fitgem/helpers.rb',
@@ -48,11 +56,17 @@ Gem::Specification.new do |s|
48
56
  'lib/fitgem/users.rb',
49
57
  'lib/fitgem/water.rb',
50
58
  'spec/fitgem_spec.rb',
51
- 'spec/spec_helper.rb'
59
+ 'spec/spec_helper.rb',
60
+ 'spec/fitgem_notifications_spec.rb',
61
+ 'spec/fitgem_helper_spec.rb',
62
+ 'spec/fitgem_constructor_spec.rb'
52
63
  ]
53
64
  s.test_files = [
54
65
  'spec/fitgem_spec.rb',
55
- 'spec/spec_helper.rb'
66
+ 'spec/spec_helper.rb',
67
+ 'spec/fitgem_notifications_spec.rb',
68
+ 'spec/fitgem_helper_spec.rb',
69
+ 'spec/fitgem_constructor_spec.rb'
56
70
  ]
57
71
  s.require_paths = ["lib"]
58
72
  end
@@ -1,7 +1,9 @@
1
1
  require 'json'
2
2
  require 'oauth'
3
-
4
3
  require 'fitgem/client'
5
4
 
5
+ # Read/write data with the {http://www.fitbit.com Fitbit.com} OAuth API.
6
+ #
7
+ # @author Zachery Moneypenny
6
8
  module Fitgem
7
9
  end
@@ -1,49 +1,109 @@
1
1
  module Fitgem
2
2
  class Client
3
-
4
3
  # ==========================================
5
4
  # Activity Retrieval Methods
6
5
  # ==========================================
6
+
7
+ # Get a list of all activities
8
+ #
9
+ # The returned list includes all public activities plus any
10
+ # activities that the current user created and logged
11
+ #
12
+ # @return [Hash] Hash tree of all activities
13
+ def activities
14
+ get("/activities.json")
15
+ end
16
+
17
+ # Get all of the logged activities on the supplied date
18
+ #
19
+ # @param [Datetime] date the date to retrieve logged activities for
20
+ # @return [Hash] the activities logged on the supplied date
7
21
  def activities_on_date(date)
8
22
  get("/user/#{@user_id}/activities/date/#{format_date(date)}.json")
9
23
  end
10
24
 
25
+ # Get a list of the frequently logged activities
26
+ #
27
+ # @return [Array] list of frequently logged activities
11
28
  def frequent_activities()
12
29
  get("/user/#{@user_id}/activities/frequent.json")
13
30
  end
14
31
 
32
+ # Get a list of recently logged activities
33
+ #
34
+ # @return [Array] list of of recently-logged activities
15
35
  def recent_activities()
16
36
  get("/user/#{@user_id}/activities/recent.json")
17
37
  end
18
38
 
39
+ # Get a list of activities marked as favorite by the user
40
+ #
41
+ # @return [Array] list of user-defined favorite activities
19
42
  def favorite_activities()
20
43
  get("/user/#{@user_id}/activities/favorite.json")
21
44
  end
22
45
 
23
- def activity(id, options ={})
46
+ # Get the detailed information for a single activity
47
+ #
48
+ # @param [Integer, String] id The ID of the activity. This ID is
49
+ # typcially retrieved by browsing or searching the list of all
50
+ # available activities. Must be a numerical ID in either Integer
51
+ # or String format.
52
+ # @return [Hash] a hash structure containing detailed information
53
+ # about the activity
54
+ def activity(id)
24
55
  get("/activities/#{id}.json")
25
56
  end
26
57
 
58
+ # Get the activity statistics for the current user
59
+ #
60
+ # @return [Hash] Hash containing statistics for the activities that
61
+ # the user has logged
62
+ def activity_statistics
63
+ get("/user/#{@user_id}/activities.json")
64
+ end
65
+
27
66
  # ==========================================
28
67
  # Activity Update Methods
29
68
  # ==========================================
30
69
 
31
- # The following values are REQUIRED when logging an activity:
32
- # options[:activityId] => The id of the activity, directory activity or intensity level activity. See activity types at http://wiki.fitbit.com/display/API/API-Log-Activity for more information.
33
- # options[:durationMillis] => Activity duration in milliseconds
34
- # options[:startTime] => Activity start time hours and minutes in the format HH:mm
35
- # options[:date] => Set to today's date when not provided
36
- #
37
- # The following value is REQUIRED when logging directory activity, OPTIONAL otherwise
38
- # options[:distance]
39
- #
40
- # The following values are OPTIONAL when logging an activity:
41
- # options[:distanceUnit] => One of Fitgem::ApiDistanceUnit values
42
- # options[:manualCalories] => Number of calories determined manually
43
- def log_activity(options)
44
- post("/user/#{@user_id}/activities.json", options)
70
+ # Log an activity to fitbit
71
+ #
72
+ # @param [Hash] opts The options used to log the activity for the
73
+ # current user
74
+ #
75
+ # @option opts [String] :activityId The id of the activity, directory activity or intensity
76
+ # level activity. See activity types at {http://wiki.fitbit.com/display/API/API-Log-Activity} for more
77
+ # information. This value OR activityName is REQUIRED for all calls.
78
+ # @option opts [String] :activityName The name of the activity to log. This value OR activityId
79
+ # is REQUIRED for alls.
80
+ #
81
+ # @option opts [String, Integer] :durationMillis Activity duration in milliseconds.
82
+ # Must be a numeric value in Integer or String format. This value is REQUIRED for all calls.
83
+ # @option opts [String] :startTime Activity start time, in the format "HH:mm" using hours
84
+ # and seconds. Thie value is REQUIRED for all calls.
85
+ # @option opts [String] :date Activity date, in "yyyy-MM-dd" format. This value is REQUIRED for all calls.
86
+ #
87
+ # @option opts [String] :distance Distance traveled, a string in the format "X.XX". This value is
88
+ # REQUIRED when logging a directory activity, OPTIONAL otherwise.
89
+ # @option opts [String, Integer] :manualCalories The number of calories to log against the activity. This
90
+ # value is REQUIRED if using the activityName, OPTIONAL otherwise.
91
+ #
92
+ # @option opts [String] :distanceUnit One of {Fitgem::ApiDistanceUnit}. The "steps" units are available
93
+ # only for "Walking" and "Running" directory activities and their intensity levels
94
+ #
95
+ # @return [Hash] A hash with a summary of the logged activity
96
+ def log_activity(opts)
97
+ post("/user/#{@user_id}/activities.json", opts)
45
98
  end
46
99
 
100
+ # Mark an activity as a favorite
101
+ #
102
+ # @param [String, Integer] :activity_id The ID of the activity. This ID is
103
+ # typcially retrieved by browsing or searching the list of all
104
+ # available activities. Must be a numerical ID in either Integer
105
+ # or String format.
106
+ # @return [Hash] Empty hash if successfully marked as a favorite
47
107
  def add_favorite_activity(activity_id)
48
108
  post("/user/#{@user_id}/activities/log/favorite/#{activity_id}.json")
49
109
  end
@@ -52,13 +112,24 @@ module Fitgem
52
112
  # Activity Removal Methods
53
113
  # ==========================================
54
114
 
115
+ # Delete a logged activity
116
+ #
117
+ # @param [String, Integer] :activity_log_id The ID of a previously
118
+ # logged activity. Note that this is NOT the activity ID itself, but
119
+ # the ID of the logging of the activity (returned when you call
120
+ # {#log_activity}).
121
+ # @return [Hash]
55
122
  def delete_logged_activity(activity_log_id)
56
123
  delete("/user/#{@user_id}/activities/#{activity_log_id}.json")
57
124
  end
58
125
 
126
+ # Unmark an activity as a favorite
127
+ #
128
+ # @param [String, Integer] :activity_id The activity ID of the
129
+ # activity to remove as a favorite.
130
+ # @return [Hash]
59
131
  def remove_favorite_activity(activity_id)
60
132
  delete("/user/#{@user_id}/activities/log/favorite/#{activity_id}.json")
61
-
62
133
  end
63
134
  end
64
- end
135
+ end