Class: RubyFitbit
- Inherits:
-
Object
- Object
- RubyFitbit
- Defined in:
- lib/ruby-fitbit.rb
Instance Attribute Summary collapse
-
#logged_in ⇒ Object
readonly
TODO change tests so reader isn’t needed.
Instance Method Summary collapse
- #get_activity_score_data(date = Time.now) ⇒ Object
- #get_aggregated_data(start_date = Time.now, end_date = Time.now) ⇒ Object
- #get_avg_data(start_date = Time.now, end_date = Time.now) ⇒ Object
- #get_calorie_data(date = Time.now) ⇒ Object
- #get_data(date = Time.now) ⇒ Object
- #get_eaten_calories(date = Time.now) ⇒ Object
- #get_fitbit_date_format(date) ⇒ Object
- #get_food_items(food = "Coffe") ⇒ Object
- #get_graph_data(graph_type = 'intradaySteps', date = Time.now, data_version = '2108') ⇒ Object
- #get_minutes_from_time(str) ⇒ Object
- #get_steps_data(date = Time.now) ⇒ Object
- #get_unit_id_for_unit(unit) ⇒ Object
-
#initialize(email, pass) ⇒ RubyFitbit
constructor
A new instance of RubyFitbit.
- #login ⇒ Object
- #submit_food_log(options = {}) ⇒ Object
- #submit_weight_log(options = {}) ⇒ Object
Constructor Details
#initialize(email, pass) ⇒ RubyFitbit
Returns a new instance of RubyFitbit.
11 12 13 14 15 16 17 |
# File 'lib/ruby-fitbit.rb', line 11 def initialize(email, pass) @email = email @pass = pass @agent = Mechanize.new #{|a| a.log = Logger.new(STDERR) } #turn on if debugging @logged_in = false @cached_data = {} end |
Instance Attribute Details
#logged_in ⇒ Object (readonly)
TODO change tests so reader isn’t needed
9 10 11 |
# File 'lib/ruby-fitbit.rb', line 9 def logged_in @logged_in end |
Instance Method Details
#get_activity_score_data(date = Time.now) ⇒ Object
234 235 236 |
# File 'lib/ruby-fitbit.rb', line 234 def get_activity_score_data(date = Time.now) get_graph_data('intradayActiveScore',date) end |
#get_aggregated_data(start_date = Time.now, end_date = Time.now) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/ruby-fitbit.rb', line 184 def get_aggregated_data(start_date = Time.now, end_date = Time.now) data = {} formatted_date = get_fitbit_date_format(end_date) data[formatted_date] = get_data(end_date) date = end_date + (24 * 60 * 60) while date < start_date formatted_date = get_fitbit_date_format(date) data[formatted_date] = get_data(date) date = date + (24 * 60 * 60) end data end |
#get_avg_data(start_date = Time.now, end_date = Time.now) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/ruby-fitbit.rb', line 199 def get_avg_data(start_date = Time.now, end_date = Time.now) data = {} data['calories'] = 0 data['steps'] = 0 data['miles_walked'] = 0 data['sedentary_active_in_minutes'] = 0 data['lightly_active_in_minutes'] = 0 data['fairly_active_in_minutes'] = 0 data['very_active_in_minutes'] = 0 days = 0 days_data = get_aggregated_data(start_date, end_date) days_data.keys.each do |key| days += 1 current_data = days_data[key] data.keys.each do |stat| data[stat] += current_data[stat].to_f end end data.keys.each do |key| data[key] = (data[key]/days) end data end |
#get_calorie_data(date = Time.now) ⇒ Object
230 231 232 |
# File 'lib/ruby-fitbit.rb', line 230 def get_calorie_data(date = Time.now) get_graph_data('intradayCaloriesBurned',date) end |
#get_data(date = Time.now) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ruby-fitbit.rb', line 146 def get_data(date = Time.now) login date = get_fitbit_date_format(date).gsub('-','/') return @cached_data[date] if @cached_data[date] page = @agent.get "https://www.fitbit.com/#{date}" data = {} data['calories'] = 0 data['steps'] = 0 data['miles_walked'] = 0.0 page.search("//div[@class='data']").each do |datadiv| if datadiv.text.match(/calories burned$/) data['calories'] = datadiv.search("span").first.text.to_i elsif datadiv.text.match(/calories eaten/) data['calories_eaten'] = datadiv.search("span").first.text.to_i elsif datadiv.text.match(/steps taken/) data['steps'] = datadiv.search("span").first.text.to_i elsif datadiv.text.match(/miles traveled/) data['miles_walked'] = datadiv.search("span").first.text.to_f end end data['sedentary_active'] = page.search("//div[@class='sedentary caption']/div[@class='number']").text.strip data['lightly_active'] = page.search("//div[@class='lightly caption']/div[@class='number']").text.strip data['fairly_active'] = page.search("//div[@class='caption fairly']/div[@class='number']").text.strip data['very_active'] = page.search("//div[@class='caption very']/div[@class='number']").text.strip data['sedentary_active_in_minutes'] = get_minutes_from_time(data['sedentary_active']) data['lightly_active_in_minutes'] = get_minutes_from_time(data['lightly_active']) data['fairly_active_in_minutes'] = get_minutes_from_time(data['fairly_active']) data['very_active_in_minutes'] = get_minutes_from_time(data['very_active']) @cached_data[date] = data data end |
#get_eaten_calories(date = Time.now) ⇒ Object
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ruby-fitbit.rb', line 135 def get_eaten_calories(date = Time.now) login date = get_fitbit_date_format(date).gsub('-','/') page = @agent.get "https://www.fitbit.com/foods/log/#{date}" calories_data = page.search("//div[@id='dailyTotals']").first calories_xml = calories_data.to_xml calories_text = calories_data.text {:calories_xml => calories_xml, :calories_text => calories_text} end |
#get_fitbit_date_format(date) ⇒ Object
267 268 269 270 |
# File 'lib/ruby-fitbit.rb', line 267 def get_fitbit_date_format(date) #fitbit date format expects like so: 2010-06-24 date = date.strftime("%Y-%m-%d") end |
#get_food_items(food = "Coffe") ⇒ Object
128 129 130 131 132 133 |
# File 'lib/ruby-fitbit.rb', line 128 def get_food_items(food="Coffe") login result = @agent.get "http://www.fitbit.com/solr/food/select?q=#{food}&wt=foodjson&qt=food" foods = JSON.parse(result.body).first[1]["foods"] foods end |
#get_graph_data(graph_type = 'intradaySteps', date = Time.now, data_version = '2108') ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/ruby-fitbit.rb', line 238 def get_graph_data(graph_type = 'intradaySteps', date = Time.now, data_version = '2108') login date = get_fitbit_date_format(date) params = {:userId => @userId, :type => graph_type, :version => "amchart", :dataVersion => data_version, :chart_Type => "column2d", :period => "1d", :dateTo => date} params = Mechanize::Util.build_query_string(params) uri = "http://www.fitbit.com/graph/getGraphData?#{params}" page = @agent.get uri doc = Nokogiri::HTML(page.content) minutes_segment = 0 chart_data = {} doc.xpath('//data/chart/graphs/graph/value').each do |ele| moment = Time.parse(date)+(5*60*minutes_segment) minutes_segment += 1 chart_data[moment] = ele.child.text end chart_data end |
#get_minutes_from_time(str) ⇒ Object
272 273 274 275 276 277 278 279 280 |
# File 'lib/ruby-fitbit.rb', line 272 def get_minutes_from_time(str) if m = str.to_s.strip.match(/^((\d+)hrs?)? ?((\d+)min)?$/) hrs = m[1].to_i * 60 mins = m[3].to_i return (hrs + mins) end return 0 end |
#get_steps_data(date = Time.now) ⇒ Object
226 227 228 |
# File 'lib/ruby-fitbit.rb', line 226 def get_steps_data(date = Time.now) get_graph_data('intradaySteps',date) end |
#get_unit_id_for_unit(unit) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ruby-fitbit.rb', line 99 def get_unit_id_for_unit(unit) unit_id = nil unit_type = unit.match(/\d+ (.*)/)[1] type_map = {'oz' => '226', 'lb' => '180', 'gram' => '147', 'kilogram' => '389', 'roll' => '290', 'serving' => '304', 'link' => '188', 'piece' => '251', 'fl oz' => '128', 'ml' => '209', 'tsp' => '364', 'tbsp' => '349', 'cup' => '91', 'pint' => '256', 'slice' => '311', 'liter' => '189', 'quart' => '279', 'entree' => '117', 'portion' => '270' } unit_id = type_map[unit_type] unit_id end |
#login ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ruby-fitbit.rb', line 19 def login unless @logged_in page = @agent.get 'https://www.fitbit.com/login' form = page.forms.first form.email = @email form.password = @pass page = @agent.submit(form, form..first) @userId = page.search("//div[@class='accountLinks']").search("a")[0]['href'].gsub('/user/','') # @agent.cookie_jar.jar["www.fitbit.com"]['/']['uid'].value # @agent.cookie_jar.jar["www.fitbit.com"]['/']['sid'].value @logged_in = true end end |
#submit_food_log(options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ruby-fitbit.rb', line 37 def submit_food_log( = {}) login date = .fetch(:date) {Time.now} date = get_fitbit_date_format(date) meal_type = .fetch(:meal_type){'7'} food_id = [:food_id] food = [:food] raise "food_id or food required to submit food log" unless food_id || food unless food_id food_recommendation = get_food_items(food) if food_recommendation.length > 0 food_recommendation = food_recommendation.first food = food_recommendation['name'] food_id = food_recommendation['id'] end end unit_id = [:unit_id] unit = [:unit] raise "unit_id or unit required to submit food log" unless unit_id || unit unless unit_id unit_id = get_unit_id_for_unit(unit) end page = @agent.get 'http://www.fitbit.com/foods/log' form = page.forms[1] form.action="/foods/log/foodLog?apiFormat=htmljson&log=on&date=#{date}" form.foodId = food_id form.foodselectinput = food form.unitId = unit_id form.quantityselectinput = unit form.quantityConsumed = unit form.mealTypeId = meal_type result = @agent.submit(form, form..first) end |
#submit_weight_log(options = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ruby-fitbit.rb', line 76 def submit_weight_log( = {}) login weight_units = {:lbs => "US", :stone => "UK", :kg => "METRIC"} date = .fetch(:date) {Time.now} date = get_fitbit_date_format(date) unit = [:unit] unit_id = weight_units[unit.to_sym] raise "#{unit} isn't one of Fitbit's units. Try #{weight_units.keys.join(", ")} instead." unless unit_id page = @agent.get 'http://www.fitbit.com/weight' form = page.forms[1] form.action="/measure/measurements?apiFormat=json&log=on&date=#{date}" form.send([:unit].to_sym, [:weight]) form.weightState = unit_id form.bodyFat = [:percentage_fat] result = @agent.submit(form, form..first) end |