myfitnesspal_stats 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f61a588a5d049eb4fa6de0372d09e6d8483c8908
4
- data.tar.gz: cc5318cd057331c6bb5c0e6c2fc44d308f6318b7
3
+ metadata.gz: a474bdee905c1c3d32159c34be8f2bdebeb89b32
4
+ data.tar.gz: 4c9ea993e7b20e3c2068577df995c4e22e07ee23
5
5
  SHA512:
6
- metadata.gz: f600b1b3dedf6dda633816e3b600b5dd8823816c1cc4b48bd57c1ef8496565094b60c056c7e34e08241c88d464420589ab96077d25fcb5683d1bb995fcb7a541
7
- data.tar.gz: 6b66aa75576fb73394c1943f348ba7bee09df57c6c5e857a81169079945e91cc630ca3d9c9e4efc811d83a32003466005e0451ae47819cfa615bec9610b2e314
6
+ metadata.gz: 46517fec18fda5d8bd9a71bc5ebc125b0eeaa4f84bc53282ca62551f75b87d7ac4e4d9f3c36813eb37ffc399f92ff39bc25a5f9a6e7bd681a0d005ba79ac1875
7
+ data.tar.gz: c78e5132e75d6202639c876fbb3900f350f8658bc1a0c7a086f4136bd4cb1935dfc23ef995ac56cc42b77a843952c79d79460f11a507f592cb920c06194dbae8
@@ -1,25 +1,8 @@
1
1
  require_relative 'myfitnesspal_stats/version'
2
2
  require_relative 'myfitnesspal_stats/account'
3
3
  require_relative 'myfitnesspal_stats/scraper'
4
+ require_relative 'myfitnesspal_stats/day'
4
5
  require 'mechanize'
5
6
 
6
- # Possible gems to use:
7
- # - markaby for generating html
8
- # - RedCloth for generating html
9
- # - hpricot or searching within html documents
10
- # - REXML for parsing XML
11
-
12
- =begin
13
- class Nutrition_data
14
- end
15
-
16
- class Weight
17
- end
18
-
19
- class Diary
20
- end
21
- =end
22
-
23
- module MyfitnesspalStats
24
-
7
+ module MyfitnesspalStats
25
8
  end
@@ -9,7 +9,7 @@ class Account
9
9
  @web_crawler = Mechanize.new do |web_crawler|
10
10
  web_crawler.follow_meta_refresh = true
11
11
  end
12
- end
12
+ end # ---- initialize
13
13
 
14
14
  def login
15
15
  # Go to homepage, click log in, and submit the form
@@ -23,7 +23,6 @@ class Account
23
23
  # Checks to see if there was an error when logging in
24
24
  begin
25
25
  calories_left = current_page.search('div#calories-remaining-number').text
26
- puts "#{@username} has successfully logged in!"
27
26
  return current_page
28
27
  rescue StandardError
29
28
  flash = current_page.search('p.flash').text.split(' ').to_a
@@ -31,5 +30,5 @@ class Account
31
30
  return false
32
31
  end
33
32
 
34
- end # Account.login
35
- end # class Account
33
+ end # ---- login
34
+ end # ---- class Account
@@ -3,5 +3,43 @@ require 'mechanize'
3
3
  class Day
4
4
  def initialize(year, month, day)
5
5
  @date = Date.new(year, month, day)
6
+
7
+ @login_page = 'http://www.myfitnesspal.com'
8
+
9
+ @web_crawler = Mechanize.new do |web_crawler|
10
+ web_crawler.cookie_jar.load('cookies.yml')
11
+ web_crawler.follow_meta_refresh = true
12
+ end
13
+ end # ---- initialize
14
+
15
+ def nutrition_totals
16
+ diary = @web_crawler.get("#{@login_page}/food/diary/#{@username}?date=
17
+ #{@date}")
18
+ totals_table = diary.search('tr.total')
19
+
20
+ # Find which nutrients are being tracked, and put them into an array
21
+ nutrients = diary.search('tfoot').search('td.alt').text.split(
22
+ /(?<=[a-z])(?=[A-Z])/).to_a
23
+
24
+ nutrient_totals = Hash.new
25
+ nutrient_totals["Date"] = @date.strftime("%A, %B %e, %Y")
26
+
27
+ # Go through the nutrients table, find the values for its respective column
28
+ nutrients.each_with_index do |nutrient, index|
29
+ todays_total = totals_table.search('td')[index+1].text.strip
30
+ daily_goal = totals_table.search('td')[index+9].text.strip
31
+ remaining = totals_table.search('td')[index+17].text.strip
32
+
33
+ nutrient_totals[nutrient] = todays_total, daily_goal, remaining
34
+ end
35
+
36
+ nutrient_totals
37
+ end # ---- nutrition_totals
38
+
39
+ # WIP
40
+ def weight
41
+ reports = @web_crawler.get("#{@login_page}/reports/")
42
+ weight_report = reports.search('//optgroup')[0].children[0]
43
+ @web_crawler.click(weight_report)
6
44
  end
7
- end
45
+ end # ---- class Day
@@ -16,33 +16,9 @@ class Scraper
16
16
  web_crawler.cookie_jar.load('cookies.yml')
17
17
  web_crawler.follow_meta_refresh = true
18
18
  end
19
- end # .initialize
19
+ end # ---- initialize
20
20
 
21
- def get_nutrition(year = @date.year, month = @date.month, day = @date.day)
22
- date = Date.new(year, month, day)
23
-
24
- # @login_page.uri already has a trailing slash: /
25
- diary = @web_crawler.get("#{@login_page.uri}food/diary/#{@username}?date=#{date}")
26
- totals_table = diary.search('tr.total')
27
-
28
- # Find which nutrients are being tracked, and put them into an array
29
- nutrients = diary.search('tfoot').search('td.alt').text.split(
30
- /(?<=[a-z])(?=[A-Z])/).to_a
31
-
32
- nutrient_totals = Hash.new
33
- nutrient_totals["Date"] = date.strftime("%A, %B %e, %Y")
34
-
35
- # For each nutrient, iterate through the table rows and get the value in its respective column. Then push the nutrient and value into a hash.
36
- nutrients.each_with_index do |nutrient, index|
37
- todays_total = totals_table.search('td')[index+1].text.strip
38
- daily_goal = totals_table.search('td')[index+9].text.strip
39
- remaining = totals_table.search('td')[index+17].text.strip
40
- #puts "-- #{nutrient}: \n Total: #{todays_total} \n Your goal: #{daily_goal} \n Remaining: #{remaining} \n\n"
41
-
42
- nutrient_totals[nutrient] = todays_total, daily_goal, remaining
43
- end
44
-
45
- nutrient_totals
46
- end
47
-
48
- end
21
+ def get_date(year = @date.year, month = @date.month, day = @date.day)
22
+ day = Day.new(year, month, day)
23
+ end # ---- get_date
24
+ end # ---- class Scraper
@@ -1,3 +1,3 @@
1
1
  module MyfitnesspalStats
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["hgducharme@gmail.com"]
11
11
  spec.summary = %q{Programatically access your daily nutrition from myfitnesspal.}
12
12
  spec.description = %q{A module that replace Myfitnesspal's API. Get access to all of your stats & attributes for nutrition and weight progression.}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/hgducharme/myfitnesspal_stats"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -1,16 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Scraper, :vcr do
4
+
4
5
  describe 'the initialization of a Scraper' do
5
6
  it 'takes a username and password argument successfully' do
6
- @scraper = Scraper.new('bob', '1234')
7
+ @scraper = Scraper.new('rspec_test', '123456')
7
8
  expect(@scraper).to be_an_instance_of(Scraper)
8
9
  end
9
10
  end
10
11
 
11
- describe 'the get_macros method' do
12
+ describe 'the get_nutrition method' do
12
13
  it 'takes a date' do
13
-
14
+
14
15
  end
15
16
 
16
17
  it 'does not fail if no date is given' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: myfitnesspal_stats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hunter Ducharme
@@ -121,7 +121,7 @@ files:
121
121
  - spec/spec_helper.rb
122
122
  - spec/support/vcr_setup.rb
123
123
  - spec/vcr_cassettes/Scraper/the_initialization_of_a_Scraper/takes_a_username_and_password_argument_successfully.yml
124
- homepage: ''
124
+ homepage: https://github.com/hgducharme/myfitnesspal_stats
125
125
  licenses:
126
126
  - MIT
127
127
  metadata: {}