usda_fdc 0.1.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/CHANGELOG.md +5 -0
- data/README.md +10 -0
- data/lib/usda_fdc/client.rb +40 -15
- data/lib/usda_fdc/helpers/errors.rb +23 -0
- data/lib/usda_fdc/version.rb +1 -1
- data/usda_fdc.gemspec +0 -1
- metadata +3 -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/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# UsdaFdc
|
2
2
|
|
3
|
+
[](https://badge.fury.io/rb/usda_fdc)
|
4
|
+
|
3
5
|
A Ruby interface to the USDA [FoodData Central](https://fdc.nal.usda.gov) API.
|
4
6
|
|
5
7
|
## Installation
|
@@ -27,6 +29,14 @@ client.food(534358)
|
|
27
29
|
#=> { "dataType" => "Branded", "description" => "NUT 'N BERRY MIX", "fdcId" => 534358, ... }
|
28
30
|
```
|
29
31
|
|
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
|
+
|
30
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).
|
31
41
|
|
32
42
|
## Development
|
data/lib/usda_fdc/client.rb
CHANGED
@@ -2,29 +2,31 @@
|
|
2
2
|
|
3
3
|
require 'net/http'
|
4
4
|
require 'json'
|
5
|
+
require_relative 'helpers/errors'
|
5
6
|
|
6
7
|
module UsdaFdc
|
7
8
|
class Client
|
8
9
|
API_URL = 'https://api.nal.usda.gov'
|
9
10
|
|
10
|
-
def initialize(api_key)
|
11
|
+
def initialize(api_key = nil)
|
11
12
|
@api_key = api_key || UsdaFdc.api_key
|
13
|
+
raise UsdaFdc::ArgumentError, 'An API key is required.' if @api_key.nil?
|
12
14
|
end
|
13
15
|
|
14
|
-
def food(fdc_id, params = {})
|
15
|
-
get("/fdc/v1/food/#{fdc_id}", params)
|
16
|
+
def food(fdc_id, params = {}, &block)
|
17
|
+
get("/fdc/v1/food/#{fdc_id}", params, &block)
|
16
18
|
end
|
17
19
|
|
18
|
-
def foods(body)
|
19
|
-
post('/fdc/v1/foods', body)
|
20
|
+
def foods(body = {}, &block)
|
21
|
+
post('/fdc/v1/foods', body, &block)
|
20
22
|
end
|
21
23
|
|
22
|
-
def foods_list(body)
|
23
|
-
post('/fdc/v1/foods/list', body)
|
24
|
+
def foods_list(body = {}, &block)
|
25
|
+
post('/fdc/v1/foods/list', body, &block)
|
24
26
|
end
|
25
27
|
|
26
|
-
def foods_search(body)
|
27
|
-
post('/fdc/v1/foods/search', body)
|
28
|
+
def foods_search(body = {}, &block)
|
29
|
+
post('/fdc/v1/foods/search', body, &block)
|
28
30
|
end
|
29
31
|
|
30
32
|
def get(path, params = {})
|
@@ -32,29 +34,52 @@ module UsdaFdc
|
|
32
34
|
uri.query = URI.encode_www_form(params) if params.any?
|
33
35
|
|
34
36
|
request = Net::HTTP::Get.new(uri)
|
35
|
-
request
|
36
|
-
|
37
|
+
set_default_headers_for(request)
|
38
|
+
|
39
|
+
yield(request) if block_given?
|
37
40
|
|
38
41
|
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
39
42
|
http.request(request)
|
40
43
|
end
|
41
44
|
|
42
|
-
|
45
|
+
handle_response(response)
|
43
46
|
end
|
44
47
|
|
45
48
|
def post(path, body = {})
|
46
49
|
uri = URI("#{API_URL}#{path}")
|
47
50
|
|
48
51
|
request = Net::HTTP::Post.new(uri)
|
49
|
-
request
|
50
|
-
request['Content-Type'] = 'application/json'
|
52
|
+
set_default_headers_for(request)
|
51
53
|
request.body = body.to_json
|
52
54
|
|
55
|
+
yield(request) if block_given?
|
56
|
+
|
53
57
|
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
54
58
|
http.request(request)
|
55
59
|
end
|
56
60
|
|
57
|
-
|
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
|
58
83
|
end
|
59
84
|
end
|
60
85
|
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/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.
|
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: []
|
@@ -27,6 +26,7 @@ files:
|
|
27
26
|
- lib/usda_fdc/client.rb
|
28
27
|
- lib/usda_fdc/configuration.rb
|
29
28
|
- lib/usda_fdc/helpers/configurable.rb
|
29
|
+
- lib/usda_fdc/helpers/errors.rb
|
30
30
|
- lib/usda_fdc/version.rb
|
31
31
|
- sig/usda_fdc.rbs
|
32
32
|
- usda_fdc.gemspec
|