Class: Account
- Inherits:
-
Object
- Object
- Account
- Defined in:
- lib/myfitnesspal_stats/account.rb
Instance Method Summary collapse
-
#initialize(username = nil, password = nil) ⇒ Account
constructor
attr_reader :username, :password.
-
#login ⇒ Object
—- initialize.
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. = true end end |
Instance Method Details
#login ⇒ Object
—- 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 login # Go to homepage, click log in, and submit the form home_page = @web_crawler.get('http://www.myfitnesspal.com/') login_form = home_page.form_with(id: "fancy_login") login_form['username'] = @username login_form['password'] = @password current_page = login_form.submit = @web_crawler..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 |