usda_fdc 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +5 -1
- data/README.md +2 -8
- data/lib/usda_fdc/client.rb +59 -0
- data/lib/usda_fdc/configuration.rb +9 -0
- data/lib/usda_fdc/helpers/configurable.rb +21 -0
- data/lib/usda_fdc/version.rb +1 -1
- data/lib/usda_fdc.rb +3 -2
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: efffc201f528c1a723bc29b4442da66156487f4b208870a026bd1fad23491899
|
4
|
+
data.tar.gz: 2c75291bcd2c6d121aa741c14d8485228fb52e9cf4654f216f3442f1e6717bea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b10bd1373720d91eb06477ee47abb8b179c8b0c73818f8ba12e5af21c84599d7e8cb1ef85aa67929f6093834e11b34b1c62332180a09b83e42d0d590af1c2db8
|
7
|
+
data.tar.gz: 942c1728a46b7f4a6f8cbc745623f223811bd5a2f81febbd1bfa3c693e5091e17ec9d8433be88c22df7d08b0e0d796ef2b7f2840a183df2d6d4d0d045a34cb4a
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# UsdaFdc
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
A Ruby interface to the USDA [FoodData Central](https://fdc.nal.usda.gov) API.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,7 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
TBD
|
26
24
|
|
27
25
|
## Development
|
28
26
|
|
@@ -37,7 +35,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
|
|
37
35
|
## License
|
38
36
|
|
39
37
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
-
|
41
|
-
## Code of Conduct
|
42
|
-
|
43
|
-
Everyone interacting in the UsdaFdc project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/usda_fdc/blob/master/CODE_OF_CONDUCT.md).
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
module UsdaFdc
|
6
|
+
class Client
|
7
|
+
API_URL = 'https://api.nal.usda.gov'
|
8
|
+
|
9
|
+
def initialize(api_key)
|
10
|
+
@api_key = api_key || UsdaFdc.api_key
|
11
|
+
end
|
12
|
+
|
13
|
+
def food(fdc_id, params = {})
|
14
|
+
get("/fdc/v1/food/#{fdc_id}", params)
|
15
|
+
end
|
16
|
+
|
17
|
+
def foods(body)
|
18
|
+
post('/fdc/v1/foods', body)
|
19
|
+
end
|
20
|
+
|
21
|
+
def foods_list(body)
|
22
|
+
post('/fdc/v1/foods/list', body)
|
23
|
+
end
|
24
|
+
|
25
|
+
def foods_search(body)
|
26
|
+
post('/fdc/v1/foods/search', body)
|
27
|
+
end
|
28
|
+
|
29
|
+
def get(path, params = {})
|
30
|
+
uri = URI("#{API_URL}#{path}")
|
31
|
+
uri.query = URI.encode_www_form(params) if params.any?
|
32
|
+
|
33
|
+
request = Net::HTTP::Get.new(uri)
|
34
|
+
request['X-Api-Key'] = @api_key
|
35
|
+
request['Content-Type'] = 'application/json'
|
36
|
+
|
37
|
+
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
38
|
+
http.request(request)
|
39
|
+
end
|
40
|
+
|
41
|
+
JSON.parse(response.body)
|
42
|
+
end
|
43
|
+
|
44
|
+
def post(path, body = {})
|
45
|
+
uri = URI("#{API_URL}#{path}")
|
46
|
+
|
47
|
+
request = Net::HTTP::Post.new(uri)
|
48
|
+
request['X-Api-Key'] = @api_key
|
49
|
+
request['Content-Type'] = 'application/json'
|
50
|
+
request.body = body.to_json
|
51
|
+
|
52
|
+
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
53
|
+
http.request(request)
|
54
|
+
end
|
55
|
+
|
56
|
+
JSON.parse(response.body)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module UsdaFdc
|
4
|
+
module Configurable
|
5
|
+
def configure
|
6
|
+
yield(self)
|
7
|
+
end
|
8
|
+
|
9
|
+
def define_setting(name, default = nil)
|
10
|
+
instance_variable_set("@#{name}", default)
|
11
|
+
|
12
|
+
define_singleton_method("#{name}=") do |value|
|
13
|
+
instance_variable_set("@#{name}", value)
|
14
|
+
end
|
15
|
+
|
16
|
+
define_singleton_method(name) do
|
17
|
+
instance_variable_get("@#{name}")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/usda_fdc/version.rb
CHANGED
data/lib/usda_fdc.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: usda_fdc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zoran Pesic
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -24,6 +24,9 @@ files:
|
|
24
24
|
- README.md
|
25
25
|
- Rakefile
|
26
26
|
- lib/usda_fdc.rb
|
27
|
+
- lib/usda_fdc/client.rb
|
28
|
+
- lib/usda_fdc/configuration.rb
|
29
|
+
- lib/usda_fdc/helpers/configurable.rb
|
27
30
|
- lib/usda_fdc/version.rb
|
28
31
|
- sig/usda_fdc.rbs
|
29
32
|
- usda_fdc.gemspec
|