Class: Day

Inherits:
Object
  • Object
show all
Defined in:
lib/myfitnesspal_stats/day.rb

Instance Method Summary collapse

Constructor Details

#initialize(year, month, day) ⇒ Day

Returns a new instance of Day.



4
5
6
7
8
9
10
11
12
13
# File 'lib/myfitnesspal_stats/day.rb', line 4

def initialize(year, month, day)
  @date = Date.new(year, month, day)

  @login_page = 'http://www.myfitnesspal.com'

  @web_crawler = Mechanize.new do |web_crawler|
    web_crawler.cookie_jar.load('cookies.yml')
    web_crawler.follow_meta_refresh = true
  end
end

Instance Method Details

#nutrition_totalsObject

—- initialize



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/myfitnesspal_stats/day.rb', line 15

def nutrition_totals
  diary = @web_crawler.get("#{@login_page}/food/diary/#{@username}?date=
    #{@date}")
  totals_table = diary.search('tr.total')

  # Find which nutrients are being tracked, and put them into an array
  nutrients = diary.search('tfoot').search('td.alt').text.split(
    /(?<=[a-z])(?=[A-Z])/).to_a

  nutrient_totals = Hash.new
  nutrient_totals[:Date] = @date.strftime("%A, %e %B %Y")

  # Go through the nutrients table, find the values for its respective column
  nutrients.each_with_index do |nutrient, index|
    todays_total = totals_table.search('td')[index+1].text.strip
    daily_goal = totals_table.search('td')[index+9].text.strip
    difference = totals_table.search('td')[index+17].text.strip

    nutrient_totals[nutrient.to_sym] = todays_total, daily_goal, difference
  end

  nutrient_totals
end