usda_fdc 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: efffc201f528c1a723bc29b4442da66156487f4b208870a026bd1fad23491899
4
- data.tar.gz: 2c75291bcd2c6d121aa741c14d8485228fb52e9cf4654f216f3442f1e6717bea
3
+ metadata.gz: b834a400890953356c7e7a6265bd5f5126724d2a5009295160fc0a9fcabfecf1
4
+ data.tar.gz: 7d41308bc4ca5d3f1ffaf3be668b9f910f1cde9422727bc5592224776b30ae36
5
5
  SHA512:
6
- metadata.gz: b10bd1373720d91eb06477ee47abb8b179c8b0c73818f8ba12e5af21c84599d7e8cb1ef85aa67929f6093834e11b34b1c62332180a09b83e42d0d590af1c2db8
7
- data.tar.gz: 942c1728a46b7f4a6f8cbc745623f223811bd5a2f81febbd1bfa3c693e5091e17ec9d8433be88c22df7d08b0e0d796ef2b7f2840a183df2d6d4d0d045a34cb4a
6
+ metadata.gz: b52fc6edacaada7389148783465473e4d73b86089f289ed9208ba99ac8fd3457e7acdb8a42184aa789f9c509d37785c0250c2a0705484814cf20c12835376413
7
+ data.tar.gz: b59138eb82af2fd0611d8869a63cff8e2f04bb922228ba549b9bb749c5152fa41302fe1fda7b7b9cb8ff1b16b0a3258ee328bd4580efde9306e7f446af406803
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.1] - 2022-07-15
4
+
5
+ - Some internal cleanup
6
+
7
+ ## [0.2.0] - 2022-07-15
8
+
9
+ - Allow request methods to accept block to manage request
10
+ - Improve response handling (e.g. raise on 4xx/5xx responses)
11
+
12
+ ## [0.1.1] - 2022-06-24
13
+
14
+ - Ensure `json` module is available to Client
15
+
3
16
  ## [0.1.0] - 2022-06-24
4
17
 
5
18
  - Add basic support for interacting with USDA FDC endpoints
data/README.md CHANGED
@@ -1,26 +1,43 @@
1
1
  # UsdaFdc
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/usda_fdc.svg)](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
6
8
 
7
- Add this line to your application's Gemfile:
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:
8
14
 
9
15
  ```ruby
10
16
  gem 'usda_fdc'
11
17
  ```
12
18
 
13
- And then execute:
19
+ ## Usage
14
20
 
15
- $ bundle install
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).
16
22
 
17
- Or install it yourself as:
23
+ Once you have an API key, you're able to make API calls via the client object:
18
24
 
19
- $ gem install usda_fdc
25
+ ```ruby
26
+ client = UsdaFdc::Client.new('your_api_key')
27
+ client.food(534358)
20
28
 
21
- ## Usage
29
+ #=> { "dataType" => "Branded", "description" => "NUT 'N BERRY MIX", "fdcId" => 534358, ... }
30
+ ```
31
+
32
+ You can also configure the default API key globally for all `UsdaFdc::Client` instances:
22
33
 
23
- TBD
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).
24
41
 
25
42
  ## Development
26
43
 
@@ -28,10 +45,6 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
28
45
 
29
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).
30
47
 
31
- ## Contributing
32
-
33
- 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).
34
-
35
48
  ## License
36
49
 
37
50
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,29 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'net/http'
4
+ require 'json'
5
+ require_relative 'helpers/errors'
4
6
 
5
7
  module UsdaFdc
6
8
  class Client
7
9
  API_URL = 'https://api.nal.usda.gov'
8
10
 
9
- def initialize(api_key)
11
+ def initialize(api_key = nil)
10
12
  @api_key = api_key || UsdaFdc.api_key
13
+ raise UsdaFdc::ArgumentError, 'An API key is required.' if @api_key.nil?
11
14
  end
12
15
 
13
- def food(fdc_id, params = {})
14
- get("/fdc/v1/food/#{fdc_id}", params)
16
+ def food(fdc_id, params = {}, &block)
17
+ get("/fdc/v1/food/#{fdc_id}", params, &block)
15
18
  end
16
19
 
17
- def foods(body)
18
- post('/fdc/v1/foods', body)
20
+ def foods(body = {}, &block)
21
+ post('/fdc/v1/foods', body, &block)
19
22
  end
20
23
 
21
- def foods_list(body)
22
- post('/fdc/v1/foods/list', body)
24
+ def foods_list(body = {}, &block)
25
+ post('/fdc/v1/foods/list', body, &block)
23
26
  end
24
27
 
25
- def foods_search(body)
26
- post('/fdc/v1/foods/search', body)
28
+ def foods_search(body = {}, &block)
29
+ post('/fdc/v1/foods/search', body, &block)
27
30
  end
28
31
 
29
32
  def get(path, params = {})
@@ -31,29 +34,52 @@ module UsdaFdc
31
34
  uri.query = URI.encode_www_form(params) if params.any?
32
35
 
33
36
  request = Net::HTTP::Get.new(uri)
34
- request['X-Api-Key'] = @api_key
35
- request['Content-Type'] = 'application/json'
37
+ assign_default_headers(request)
38
+
39
+ yield(request) if block_given?
36
40
 
37
41
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
38
42
  http.request(request)
39
43
  end
40
44
 
41
- JSON.parse(response.body)
45
+ handle_response(response)
42
46
  end
43
47
 
44
48
  def post(path, body = {})
45
49
  uri = URI("#{API_URL}#{path}")
46
50
 
47
51
  request = Net::HTTP::Post.new(uri)
48
- request['X-Api-Key'] = @api_key
49
- request['Content-Type'] = 'application/json'
52
+ assign_default_headers(request)
50
53
  request.body = body.to_json
51
54
 
55
+ yield(request) if block_given?
56
+
52
57
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
53
58
  http.request(request)
54
59
  end
55
60
 
56
- JSON.parse(response.body)
61
+ handle_response(response)
62
+ end
63
+
64
+ private
65
+
66
+ def assign_default_headers(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
57
83
  end
58
84
  end
59
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UsdaFdc
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.1'
5
5
  end
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.1.0
4
+ version: 0.2.1
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-06-24 00:00:00.000000000 Z
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