usda_fdc 0.0.1 → 0.2.0
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +14 -1
- data/README.md +24 -17
- data/lib/usda_fdc/client.rb +85 -0
- data/lib/usda_fdc/configuration.rb +9 -0
- data/lib/usda_fdc/helpers/configurable.rb +21 -0
- data/lib/usda_fdc/helpers/errors.rb +23 -0
- data/lib/usda_fdc/version.rb +1 -1
- data/lib/usda_fdc.rb +3 -2
- data/usda_fdc.gemspec +0 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3259d937f8cab77f84475be7d9cc4871efed34326604ceeb0aa2fb6f3e217546
|
4
|
+
data.tar.gz: 88c52fdab8c2bd8fc4d6492fdc81d594dd477d63c49cac8c77b216a35bd21926
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9804c6de9e90e962dafb4cc9184165c85f6f1b9bbf20a8e6551ab45b817319c87266b82b85cc12988bd2d714ba9db192127739d559ab6eb49d968e4cbcb61d6
|
7
|
+
data.tar.gz: 97dcb6d90428858f511c413e59faab3faa14e056377cab0da0ebb96427371d93132507b24b6220c17aba592ac82059ace8d053a648dc7a177cf770b49786c4e3
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
-
## [0.
|
3
|
+
## [0.2.0] - 2022-07-15
|
4
|
+
|
5
|
+
- Allow request methods to accept block to manage request
|
6
|
+
- Improve response handling (e.g. raise on 4xx/5xx responses)
|
7
|
+
|
8
|
+
## [0.1.1] - 2022-06-24
|
9
|
+
|
10
|
+
- Ensure `json` module is available to Client
|
11
|
+
|
12
|
+
## [0.1.0] - 2022-06-24
|
13
|
+
|
14
|
+
- Add basic support for interacting with USDA FDC endpoints
|
15
|
+
|
16
|
+
## [0.0.1] - 2022-01-26
|
4
17
|
|
5
18
|
- Initial release
|
data/README.md
CHANGED
@@ -1,28 +1,43 @@
|
|
1
1
|
# UsdaFdc
|
2
2
|
|
3
|
-
|
3
|
+
[](https://badge.fury.io/rb/usda_fdc)
|
4
4
|
|
5
|
-
|
5
|
+
A Ruby interface to the USDA [FoodData Central](https://fdc.nal.usda.gov) API.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
|
-
|
9
|
+
To install the latest release:
|
10
|
+
|
11
|
+
$ gem install usda_fdc
|
12
|
+
|
13
|
+
To include in a Rails project, add it to the Gemfile:
|
10
14
|
|
11
15
|
```ruby
|
12
16
|
gem 'usda_fdc'
|
13
17
|
```
|
14
18
|
|
15
|
-
|
19
|
+
## Usage
|
16
20
|
|
17
|
-
|
21
|
+
An API key is required to access the service. You can find details on how to obtain a key [here](https://fdc.nal.usda.gov/api-guide.html#bkmk-3).
|
18
22
|
|
19
|
-
|
23
|
+
Once you have an API key, you're able to make API calls via the client object:
|
20
24
|
|
21
|
-
|
25
|
+
```ruby
|
26
|
+
client = UsdaFdc::Client.new('your_api_key')
|
27
|
+
client.food(534358)
|
22
28
|
|
23
|
-
|
29
|
+
#=> { "dataType" => "Branded", "description" => "NUT 'N BERRY MIX", "fdcId" => 534358, ... }
|
30
|
+
```
|
24
31
|
|
25
|
-
|
32
|
+
You can also configure the default API key globally for all `UsdaFdc::Client` instances:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
UsdaFdc.configure do |config|
|
36
|
+
config.api_key = 'your_api_key'
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
For more information on interacting with the API, including accepted parameters and response formats, check out the official USDA FDC [API guide](https://fdc.nal.usda.gov/api-guide.html).
|
26
41
|
|
27
42
|
## Development
|
28
43
|
|
@@ -30,14 +45,6 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
30
45
|
|
31
46
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
47
|
|
33
|
-
## Contributing
|
34
|
-
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/usda_fdc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/usda_fdc/blob/master/CODE_OF_CONDUCT.md).
|
36
|
-
|
37
48
|
## License
|
38
49
|
|
39
50
|
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,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'json'
|
5
|
+
require_relative 'helpers/errors'
|
6
|
+
|
7
|
+
module UsdaFdc
|
8
|
+
class Client
|
9
|
+
API_URL = 'https://api.nal.usda.gov'
|
10
|
+
|
11
|
+
def initialize(api_key = nil)
|
12
|
+
@api_key = api_key || UsdaFdc.api_key
|
13
|
+
raise UsdaFdc::ArgumentError, 'An API key is required.' if @api_key.nil?
|
14
|
+
end
|
15
|
+
|
16
|
+
def food(fdc_id, params = {}, &block)
|
17
|
+
get("/fdc/v1/food/#{fdc_id}", params, &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def foods(body = {}, &block)
|
21
|
+
post('/fdc/v1/foods', body, &block)
|
22
|
+
end
|
23
|
+
|
24
|
+
def foods_list(body = {}, &block)
|
25
|
+
post('/fdc/v1/foods/list', body, &block)
|
26
|
+
end
|
27
|
+
|
28
|
+
def foods_search(body = {}, &block)
|
29
|
+
post('/fdc/v1/foods/search', body, &block)
|
30
|
+
end
|
31
|
+
|
32
|
+
def get(path, params = {})
|
33
|
+
uri = URI("#{API_URL}#{path}")
|
34
|
+
uri.query = URI.encode_www_form(params) if params.any?
|
35
|
+
|
36
|
+
request = Net::HTTP::Get.new(uri)
|
37
|
+
set_default_headers_for(request)
|
38
|
+
|
39
|
+
yield(request) if block_given?
|
40
|
+
|
41
|
+
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
42
|
+
http.request(request)
|
43
|
+
end
|
44
|
+
|
45
|
+
handle_response(response)
|
46
|
+
end
|
47
|
+
|
48
|
+
def post(path, body = {})
|
49
|
+
uri = URI("#{API_URL}#{path}")
|
50
|
+
|
51
|
+
request = Net::HTTP::Post.new(uri)
|
52
|
+
set_default_headers_for(request)
|
53
|
+
request.body = body.to_json
|
54
|
+
|
55
|
+
yield(request) if block_given?
|
56
|
+
|
57
|
+
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
58
|
+
http.request(request)
|
59
|
+
end
|
60
|
+
|
61
|
+
handle_response(response)
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def set_default_headers_for(request)
|
67
|
+
request['X-Api-Key'] = @api_key
|
68
|
+
request['Content-Type'] = 'application/json'
|
69
|
+
request['User-Agent'] = "usda_fdc gem (v#{UsdaFdc::VERSION})"
|
70
|
+
end
|
71
|
+
|
72
|
+
def handle_response(response)
|
73
|
+
case response
|
74
|
+
when Net::HTTPSuccess
|
75
|
+
JSON.parse(response.body)
|
76
|
+
when Net::HTTPClientError
|
77
|
+
raise UsdaFdc::ClientError.new(response.message, response.code)
|
78
|
+
when Net::HTTPServerError
|
79
|
+
raise UsdaFdc::ServerError.new(response.message, response.code)
|
80
|
+
else
|
81
|
+
response
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
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
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module UsdaFdc
|
4
|
+
class ArgumentError < ::ArgumentError
|
5
|
+
end
|
6
|
+
|
7
|
+
class ResponseError < StandardError
|
8
|
+
attr_reader :code
|
9
|
+
|
10
|
+
def initialize(message = '', code = nil)
|
11
|
+
@code = code
|
12
|
+
super(message)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# For 4xx responses
|
17
|
+
class ClientError < ResponseError
|
18
|
+
end
|
19
|
+
|
20
|
+
# For 5xx responses
|
21
|
+
class ServerError < ResponseError
|
22
|
+
end
|
23
|
+
end
|
data/lib/usda_fdc/version.rb
CHANGED
data/lib/usda_fdc.rb
CHANGED
data/usda_fdc.gemspec
CHANGED
@@ -6,7 +6,6 @@ Gem::Specification.new do |spec|
|
|
6
6
|
spec.name = 'usda_fdc'
|
7
7
|
spec.version = UsdaFdc::VERSION
|
8
8
|
spec.authors = ['Zoran Pesic']
|
9
|
-
spec.email = ['zspesic@gmail.com']
|
10
9
|
|
11
10
|
spec.summary = 'A Ruby interface to the USDA FoodData Central API.'
|
12
11
|
spec.homepage = 'https://github.com/zokioki/usda_fdc'
|
metadata
CHANGED
@@ -1,18 +1,17 @@
|
|
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.2.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-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
15
|
-
- zspesic@gmail.com
|
16
15
|
executables: []
|
17
16
|
extensions: []
|
18
17
|
extra_rdoc_files: []
|
@@ -24,6 +23,10 @@ files:
|
|
24
23
|
- README.md
|
25
24
|
- Rakefile
|
26
25
|
- lib/usda_fdc.rb
|
26
|
+
- lib/usda_fdc/client.rb
|
27
|
+
- lib/usda_fdc/configuration.rb
|
28
|
+
- lib/usda_fdc/helpers/configurable.rb
|
29
|
+
- lib/usda_fdc/helpers/errors.rb
|
27
30
|
- lib/usda_fdc/version.rb
|
28
31
|
- sig/usda_fdc.rbs
|
29
32
|
- usda_fdc.gemspec
|