food_info 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.markdown +8 -0
- data/README.markdown +14 -2
- data/TODO.txt +1 -3
- data/lib/food_info/adapters/fat_secret/data/search_results.rb +8 -0
- data/lib/food_info/adapters/fat_secret.rb +4 -1
- data/lib/food_info/errors.rb +1 -0
- data/lib/food_info/version.rb +1 -1
- data/lib/food_info.rb +1 -1
- metadata +37 -63
data/HISTORY.markdown
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 0.0.2 September 11, 2011
|
4
|
+
* Added propagation of errors from data source
|
5
|
+
* API improvement: No need to call <tt>results</tt> on FoodItem.search to access search results
|
6
|
+
|
7
|
+
## 0.0.1 September 10, 2011
|
8
|
+
* Initial release. FatSecret adapter supports both <tt>search</tt> and <tt>details</tt> methods.
|
data/README.markdown
CHANGED
@@ -49,16 +49,28 @@ Now we can search for foods.
|
|
49
49
|
|
50
50
|
(As an aside, I get that pretty, nicely-lined-up console formatting from the remarkably awesome [AwesomePrint Gem](https://github.com/michaeldv/awesome_print)).
|
51
51
|
|
52
|
-
|
52
|
+
#### Pagination
|
53
|
+
|
54
|
+
Search supports pagination via the <tt>page</tt> and <tt>per_page</tt> (max 50) parameters:
|
53
55
|
|
54
56
|
FoodInfo.search('cheese', :page => 2, :per_page => 50)
|
55
57
|
|
58
|
+
#### Access to search results
|
59
|
+
|
60
|
+
You can access the results of the search explicitly:
|
61
|
+
|
62
|
+
FoodInfo.search('cheese').results # => ... lots of results ...
|
63
|
+
|
64
|
+
But the SearchResults class includes Enumerable, so you can also just call enumerable methods on it directly:
|
65
|
+
|
66
|
+
FoodInfo.search('cheese').map(&:name) # => array of names of matching foods
|
67
|
+
|
56
68
|
|
57
69
|
### Nutritional Details
|
58
70
|
|
59
71
|
Once you have a specific food item in mind from the search results, you can retrieve a whole lot of additional information.
|
60
72
|
|
61
|
-
cheddar = FoodInfo.search('cheese').
|
73
|
+
cheddar = FoodInfo.search('cheese').first
|
62
74
|
info = FoodInfo.details( cheddar.id ) # => ... a whole lotta data ...
|
63
75
|
|
64
76
|
General metadata about the cheese includes id, name, kind, and url, which are identical to what you'd get from the <tt>search</tt> method. It also has one or more servings, however, and this is where we finally get our nutrition info.
|
data/TODO.txt
CHANGED
@@ -1,4 +1,2 @@
|
|
1
|
-
*
|
2
|
-
* Better search API (search('cheese'), not search('cheese').results)
|
3
|
-
* Add DB caching
|
1
|
+
* Add DB caching (or is that better left to the client, who can control the caching layer completely?)
|
4
2
|
* It'd be great to have a connection pool, so we could process multiple requests concurrently. I've added a skeleton for it, but actually using it would require a non-blocking HTTP library in HTTParty.
|
@@ -4,6 +4,8 @@ module FoodInfo
|
|
4
4
|
module Data
|
5
5
|
|
6
6
|
class SearchResults < Hashie::Trash
|
7
|
+
include Enumerable
|
8
|
+
|
7
9
|
property :results, :from => :food
|
8
10
|
property :page, :from => :page_number
|
9
11
|
property :per_page, :from => :max_results
|
@@ -23,6 +25,12 @@ module FoodInfo
|
|
23
25
|
self[:results] = [self[:results]] unless self[:results].is_a?(Array)
|
24
26
|
self[:results] = (self[:results] || []).collect {|result| SearchResult.new(result) }
|
25
27
|
end
|
28
|
+
|
29
|
+
# Allow direct enumerable access to search results without calling search('cheese').results.each
|
30
|
+
def each(&block)
|
31
|
+
self[:results].each{|result| block.call(result)}
|
32
|
+
end
|
33
|
+
|
26
34
|
end
|
27
35
|
|
28
36
|
end
|
@@ -37,7 +37,10 @@ module FoodInfo
|
|
37
37
|
|
38
38
|
def query(method, opts = {})
|
39
39
|
query_url = Request.new(method, {:key => @key, :secret => @secret}, opts).signed_request
|
40
|
-
self.class.get( query_url )
|
40
|
+
data = self.class.get( query_url )
|
41
|
+
raise DataSourceException.new(data['error']['message']) if data['error']
|
42
|
+
|
43
|
+
return data
|
41
44
|
end
|
42
45
|
|
43
46
|
end
|
data/lib/food_info/errors.rb
CHANGED
data/lib/food_info/version.rb
CHANGED
data/lib/food_info.rb
CHANGED
@@ -50,8 +50,8 @@ end
|
|
50
50
|
__END__
|
51
51
|
|
52
52
|
FoodInfo.establish_connection(:fat_secret, :key => ENV['KEY'], :secret => ENV['SECRET'])
|
53
|
+
a=FoodInfo.search('cheese', :page => 1, :per_page => 1).results.first
|
53
54
|
a=FoodInfo.search('cheese')
|
54
|
-
a=FoodInfo.search('cheese', :page => 1, :per_page => 1)
|
55
55
|
|
56
56
|
FoodInfo.establish_connection(:fat_secret, :key => ENV['KEY'], :secret => ENV['SECRET'])
|
57
57
|
a=FoodInfo.details("33689")
|
metadata
CHANGED
@@ -1,66 +1,50 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: food_info
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 1
|
10
|
-
version: 0.0.1
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Kali Donovan
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-09-11 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: httparty
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &2156585880 !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
hash: 13
|
29
|
-
segments:
|
30
|
-
- 0
|
31
|
-
- 7
|
32
|
-
- 7
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
33
21
|
version: 0.7.7
|
34
22
|
type: :runtime
|
35
|
-
version_requirements: *id001
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: hashie
|
38
23
|
prerelease: false
|
39
|
-
|
24
|
+
version_requirements: *2156585880
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: hashie
|
27
|
+
requirement: &2156585140 !ruby/object:Gem::Requirement
|
40
28
|
none: false
|
41
|
-
requirements:
|
42
|
-
- -
|
43
|
-
- !ruby/object:Gem::Version
|
44
|
-
hash: 19
|
45
|
-
segments:
|
46
|
-
- 1
|
47
|
-
- 1
|
48
|
-
- 0
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
49
32
|
version: 1.1.0
|
50
33
|
type: :runtime
|
51
|
-
|
52
|
-
|
53
|
-
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2156585140
|
36
|
+
description: Generic Ruby interface to look up nutritional information on food. Design
|
37
|
+
is modular so other adapters can be plugged in, but only data source currently implemented
|
38
|
+
is FatSecret.
|
39
|
+
email:
|
54
40
|
- kali@deviantech.com
|
55
41
|
executables: []
|
56
|
-
|
57
42
|
extensions: []
|
58
|
-
|
59
43
|
extra_rdoc_files: []
|
60
|
-
|
61
|
-
files:
|
44
|
+
files:
|
62
45
|
- .gitignore
|
63
46
|
- Gemfile
|
47
|
+
- HISTORY.markdown
|
64
48
|
- LICENSE
|
65
49
|
- README.markdown
|
66
50
|
- Rakefile
|
@@ -79,36 +63,26 @@ files:
|
|
79
63
|
- lib/food_info/version.rb
|
80
64
|
homepage: https://github.com/deviantech/food_info
|
81
65
|
licenses: []
|
82
|
-
|
83
66
|
post_install_message:
|
84
67
|
rdoc_options: []
|
85
|
-
|
86
|
-
require_paths:
|
68
|
+
require_paths:
|
87
69
|
- lib
|
88
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
71
|
none: false
|
90
|
-
requirements:
|
91
|
-
- -
|
92
|
-
- !ruby/object:Gem::Version
|
93
|
-
|
94
|
-
|
95
|
-
- 0
|
96
|
-
version: "0"
|
97
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
77
|
none: false
|
99
|
-
requirements:
|
100
|
-
- -
|
101
|
-
- !ruby/object:Gem::Version
|
102
|
-
|
103
|
-
segments:
|
104
|
-
- 0
|
105
|
-
version: "0"
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
106
82
|
requirements: []
|
107
|
-
|
108
83
|
rubyforge_project:
|
109
84
|
rubygems_version: 1.8.10
|
110
85
|
signing_key:
|
111
86
|
specification_version: 3
|
112
87
|
summary: API for researching nutritional information of various foods
|
113
88
|
test_files: []
|
114
|
-
|