Class: Fitbit::Client
- Inherits:
-
Object
- Object
- Fitbit::Client
- Defined in:
- lib/fitbit/foods.rb,
lib/fitbit/users.rb,
lib/fitbit/client.rb,
lib/fitbit/weight.rb,
lib/fitbit/devices.rb,
lib/fitbit/helpers.rb,
lib/fitbit/activities.rb,
lib/fitbit/time_range.rb,
lib/fitbit/notifications.rb
Instance Attribute Summary collapse
-
#api_unit_system ⇒ Object
Returns the value of attribute api_unit_system.
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#user_id ⇒ Object
Returns the value of attribute user_id.
Instance Method Summary collapse
-
#activities_on_date(date) ⇒ Object
Activity Retrieval Methods ==========================================.
- #activity(id, options = {}) ⇒ Object
- #add_favorite_activity(activity_id) ⇒ Object
- #add_favorite_food(food_id) ⇒ Object
- #authentication_request_token(options = {}) ⇒ Object
- #authorize(token, secret, options = {}) ⇒ Object
- #construct_date_range_fragment(options) ⇒ Object
- #create_subscription(options = {}) ⇒ Object
-
#data_by_time_range(resource_path, options) ⇒ Object
Gets historical resource data in the time range specified by options param.
-
#delete_logged_activity(activity_log_id) ⇒ Object
Activity Removal Methods ==========================================.
-
#delete_logged_food(food_log_id) ⇒ Object
Food Removal Methods ==========================================.
- #device_info(device_id) ⇒ Object
-
#devices ⇒ Object
Device Retrieval Methods ==========================================.
- #favorite_activities ⇒ Object
- #favorite_foods ⇒ Object
- #find_food(query_string) ⇒ Object
-
#foods_on_date(date) ⇒ Object
Food Retrieval Methods ==========================================.
- #foods_units ⇒ Object
-
#format_date(date) ⇒ Object
Should return date as YYYY-MM-DD.
- #frequent_activities ⇒ Object
- #frequent_foods ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#log_activity(options) ⇒ Object
The following values are REQUIRED when logging an activity: options => The activity id options => Activity duration in milliseconds options => Distance covered during activity date options => Activity start time hours and minutes in the format HH:mm The following values are OPTIONAL when logging an activity: options => set to today’s date when not provided.
-
#log_food(options) ⇒ Object
Send the following required ID’s in the options hash: options => ID of the food to log options => ID of the meal to log the food for options => ID of the unit to log with the food (typically retrieved via a previous call to get Foods (all, recent, frequent, favorite) or Food Units. ) options => Amount consumed of the selected unit; a floating point number options => Log date in the format yyyy-MM-dd.
-
#log_weight(weight, date, options = {}) ⇒ Object
Weight Update Methods ==========================================.
- #recent_activities ⇒ Object
- #recent_foods ⇒ Object
- #reconnect(token, secret) ⇒ Object
- #remove_favorite_activity(activity_id) ⇒ Object
- #remove_favorite_food(food_id) ⇒ Object
- #remove_subscription(options = {}) ⇒ Object
- #request_token(options = {}) ⇒ Object
- #user_info(options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/fitbit/client.rb', line 21 def initialize( = {}) @consumer_key = [:consumer_key] @consumer_secret = [:consumer_secret] @token = [:token] @secret = [:secret] @proxy = [:proxy] @user_id = [:user_id] || "-" @api_unit_system = Fitbit::ApiUnitSystem.US @api_version = "1" end |
Instance Attribute Details
#api_unit_system ⇒ Object
Returns the value of attribute api_unit_system.
18 19 20 |
# File 'lib/fitbit/client.rb', line 18 def api_unit_system @api_unit_system end |
#api_version ⇒ Object
Returns the value of attribute api_version.
17 18 19 |
# File 'lib/fitbit/client.rb', line 17 def api_version @api_version end |
#user_id ⇒ Object
Returns the value of attribute user_id.
19 20 21 |
# File 'lib/fitbit/client.rb', line 19 def user_id @user_id end |
Instance Method Details
#activities_on_date(date) ⇒ Object
Activity Retrieval Methods
7 8 9 |
# File 'lib/fitbit/activities.rb', line 7 def activities_on_date(date) get("/user/#{@user_id}/activities/date/#{format_date(date)}.json") end |
#activity(id, options = {}) ⇒ Object
23 24 25 |
# File 'lib/fitbit/activities.rb', line 23 def activity(id, ={}) get("/activities/#{id}.json") end |
#add_favorite_activity(activity_id) ⇒ Object
42 43 44 |
# File 'lib/fitbit/activities.rb', line 42 def add_favorite_activity(activity_id) post("/user/#{@user_id}/activities/log/favorite/#{activity_id}.json") end |
#add_favorite_food(food_id) ⇒ Object
47 48 49 |
# File 'lib/fitbit/foods.rb', line 47 def add_favorite_food(food_id) post("/user/#{@user_id}/foods/log/favorite/#{food_id}.json") end |
#authentication_request_token(options = {}) ⇒ Object
52 53 54 55 |
# File 'lib/fitbit/client.rb', line 52 def authentication_request_token(={}) consumer.[:authorize_path] = '/oauth/authenticate' request_token() end |
#authorize(token, secret, options = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/fitbit/client.rb', line 32 def (token, secret, = {}) request_token = OAuth::RequestToken.new( consumer, token, secret ) @access_token = request_token.get_access_token() @token = @access_token.token @secret = @access_token.secret @access_token end |
#construct_date_range_fragment(options) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fitbit/time_range.rb', line 40 def construct_date_range_fragment() range_str = "date/" if [:base_date] && [:period] range_str += "#{[:base_date]}/#{[:period]}" elsif [:base_date] && [:end_date] range_str += "#{[:base_date]}/#{[:end_date]}" else raise Fitbit::InvalidTimeRange, "Must supply either base_date and period OR base_date and end_date" end range_str end |
#create_subscription(options = {}) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/fitbit/notifications.rb', line 4 def create_subscription(={}) unless [:type] && [:sleep,:body,:activities,:foods].include?([:type]) raise Error, 'Must include options[:type] (values are :activities, :foods, :sleep, and :body)' end base_url = "/user/#{@user_id}/#{[:type].to_s}/apiSubscriptions" post_subscription(base_url, ) end |
#data_by_time_range(resource_path, options) ⇒ Object
Gets historical resource data in the time range specified by options param. The time range can either be specified by :base_date and :end_date OR by using :base_date and a :period (supported periods are 1d, 7d, 30d, 1w, 1m, 3m, 6m, 1y, max)
Example values for resource_path are below:
Food: /foods/log/caloriesIn
Activity: /activities/log/calories /activities/log/steps /activities/log/distance /activities/log/minutesSedentary /activities/log/minutesLightlyActive /activities/log/minutesFairlyActive /activities/log/minutesVeryActive /activities/log/activeScore /activities/log/activityCalories
Sleep: /sleep/minutesAsleep /sleep/minutesAwake /sleep/awakeningsCount /sleep/timeInBed
Body: /body/weight /body/bmi /body/fat
35 36 37 38 |
# File 'lib/fitbit/time_range.rb', line 35 def data_by_time_range(resource_path, ) range_str = construct_date_range_fragment() get("/user/#{@user_id}#{resource_path}/#{range_str}.json") end |
#delete_logged_activity(activity_log_id) ⇒ Object
Activity Removal Methods
50 51 52 |
# File 'lib/fitbit/activities.rb', line 50 def delete_logged_activity(activity_log_id) delete("/user/#{@user_id}/activities/#{activity_log_id}.json") end |
#delete_logged_food(food_log_id) ⇒ Object
Food Removal Methods
55 56 57 |
# File 'lib/fitbit/foods.rb', line 55 def delete_logged_food(food_log_id) delete("/user/#{@user_id}/foods/log/#{food_log_id}.json") end |
#device_info(device_id) ⇒ Object
12 13 14 |
# File 'lib/fitbit/devices.rb', line 12 def device_info(device_id) get("/user/#{@user_id}/devices/#{device_id}.json") end |
#devices ⇒ Object
Device Retrieval Methods
8 9 10 |
# File 'lib/fitbit/devices.rb', line 8 def devices get("/user/#{@user_id}/devices.json") end |
#favorite_activities ⇒ Object
19 20 21 |
# File 'lib/fitbit/activities.rb', line 19 def favorite_activities() get("/user/#{@user_id}/activities/favorite.json") end |
#favorite_foods ⇒ Object
20 21 22 |
# File 'lib/fitbit/foods.rb', line 20 def favorite_foods() get("/user/#{@user_id}/foods/log/favorite.json") end |
#find_food(query_string) ⇒ Object
28 29 30 |
# File 'lib/fitbit/foods.rb', line 28 def find_food(query_string) get("/foods/search.json?query=#{URI.escape(query_string)}") end |
#foods_on_date(date) ⇒ Object
Food Retrieval Methods
8 9 10 |
# File 'lib/fitbit/foods.rb', line 8 def foods_on_date(date) get("/user/#{@user_id}/foods/log/date/#{format_date(date)}.json") end |
#foods_units ⇒ Object
24 25 26 |
# File 'lib/fitbit/foods.rb', line 24 def foods_units() get("/foods/units.json") end |
#format_date(date) ⇒ Object
Should return date as YYYY-MM-DD
5 6 7 8 9 10 11 12 13 |
# File 'lib/fitbit/helpers.rb', line 5 def format_date(date) if date.is_a? String return date elsif Date === date || Time === date || DateTime === date return date.strftime("%Y-%m-%d") else raise Fitbit::InvalidArgumentError, "Date used must be a date/time object or a string in the format YYYY=MM-DD; current argument is a #{date.class}" end end |
#frequent_activities ⇒ Object
11 12 13 |
# File 'lib/fitbit/activities.rb', line 11 def frequent_activities() get("/user/#{@user_id}/activities/frequent.json") end |
#frequent_foods ⇒ Object
16 17 18 |
# File 'lib/fitbit/foods.rb', line 16 def frequent_foods() get("/user/#{@user_id}/foods/log/frequent.json") end |
#log_activity(options) ⇒ Object
The following values are REQUIRED when logging an activity:
options[:activityId] => The activity id
options[:durationMillis] => Activity duration in milliseconds
options[:distance] => Distance covered during activity date
options[:startTime] => Activity start time hours and minutes in the format HH:mm
The following values are OPTIONAL when logging an activity:
options[:date] => set to today's date when not provided
38 39 40 |
# File 'lib/fitbit/activities.rb', line 38 def log_activity() post("/user/#{@user_id}/activities.json", ) end |
#log_food(options) ⇒ Object
Send the following required ID’s in the options hash:
options[:foodId] => ID of the food to log
options[:mealTypeId] => ID of the meal to log the food for
options[:unitId] => ID of the unit to log with the food
(typically retrieved via a previous call to get Foods (all, recent, frequent, favorite) or Food Units. )
options[:amount] => Amount consumed of the selected unit; a floating point number
options[:date] => Log date in the format yyyy-MM-dd
43 44 45 |
# File 'lib/fitbit/foods.rb', line 43 def log_food() post("/user/#{@user_id}/foods/log.json", ) end |
#log_weight(weight, date, options = {}) ⇒ Object
Weight Update Methods
8 9 10 |
# File 'lib/fitbit/weight.rb', line 8 def log_weight(weight, date, ={}) post("/user/#{@user_id}/body/weight.json", .merge(:weight => weight, :date => format_date(date))) end |
#recent_activities ⇒ Object
15 16 17 |
# File 'lib/fitbit/activities.rb', line 15 def recent_activities() get("/user/#{@user_id}/activities/recent.json") end |
#recent_foods ⇒ Object
12 13 14 |
# File 'lib/fitbit/foods.rb', line 12 def recent_foods() get("/user/#{@user_id}/foods/log/recent.json") end |
#reconnect(token, secret) ⇒ Object
42 43 44 45 46 |
# File 'lib/fitbit/client.rb', line 42 def reconnect(token, secret) @token = token @secret = secret access_token end |
#remove_favorite_activity(activity_id) ⇒ Object
54 55 56 57 |
# File 'lib/fitbit/activities.rb', line 54 def remove_favorite_activity(activity_id) delete("/user/#{@user_id}/activities/log/favorite/#{activity_id}.json") end |
#remove_favorite_food(food_id) ⇒ Object
59 60 61 |
# File 'lib/fitbit/foods.rb', line 59 def remove_favorite_food(food_id) delete("/user/#{@user_id}/foods/favorite/#{food_id}.json") end |
#remove_subscription(options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/fitbit/notifications.rb', line 12 def remove_subscription(={}) unless [:type] && [:sleep,:body,:activities,:foods].include?([:type]) raise Error, 'Must include options[:type] (values are :activities, :foods, :sleep, and :body)' end unless [:subscription_id] raise Error, "Must include options[:subscription_id] to delete a subscription" end base_url = "/user/#{@user_id}/#{[:type].to_s}/apiSubscriptions" url = finalize_subscription_url(base_url, ) headers = {} headers['X-Fitbit-Subscriber-Id'] = [:subscriber_id] if [:subscriber_id] begin delete(url, headers) rescue TypeError # Deleting a subscription returns a nil response, which causes a TypeError # when the oauth library tries to parse it. end end |
#request_token(options = {}) ⇒ Object
48 49 50 |
# File 'lib/fitbit/client.rb', line 48 def request_token(={}) consumer.get_request_token() end |
#user_info(options = {}) ⇒ Object
4 5 6 |
# File 'lib/fitbit/users.rb', line 4 def user_info( = {}) get("/user/#{@user_id}/profile.json") end |