Class: Fitbit::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options = {})
  @consumer_key = options[:consumer_key]
  @consumer_secret = options[:consumer_secret]
  @token = options[:token]
  @secret = options[:secret]
  @proxy = options[:proxy]
  @user_id = options[:user_id] || "-"
  @api_unit_system = Fitbit::ApiUnitSystem.US
  @api_version = "1"
end

Instance Attribute Details

#api_unit_systemObject

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_versionObject

Returns the value of attribute api_version.



17
18
19
# File 'lib/fitbit/client.rb', line 17

def api_version
  @api_version
end

#user_idObject

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, options ={})
  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(options={})
  consumer.options[:authorize_path] = '/oauth/authenticate'
  request_token(options)
end

#authorize(token, secret, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/fitbit/client.rb', line 32

def authorize(token, secret, options = {})
  request_token = OAuth::RequestToken.new(
    consumer, token, secret
  )
  @access_token = request_token.get_access_token(options)
  @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(options)
  range_str = "date/"
  if options[:base_date] && options[:period]
    range_str += "#{options[:base_date]}/#{options[:period]}"
  elsif options[:base_date] && options[:end_date]
    range_str += "#{options[:base_date]}/#{options[: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(options={})
  unless options[:type] && [:sleep,:body,:activities,:foods].include?(options[:type])
    raise Error, 'Must include options[:type] (values are :activities, :foods, :sleep, and :body)'
  end
  base_url = "/user/#{@user_id}/#{options[:type].to_s}/apiSubscriptions"
  post_subscription(base_url, options)
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, options)
  range_str = construct_date_range_fragment(options)
  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

#devicesObject

Device Retrieval Methods


8
9
10
# File 'lib/fitbit/devices.rb', line 8

def devices
  get("/user/#{@user_id}/devices.json")
end

#favorite_activitiesObject



19
20
21
# File 'lib/fitbit/activities.rb', line 19

def favorite_activities()
  get("/user/#{@user_id}/activities/favorite.json")
end

#favorite_foodsObject



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_unitsObject



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_activitiesObject



11
12
13
# File 'lib/fitbit/activities.rb', line 11

def frequent_activities()
  get("/user/#{@user_id}/activities/frequent.json")
end

#frequent_foodsObject



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(options)
  post("/user/#{@user_id}/activities.json", options)
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(options)
  post("/user/#{@user_id}/foods/log.json", options)
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, options={})
  post("/user/#{@user_id}/body/weight.json", options.merge(:weight => weight, :date => format_date(date)))
end

#recent_activitiesObject



15
16
17
# File 'lib/fitbit/activities.rb', line 15

def recent_activities()
  get("/user/#{@user_id}/activities/recent.json")
end

#recent_foodsObject



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(options={})
  unless options[:type] && [:sleep,:body,:activities,:foods].include?(options[:type])
    raise Error, 'Must include options[:type] (values are :activities, :foods, :sleep, and :body)'
  end
  unless options[:subscription_id]
    raise Error, "Must include options[:subscription_id] to delete a subscription"
  end
  base_url = "/user/#{@user_id}/#{options[:type].to_s}/apiSubscriptions"
  url = finalize_subscription_url(base_url, options)
  headers = {}
  headers['X-Fitbit-Subscriber-Id'] = options[:subscriber_id] if options[: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(options={})
  consumer.get_request_token(options)
end

#user_info(options = {}) ⇒ Object



4
5
6
# File 'lib/fitbit/users.rb', line 4

def (options = {})    
  get("/user/#{@user_id}/profile.json")
end