esha 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +28 -0
- data/Rakefile +6 -0
- data/esha.gemspec +21 -0
- data/lib/esha.rb +7 -0
- data/lib/esha/api.rb +22 -0
- data/lib/esha/food.rb +23 -0
- data/lib/esha/nutrient.rb +22 -0
- data/lib/esha/nutrition.rb +19 -0
- data/lib/esha/version.rb +3 -0
- data/spec/lib/esha/food_spec.rb +27 -0
- data/spec/lib/esha/nutrient_spec.rb +23 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/support/api_key.rb +5 -0
- metadata +100 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jens Balvig
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# Esha
|
2
|
+
|
3
|
+
Esha is a simple wrapper that makes the ESHA nutrition api a little easier to work with.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'esha'
|
10
|
+
|
11
|
+
And then run:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself:
|
16
|
+
|
17
|
+
$ gem install esha
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
Esha::Api.key = '....' # Get an api key from http://developer.esha.com/
|
23
|
+
|
24
|
+
pizza = Esha::Food.search('pizza').first
|
25
|
+
pizza.description # => 'Pizza, hand tossed, cheese, 12"'
|
26
|
+
pizza.product # => "Domino's Pizza"
|
27
|
+
pizza.nutrients.first.summary => "1 Piece -> 187.255 kcal Calories"
|
28
|
+
```
|
data/Rakefile
ADDED
data/esha.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/esha/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jens Balvig"]
|
6
|
+
gem.email = ["jens@balvig.com"]
|
7
|
+
gem.description = %q{Esha is a simple wrapper that makes the ESHA nutrition api a little easier to work with.}
|
8
|
+
gem.summary = %q{Simple wrapper for ESHA nutrition database}
|
9
|
+
gem.homepage = "https://github.com/balvig/esha"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "esha"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Esha::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "httparty"
|
19
|
+
gem.add_development_dependency "rspec"
|
20
|
+
gem.add_development_dependency "rspec-rails"
|
21
|
+
end
|
data/lib/esha.rb
ADDED
data/lib/esha/api.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "httparty"
|
2
|
+
module Esha
|
3
|
+
class Api
|
4
|
+
include HTTParty
|
5
|
+
|
6
|
+
base_uri 'api.esha.com'
|
7
|
+
format :json
|
8
|
+
|
9
|
+
def self.key=(key)
|
10
|
+
default_params apikey: key
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(method, *args)
|
14
|
+
if @attributes.keys.include?(method.to_s)
|
15
|
+
@attributes[method.to_s]
|
16
|
+
else
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
data/lib/esha/food.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module Esha
|
2
|
+
class Food < Api
|
3
|
+
|
4
|
+
def self.search(name)
|
5
|
+
result = get("/foods", query: { query: name})
|
6
|
+
result['items'].map do |r|
|
7
|
+
new(r)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(attributes)
|
12
|
+
@attributes = attributes
|
13
|
+
end
|
14
|
+
|
15
|
+
def nutrients
|
16
|
+
result = self.class.get('/analysis', query: { f0: id })
|
17
|
+
result['results'].map do |r|
|
18
|
+
Nutrient.new(r.merge(result['items'].first))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Esha
|
2
|
+
class Nutrient < Api
|
3
|
+
def initialize(attributes)
|
4
|
+
@attributes = attributes
|
5
|
+
end
|
6
|
+
|
7
|
+
def name
|
8
|
+
result = self.class.get('/nutrients').find {|n|n['id'] == @attributes['nutrient']}
|
9
|
+
"#{result['unit']} #{result['description']}"
|
10
|
+
end
|
11
|
+
|
12
|
+
def food_unit
|
13
|
+
result = self.class.get('/food-units').find {|n|n['id'] == @attributes['unit']}
|
14
|
+
result['description']
|
15
|
+
end
|
16
|
+
|
17
|
+
def summary
|
18
|
+
"#{quantity} #{food_unit} -> #{value} #{name}"
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "httparty"
|
2
|
+
require 'active_support/all'
|
3
|
+
module Esha
|
4
|
+
class Nutrition
|
5
|
+
include HTTParty
|
6
|
+
|
7
|
+
base_uri 'api.esha.com'
|
8
|
+
default_params apikey: 'ekkyhde3bes6m86sv396uj8y'
|
9
|
+
format :json
|
10
|
+
|
11
|
+
def initialize(name)
|
12
|
+
@name = name
|
13
|
+
end
|
14
|
+
|
15
|
+
def go
|
16
|
+
self.class.get("/foods", query: { query: @name})
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/esha/version.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Esha::Food do
|
4
|
+
describe '.search' do
|
5
|
+
it "returns an array of foods" do
|
6
|
+
result = Esha::Food.search('beer')
|
7
|
+
result.size.should == 25
|
8
|
+
result.first.product.should == 'Anheuser-Busch'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#method_missing' do
|
13
|
+
it "maps methods to attributes" do
|
14
|
+
food = Esha::Food.new('id' => '1234', 'product' => 'Beer')
|
15
|
+
food.id.should == '1234'
|
16
|
+
food.product.should == 'Beer'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#nutrients' do
|
21
|
+
it "returns an array of nutrients" do
|
22
|
+
food = Esha::Food.new('id' => 'urn:uuid:a81993c5-7dfa-456e-8248-76a5206352df')
|
23
|
+
food.nutrients.size.should == 1
|
24
|
+
food.nutrients.first.summary.should == '1 Fluid ounce -> 9.145 kcal Calories'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Esha::Nutrient do
|
4
|
+
describe '#name' do
|
5
|
+
it "returns the name of the nutrient" do
|
6
|
+
nutrient = Esha::Nutrient.new('nutrient' => 'urn:uuid:a4d01e46-5df2-4cb3-ad2c-6b438e79e5b9')
|
7
|
+
nutrient.name.should == 'kcal Calories'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#food_unit' do
|
12
|
+
it "returns the food unit of the nutrient" do
|
13
|
+
nutrient = Esha::Nutrient.new('unit' => 'urn:uuid:20346378-1e1f-4d65-8833-6119228e8639')
|
14
|
+
nutrient.food_unit.should == 'Fluid ounce'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#method_missing' do
|
19
|
+
it "maps methods to attributes" do
|
20
|
+
Esha::Nutrient.new('value' => '1234').value.should == '1234'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'esha'
|
2
|
+
|
3
|
+
Dir[File.join(File.dirname(__FILE__), 'support','**','*.rb')].each {|f| require f }
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
7
|
+
config.run_all_when_everything_filtered = true
|
8
|
+
config.filter_run :focus
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: esha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jens Balvig
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
16
|
+
requirement: &70325421751520 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70325421751520
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70325421750120 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70325421750120
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec-rails
|
38
|
+
requirement: &70325421748480 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70325421748480
|
47
|
+
description: Esha is a simple wrapper that makes the ESHA nutrition api a little easier
|
48
|
+
to work with.
|
49
|
+
email:
|
50
|
+
- jens@balvig.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- .rspec
|
57
|
+
- Gemfile
|
58
|
+
- LICENSE
|
59
|
+
- README.md
|
60
|
+
- Rakefile
|
61
|
+
- esha.gemspec
|
62
|
+
- lib/esha.rb
|
63
|
+
- lib/esha/api.rb
|
64
|
+
- lib/esha/food.rb
|
65
|
+
- lib/esha/nutrient.rb
|
66
|
+
- lib/esha/nutrition.rb
|
67
|
+
- lib/esha/version.rb
|
68
|
+
- spec/lib/esha/food_spec.rb
|
69
|
+
- spec/lib/esha/nutrient_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
- spec/support/api_key.rb
|
72
|
+
homepage: https://github.com/balvig/esha
|
73
|
+
licenses: []
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.8.11
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: Simple wrapper for ESHA nutrition database
|
96
|
+
test_files:
|
97
|
+
- spec/lib/esha/food_spec.rb
|
98
|
+
- spec/lib/esha/nutrient_spec.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
- spec/support/api_key.rb
|