usda_fdc 0.0.1 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c953718a90e8cc39639ed57400d3b22bf689381f0fd3bd734149216f9d541f26
4
- data.tar.gz: a45ec9a472df7e292833f825a42a3e81b0b2b9d686dd01c2c366a179d255ca07
3
+ metadata.gz: efffc201f528c1a723bc29b4442da66156487f4b208870a026bd1fad23491899
4
+ data.tar.gz: 2c75291bcd2c6d121aa741c14d8485228fb52e9cf4654f216f3442f1e6717bea
5
5
  SHA512:
6
- metadata.gz: 2c2db45624943439fdff3d8f92498660062a454a2617beb7d2999b55d5add1bee31b4016ddc24e27540cb0c02b6c18dcbb961ec57f8130ed121053e92ebf2f74
7
- data.tar.gz: d24b77cd1a3e7cbd6aec8200a6a3f96c5f56a3462f9ad020ee17a9568ce37faa7e9d2b7d882bfc5dee7b09aa71602a8745aa8c098363a8b1825e1b98c9627903
6
+ metadata.gz: b10bd1373720d91eb06477ee47abb8b179c8b0c73818f8ba12e5af21c84599d7e8cb1ef85aa67929f6093834e11b34b1c62332180a09b83e42d0d590af1c2db8
7
+ data.tar.gz: 942c1728a46b7f4a6f8cbc745623f223811bd5a2f81febbd1bfa3c693e5091e17ec9d8433be88c22df7d08b0e0d796ef2b7f2840a183df2d6d4d0d045a34cb4a
data/.rubocop.yml CHANGED
@@ -6,3 +6,6 @@ Layout/LineLength:
6
6
 
7
7
  Style/Documentation:
8
8
  Enabled: false
9
+
10
+ Bundler/OrderedGems:
11
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.0] - 2022-01-26
3
+ ## [0.1.0] - 2022-06-24
4
+
5
+ - Add basic support for interacting with USDA FDC endpoints
6
+
7
+ ## [0.0.1] - 2022-01-26
4
8
 
5
9
  - Initial release
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # UsdaFdc
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/usda_fdc`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
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,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helpers/configurable'
4
+
5
+ module UsdaFdc
6
+ extend Configurable
7
+
8
+ define_setting :api_key
9
+ 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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UsdaFdc
4
- VERSION = '0.0.1'
4
+ VERSION = '0.1.0'
5
5
  end
data/lib/usda_fdc.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'usda_fdc/version'
4
+ require_relative 'usda_fdc/configuration'
5
+ require_relative 'usda_fdc/client'
4
6
 
5
- module UsdaFdc
6
- end
7
+ module UsdaFdc; end
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.1
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-01-27 00:00:00.000000000 Z
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