Class: Account

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

Instance Method Summary collapse

Constructor Details

#initialize(username = nil, password = nil) ⇒ Account

attr_reader :username, :password



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

def initialize(username = nil, password = nil)
  @username = username
  @password = password

  @web_crawler = Mechanize.new do |web_crawler|
    web_crawler.follow_meta_refresh = true
  end
end

Instance Method Details

#loginObject

—- initialize



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

def 
  # Go to homepage, click log in, and submit the form
  home_page = @web_crawler.get('http://www.myfitnesspal.com/')
   = home_page.form_with(id: "fancy_login")
  ['username'] = @username
  ['password'] = @password
  current_page = .submit
   = @web_crawler.cookie_jar.save('cookies.yml', session: true)

  # Checks to see if there was an error when logging in
  begin
    calories_left = current_page.search('div#calories-remaining-number').text
    return current_page
  rescue StandardError
    flash = current_page.search('p.flash').text.split(' ').to_a
    puts flash
    return false
  end

end