mat 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +15 -0
- data/lib/mat.rb +37 -0
- data/lib/mat/api.rb +55 -0
- data/lib/mat/api/config.rb +20 -0
- data/lib/mat/foodstuff.rb +53 -0
- data/lib/mat/http.rb +46 -0
- data/lib/mat/nutrient.rb +42 -0
- data/lib/mat/version.rb +5 -0
- data/mat.gemspec +28 -0
- data/spec/fixtures/foodstuff/24_nutrient_carbohydrates.json +5 -0
- data/spec/fixtures/foodstuff/44.json +58 -0
- data/spec/mat/api_spec.rb +43 -0
- data/spec/mat/foodstuff_spec.rb +17 -0
- data/spec/mat/http_spec.rb +20 -0
- data/spec/mat/version_spec.rb +11 -0
- data/spec/mat_spec.rb +58 -0
- data/spec/spec_helper.rb +47 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a9766485afe87dccb42c0ecec87cd259f9b45503
|
4
|
+
data.tar.gz: 270ab12ccd5262b6f77f84be8e117155f7a33215
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 295337f809540e33831b43ff7ed360ab626ad272da1fe6344b5732a21bb4fa6f0a04231cd6189a75f1e7c4028245022b32b7602d1595ce0d6e058fbc3d40e234
|
7
|
+
data.tar.gz: 12d167649edeba0ab5e00716bf5a53ddd029b3d22045e6db966db8dc79f039f4a02f1862d4e2dbf81f000c3b50c9a8f7dedf8bf0f1185b7c52755711170a4bed
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Peter Hellberg
|
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,46 @@
|
|
1
|
+
# Mat
|
2
|
+
|
3
|
+
A small (unofficial) API client for the [Mat API](http://matapi.se/).
|
4
|
+
|
5
|
+
The documentation is in Swedish, but so is the [data](http://www.slv.se/sv/grupp1/mat-och-naring/vad-innehaller-maten/livsmedelsdatabasen-/).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'mat'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install mat
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
It is probably a good idea to take a look in `specs`
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# All nutrients
|
27
|
+
Mat.nutrients
|
28
|
+
|
29
|
+
# Find the first food with ’köttfärs’ in the name
|
30
|
+
köttfärspaj = Mat.all('Köttfärs').first
|
31
|
+
|
32
|
+
# Get the `iron` value
|
33
|
+
köttfärspaj.iron #=> 1.09
|
34
|
+
|
35
|
+
# Find a specific food by its number
|
36
|
+
food = Mat.find(3)
|
37
|
+
food.name #=> "Ister gris"
|
38
|
+
```
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "rake/testtask"
|
4
|
+
require "bundler/gem_tasks"
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
Rake::TestTask.new(:spec) do |t|
|
9
|
+
t.test_files = FileList['spec/**/*_spec.rb']
|
10
|
+
end
|
11
|
+
|
12
|
+
desc "Console"
|
13
|
+
task :console do
|
14
|
+
exec 'pry -r./lib/mat'
|
15
|
+
end
|
data/lib/mat.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative "mat/api"
|
4
|
+
require_relative "mat/foodstuff"
|
5
|
+
require_relative "mat/http"
|
6
|
+
require_relative "mat/nutrient"
|
7
|
+
require_relative "mat/version"
|
8
|
+
|
9
|
+
module Mat
|
10
|
+
class << self
|
11
|
+
def api(config = nil)
|
12
|
+
API.new(config).tap do |api|
|
13
|
+
yield(api.config) if block_given?
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def all(query = nil)
|
18
|
+
Foodstuff.all(query)
|
19
|
+
end
|
20
|
+
|
21
|
+
def find(number, nutrient = nil)
|
22
|
+
if number.to_s.match(/^\d+$/)
|
23
|
+
Foodstuff.find(number, nutrient)
|
24
|
+
else
|
25
|
+
Foodstuff.all(number).first
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def nutrient(slug)
|
30
|
+
Nutrient.find(slug)
|
31
|
+
end
|
32
|
+
|
33
|
+
def nutrients
|
34
|
+
Nutrient.all
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/mat/api.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
require_relative "api/config"
|
6
|
+
|
7
|
+
module Mat
|
8
|
+
class API
|
9
|
+
attr_accessor :config
|
10
|
+
|
11
|
+
def initialize(config = nil)
|
12
|
+
@config = config || API::Config.new
|
13
|
+
|
14
|
+
yield(@config) if block_given?
|
15
|
+
end
|
16
|
+
|
17
|
+
def foodstuff(number, nutrient = nil)
|
18
|
+
endpoint = "foodstuff/#{number}"
|
19
|
+
endpoint += "?nutrient=#{nutrient}" if nutrient
|
20
|
+
|
21
|
+
get(endpoint)
|
22
|
+
end
|
23
|
+
|
24
|
+
def foodstuffs(query = nil)
|
25
|
+
query.nil?? get("foodstuff") : get("foodstuff?query=#{URI::encode(query)}")
|
26
|
+
end
|
27
|
+
|
28
|
+
def nutrient(slug)
|
29
|
+
get("nutrient/#{slug}")
|
30
|
+
end
|
31
|
+
|
32
|
+
def nutrients
|
33
|
+
get("nutrient")
|
34
|
+
end
|
35
|
+
|
36
|
+
def get(path)
|
37
|
+
response = http_get(path)
|
38
|
+
load_json(response.body) if response && response.body
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def http_get(path)
|
44
|
+
config.http_client.get(uri(path))
|
45
|
+
end
|
46
|
+
|
47
|
+
def uri(endpoint = nil)
|
48
|
+
URI.parse("#{config.base_url}#{endpoint}")
|
49
|
+
end
|
50
|
+
|
51
|
+
def load_json(doc)
|
52
|
+
config.json_parser.load(doc)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Mat
|
6
|
+
class API
|
7
|
+
class Config
|
8
|
+
attr_accessor :base_url, :http_client, :json_parser
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@base_url = 'http://matapi.se/'
|
12
|
+
@http_client = Mat::HTTP
|
13
|
+
@json_parser = JSON
|
14
|
+
|
15
|
+
yield self if block_given?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Mat
|
4
|
+
class Foodstuff
|
5
|
+
class << self
|
6
|
+
def find(number, nutrient = nil)
|
7
|
+
new get_data(number, nutrient)
|
8
|
+
end
|
9
|
+
|
10
|
+
def all(query)
|
11
|
+
Mat.api.foodstuffs(query).map { |d| new(d) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_data(number, nutrient = nil)
|
15
|
+
Mat.api.foodstuff(number, nutrient)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_reader :data
|
20
|
+
|
21
|
+
def initialize(data)
|
22
|
+
@data = data || {}
|
23
|
+
end
|
24
|
+
|
25
|
+
def name
|
26
|
+
data['name']
|
27
|
+
end
|
28
|
+
|
29
|
+
def number
|
30
|
+
data['number']
|
31
|
+
end
|
32
|
+
|
33
|
+
def nutrient_values
|
34
|
+
get_data! if data['nutrientValues'].nil?
|
35
|
+
|
36
|
+
data['nutrientValues']
|
37
|
+
end
|
38
|
+
|
39
|
+
def method_missing(m, *args, &block)
|
40
|
+
if nutrient_values.respond_to?(:keys) &&
|
41
|
+
nutrient_values.keys.include?(m.to_s)
|
42
|
+
nutrient_values[m.to_s]
|
43
|
+
else
|
44
|
+
super
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def get_data!
|
49
|
+
initialize self.class.get_data(number)
|
50
|
+
self
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/mat/http.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
require_relative "version"
|
6
|
+
|
7
|
+
module Mat
|
8
|
+
class HTTP
|
9
|
+
HEADERS = {
|
10
|
+
'User-Agent' => "RubyGem: mat (#{Mat::VERSION})"
|
11
|
+
}
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def get(uri)
|
15
|
+
perform uri, Net::HTTP::Get.new(uri.request_uri, HEADERS)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def perform(uri, request)
|
21
|
+
Net::HTTP.start(uri.host, uri.port) do |http|
|
22
|
+
http.read_timeout = 60
|
23
|
+
response = http.request(request)
|
24
|
+
http.finish
|
25
|
+
|
26
|
+
if response.kind_of? Net::HTTPSuccess
|
27
|
+
response
|
28
|
+
else
|
29
|
+
raise Exception, response
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Exception < RuntimeError
|
36
|
+
attr_accessor :response
|
37
|
+
attr_reader :message
|
38
|
+
|
39
|
+
def initialize(response = nil)
|
40
|
+
@response = response
|
41
|
+
@message = response.message if response
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
data/lib/mat/nutrient.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Mat
|
4
|
+
class Nutrient
|
5
|
+
class << self
|
6
|
+
def find(slug)
|
7
|
+
new get_data(slug)
|
8
|
+
end
|
9
|
+
|
10
|
+
def all
|
11
|
+
Mat.api.nutrients.map { |d| new(d) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_data(slug)
|
15
|
+
Mat.api.nutrient(slug)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_reader :data
|
20
|
+
|
21
|
+
def initialize(data)
|
22
|
+
@data = data || {}
|
23
|
+
end
|
24
|
+
|
25
|
+
def slug
|
26
|
+
@data['slug']
|
27
|
+
end
|
28
|
+
|
29
|
+
def name
|
30
|
+
@data['name']
|
31
|
+
end
|
32
|
+
|
33
|
+
def unit
|
34
|
+
@data['unit']
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_data!
|
38
|
+
initialize self.class.get_data(slug)
|
39
|
+
self
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/mat/version.rb
ADDED
data/mat.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'mat/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = "mat"
|
10
|
+
spec.version = Mat::VERSION
|
11
|
+
spec.authors = ["Peter Hellberg"]
|
12
|
+
spec.email = ["peter@c7.se"]
|
13
|
+
spec.summary = %q{A small API client for the Mat API}
|
14
|
+
spec.homepage = "https://github.com/peterhellberg/mat"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.required_ruby_version = '>= 1.9.3'
|
18
|
+
|
19
|
+
spec.files = `git ls-files`.split($/)
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
21
|
+
spec.test_files = spec.files.grep(%r{^spec/})
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "minitest", "~> 4.7"
|
26
|
+
spec.add_development_dependency "rake"
|
27
|
+
spec.add_development_dependency "pry"
|
28
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
{
|
2
|
+
"name": "Dressing majonnäs fett ca 40% ",
|
3
|
+
"foodstuffNumber": 44,
|
4
|
+
"energyKj": 1898,
|
5
|
+
"energyKcal": 454,
|
6
|
+
"protein": 0.5,
|
7
|
+
"fat": 40,
|
8
|
+
"carbohydrates": 24,
|
9
|
+
"fibres": 0.2,
|
10
|
+
"salt": 1.7,
|
11
|
+
"ash": 2.1,
|
12
|
+
"water": 33.2,
|
13
|
+
"alcohol": 0,
|
14
|
+
"monosaccharides": 3.1,
|
15
|
+
"disaccharides": 18.6,
|
16
|
+
"saccharose": 18.6,
|
17
|
+
"wholegrain": 0,
|
18
|
+
"saturatedFattyAcids": 6.1,
|
19
|
+
"fattyAcid40100": 0,
|
20
|
+
"fattyAcid120": 0,
|
21
|
+
"fattyAcid140": 0,
|
22
|
+
"fattyAcid160": 4.2,
|
23
|
+
"fattyAcid180": 1.5,
|
24
|
+
"fattyAcid200": 0.2,
|
25
|
+
"monounsaturatedFattyAcids": 9.8,
|
26
|
+
"fattyAcid161": 0,
|
27
|
+
"fattyAcid181": 9.8,
|
28
|
+
"sumPolyunsaturatedFattyAcids": 22.3,
|
29
|
+
"fattyAcid182": 19.7,
|
30
|
+
"fattyAcid204": 0,
|
31
|
+
"fattyAcid183": 2.6,
|
32
|
+
"epa": 0,
|
33
|
+
"dpa": 0,
|
34
|
+
"dha": 0,
|
35
|
+
"cholesterol": 26,
|
36
|
+
"retinolEquivalents": 2,
|
37
|
+
"retinol": 2,
|
38
|
+
"betacarotene": 0,
|
39
|
+
"vitaminD": 0,
|
40
|
+
"vitaminE": 2.5,
|
41
|
+
"timamine": 0.01,
|
42
|
+
"riboflavin": 0.02,
|
43
|
+
"vitaminC": 0,
|
44
|
+
"niacin": 0,
|
45
|
+
"niacinEquivalents": 0.1,
|
46
|
+
"vitaminB6": 0.02,
|
47
|
+
"vitaminB12": 0.2,
|
48
|
+
"phosphorous": 17,
|
49
|
+
"folate": 7,
|
50
|
+
"trash": 0,
|
51
|
+
"iron": 0.3,
|
52
|
+
"calcium": 11,
|
53
|
+
"potassium": 142,
|
54
|
+
"magnesium": 5,
|
55
|
+
"sodium": 680,
|
56
|
+
"selenium": 0.5,
|
57
|
+
"zink": 0.1
|
58
|
+
}
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative '../spec_helper'
|
4
|
+
|
5
|
+
describe "API" do
|
6
|
+
subject { Mat::API }
|
7
|
+
|
8
|
+
let(:api) { api_with_test_config(Mat::API.new) }
|
9
|
+
let(:base_url) { "http://api.test/" }
|
10
|
+
let(:expected_uri) { URI.parse "#{base_url}/endpoint" }
|
11
|
+
|
12
|
+
describe "initialize" do
|
13
|
+
it "takes a config" do
|
14
|
+
subject.new("foo").config.must_equal "foo"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "instantiates a default config" do
|
18
|
+
subject.new.config.base_url.must_equal "http://matapi.se/"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "get" do
|
23
|
+
it "checks for nil responses" do
|
24
|
+
api.stub(:http_get, nil) do
|
25
|
+
api.get('nil').must_be_nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "retrieves the data using a HTTP GET" do
|
30
|
+
api.stub(:http_get, fake_response('{"foo":"bar"}')) do
|
31
|
+
api.get('/').keys.first.must_equal "foo"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "foodstuff" do
|
37
|
+
it "parses the returned body" do
|
38
|
+
api.stub(:http_get, fixture_response('foodstuff/44')) do
|
39
|
+
api.foodstuff(44)["name"].must_equal "Dressing majonnäs fett ca 40% "
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative '../spec_helper'
|
4
|
+
|
5
|
+
describe Mat::Foodstuff do
|
6
|
+
subject { Mat::Foodstuff }
|
7
|
+
|
8
|
+
let(:fixture_file) { 'foodstuff/44' }
|
9
|
+
let(:fixture_data) { parsed_fixture(fixture_file) }
|
10
|
+
let(:foodstuff) { subject.new(fixture_data) }
|
11
|
+
|
12
|
+
describe "initialize" do
|
13
|
+
it "takes data as input" do
|
14
|
+
subject.new("foo").data.must_equal "foo"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative '../spec_helper'
|
4
|
+
|
5
|
+
describe Mat::HTTP do
|
6
|
+
subject { Mat::HTTP }
|
7
|
+
|
8
|
+
describe "headers" do
|
9
|
+
it "must set a custom user-agent" do
|
10
|
+
expected_ua = "RubyGem: mat (#{Mat::VERSION})"
|
11
|
+
subject::HEADERS['User-Agent'].must_equal expected_ua
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "Exception" do
|
16
|
+
it "must be a RuntimeError" do
|
17
|
+
subject::Exception.new.must_be_kind_of RuntimeError
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/mat_spec.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative 'spec_helper'
|
4
|
+
|
5
|
+
describe Mat do
|
6
|
+
subject { Mat }
|
7
|
+
|
8
|
+
let(:api) { api_with_test_config(Mat::API.new) }
|
9
|
+
|
10
|
+
describe "api" do
|
11
|
+
it "returns a configured api client" do
|
12
|
+
api.config.base_url.must_equal "api.test"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "memoizes the api client" do
|
16
|
+
subject.api.object_id.wont_equal subject.api.object_id
|
17
|
+
end
|
18
|
+
|
19
|
+
it "can configure the base_url" do
|
20
|
+
api.config.base_url.must_equal 'api.test'
|
21
|
+
|
22
|
+
configured_api = subject.api { |c| c.base_url = 'api.changed' }
|
23
|
+
configured_api.config.base_url.must_equal 'api.changed'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "all" do
|
28
|
+
it "finds all foodstuffs" do
|
29
|
+
Mat::Foodstuff.stub(:all, 'all foodstuffs') do
|
30
|
+
Mat.all.must_equal 'all foodstuffs'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "find" do
|
36
|
+
it "finds a foodstuff" do
|
37
|
+
Mat::Foodstuff.stub(:find, 'foodstuff') do
|
38
|
+
Mat.find(3).must_equal 'foodstuff'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "nutrient" do
|
44
|
+
it "finds a nutrient" do
|
45
|
+
Mat::Nutrient.stub(:find, 'nutrient') do
|
46
|
+
Mat.nutrient(:salt).must_equal 'nutrient'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "nutrients" do
|
52
|
+
it "returns all nutrients" do
|
53
|
+
Mat::Nutrient.stub(:all, 'all nutrients') do
|
54
|
+
Mat.nutrients.must_equal 'all nutrients'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "minitest/spec"
|
4
|
+
require "minitest/pride"
|
5
|
+
require "minitest/autorun"
|
6
|
+
|
7
|
+
require_relative "../lib/mat"
|
8
|
+
|
9
|
+
$loaded_fixtures = {}
|
10
|
+
$parsed_fixtures = {}
|
11
|
+
|
12
|
+
def s(*args)
|
13
|
+
subject.new(*args)
|
14
|
+
end
|
15
|
+
|
16
|
+
def fixture(name)
|
17
|
+
$loaded_fixtures[name] ||= IO.read("spec/fixtures/#{name}.json")
|
18
|
+
end
|
19
|
+
|
20
|
+
def parsed_fixture(name)
|
21
|
+
$parsed_fixtures[name] ||= JSON.parse(fixture(name))
|
22
|
+
end
|
23
|
+
|
24
|
+
def api_with_test_config(api)
|
25
|
+
test_configuration!(api.config)
|
26
|
+
api
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_configuration!(c)
|
30
|
+
c.base_url = 'api.test'
|
31
|
+
end
|
32
|
+
|
33
|
+
class Mat::HTTP::FakeResponse
|
34
|
+
attr_reader :body
|
35
|
+
|
36
|
+
def initialize(body = nil)
|
37
|
+
@body = body
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def fake_response(body)
|
42
|
+
Mat::HTTP::FakeResponse.new(body)
|
43
|
+
end
|
44
|
+
|
45
|
+
def fixture_response(name)
|
46
|
+
fake_response(fixture(name))
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Hellberg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- peter@c7.se
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/mat.rb
|
82
|
+
- lib/mat/api.rb
|
83
|
+
- lib/mat/api/config.rb
|
84
|
+
- lib/mat/foodstuff.rb
|
85
|
+
- lib/mat/http.rb
|
86
|
+
- lib/mat/nutrient.rb
|
87
|
+
- lib/mat/version.rb
|
88
|
+
- mat.gemspec
|
89
|
+
- spec/fixtures/foodstuff/24_nutrient_carbohydrates.json
|
90
|
+
- spec/fixtures/foodstuff/44.json
|
91
|
+
- spec/mat/api_spec.rb
|
92
|
+
- spec/mat/foodstuff_spec.rb
|
93
|
+
- spec/mat/http_spec.rb
|
94
|
+
- spec/mat/version_spec.rb
|
95
|
+
- spec/mat_spec.rb
|
96
|
+
- spec/spec_helper.rb
|
97
|
+
homepage: https://github.com/peterhellberg/mat
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 1.9.3
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.0.3
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: A small API client for the Mat API
|
121
|
+
test_files:
|
122
|
+
- spec/fixtures/foodstuff/24_nutrient_carbohydrates.json
|
123
|
+
- spec/fixtures/foodstuff/44.json
|
124
|
+
- spec/mat/api_spec.rb
|
125
|
+
- spec/mat/foodstuff_spec.rb
|
126
|
+
- spec/mat/http_spec.rb
|
127
|
+
- spec/mat/version_spec.rb
|
128
|
+
- spec/mat_spec.rb
|
129
|
+
- spec/spec_helper.rb
|
130
|
+
has_rdoc:
|