cpsc 0.1.0 → 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 +54 -1
- data/README.md +98 -10
- data/cpsc.gemspec +33 -0
- data/lib/cpsc/api.rb +90 -22
- data/lib/cpsc/errors.rb +20 -0
- data/lib/cpsc/network/client.rb +20 -5
- data/lib/cpsc/network/uri_builder.rb +8 -13
- data/lib/cpsc/version.rb +1 -1
- data/lib/cpsc.rb +11 -3
- data/sig/cpsc.rbs +56 -2
- metadata +14 -19
- data/.rubocop.yml +0 -18
- data/Gemfile +0 -16
- data/Gemfile.lock +0 -57
- data/Rakefile +0 -16
- data/lib/cpsc/serializable.rb +0 -23
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 79e4fb98e8c0862ad3ca5338646bc71f37a3e68007debc5c63f0a4761d29b9e3
|
|
4
|
+
data.tar.gz: 59485efc796d14c977dc66d4f3c03f75e7dc350a6ad37675d12b15a2bf55bcb0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9b37602978de9e5acab0822689c3f757d48d0de95f485c4a1d9fff0ac0b77400591452f8dd6eb095a7ddcddfa69b243a9841ebcaa178725fccec4833b62b128f
|
|
7
|
+
data.tar.gz: ab94e3976af65b1d21733feef6e066818d79fdea1e03261180f4d99dd889468fb6e805af650dff2f617f1bc7653e89659da34b174b6d5321bea2f13137c927f6
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,58 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
1
8
|
## [Unreleased]
|
|
2
9
|
|
|
10
|
+
## [0.2.0] - 2026-12-02
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Search filters for `recalls_by` and `each_recall_from`: `from`/`to` recall
|
|
15
|
+
date ranges, `published_from`/`published_to`, `recall_id`, `recall_number`,
|
|
16
|
+
`title`, `product_name`, `product_type`, `manufacturer`, `retailer`,
|
|
17
|
+
`importer`, `distributor` and `upc`.
|
|
18
|
+
- `CPSC::HTTPError` (with `#status` and `#body`) is raised for non-2xx
|
|
19
|
+
responses, and `CPSC::ParseError` for malformed JSON.
|
|
20
|
+
- `each_recall_from` returns an `Enumerator` when called without a block.
|
|
21
|
+
- Connection timeouts (10s open, 60s read), configurable by injecting a
|
|
22
|
+
`CPSC::Network::Client` into `CPSC::API.new(client:)`.
|
|
23
|
+
- Requests send `Accept: application/json` and a `cpsc-ruby/<version>`
|
|
24
|
+
User-Agent.
|
|
25
|
+
- Test suite (Minitest, WebMock and VCR) with 100% line and branch coverage.
|
|
26
|
+
- RBS signatures for the public API.
|
|
27
|
+
- GitHub Actions CI for Ruby 3.3, 3.4 and 4.0, plus Dependabot for Bundler and
|
|
28
|
+
GitHub Actions.
|
|
29
|
+
|
|
30
|
+
### Changed
|
|
31
|
+
|
|
32
|
+
- **Breaking:** requires Ruby 3.3 or newer.
|
|
33
|
+
- **Breaking:** unknown filter keywords raise `ArgumentError`. The API ignores
|
|
34
|
+
unknown parameters and returns every recall, which previously masked typos.
|
|
35
|
+
- **Breaking:** an empty response body returns `[]` (was `{}`), and a malformed
|
|
36
|
+
body raises `CPSC::ParseError` (was `nil`).
|
|
37
|
+
- Dates are sent to the API in ISO 8601 (`YYYY-MM-DD`) format.
|
|
38
|
+
- Development dependencies moved from the gemspec to the `Gemfile`.
|
|
39
|
+
- Gem metadata now points to https://github.com/3zcurdia/cpsc.
|
|
40
|
+
|
|
41
|
+
### Removed
|
|
42
|
+
|
|
43
|
+
- The `CPSC::Serializable` refinement, which overrode `String#to_json` and
|
|
44
|
+
`NilClass#to_json` to *parse* JSON.
|
|
45
|
+
|
|
46
|
+
### Fixed
|
|
47
|
+
|
|
48
|
+
- HTTP errors were parsed as if they were successful responses.
|
|
49
|
+
- `each_recall_from` crashed with `NoMethodError` when the body was not JSON.
|
|
50
|
+
- The RBS signature declared a `Cpsc` module instead of `CPSC`.
|
|
51
|
+
|
|
52
|
+
## [0.1.1] - 2023-03-14
|
|
53
|
+
|
|
54
|
+
- Gem metadata and dependency updates.
|
|
55
|
+
|
|
3
56
|
## [0.1.0] - 2022-04-26
|
|
4
57
|
|
|
5
|
-
- Initial release
|
|
58
|
+
- Initial release.
|
data/README.md
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
|
-
#
|
|
1
|
+
# CPSC
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/3zcurdia/cpsc/actions/workflows/ruby.yml)
|
|
4
|
+
[](https://rubygems.org/gems/cpsc)
|
|
4
5
|
|
|
6
|
+
A small, dependency-free Ruby client for the
|
|
7
|
+
[U.S. Consumer Product Safety Commission](https://www.cpsc.gov/) recalls API
|
|
8
|
+
([saferproducts.gov REST web services](https://www.saferproducts.gov/)).
|
|
9
|
+
|
|
10
|
+
## Requirements
|
|
11
|
+
|
|
12
|
+
Ruby 3.3 or newer (tested on 3.3, 3.4 and 4.0).
|
|
5
13
|
|
|
6
14
|
## Installation
|
|
7
15
|
|
|
8
16
|
Add this line to your application's Gemfile:
|
|
9
17
|
|
|
10
18
|
```ruby
|
|
11
|
-
gem
|
|
19
|
+
gem "cpsc"
|
|
12
20
|
```
|
|
13
21
|
|
|
14
22
|
And then execute:
|
|
@@ -21,26 +29,106 @@ Or install it yourself as:
|
|
|
21
29
|
|
|
22
30
|
## Usage
|
|
23
31
|
|
|
24
|
-
|
|
32
|
+
```ruby
|
|
33
|
+
require "cpsc"
|
|
34
|
+
|
|
35
|
+
# Every recall announced in a calendar year
|
|
36
|
+
CPSC.recalls_by(year: 2022)
|
|
37
|
+
|
|
38
|
+
# A custom recall date range (Date objects or "YYYY-MM-DD" strings)
|
|
39
|
+
CPSC.recalls_by(from: Date.new(2023, 1, 1), to: "2023-01-31")
|
|
40
|
+
|
|
41
|
+
# Recalls updated within a date range
|
|
42
|
+
CPSC.recalls_by(published_from: "2026-11-01", published_to: "2026-11-30")
|
|
43
|
+
|
|
44
|
+
# Search filters can be combined
|
|
45
|
+
CPSC.recalls_by(product_name: "stroller", manufacturer: "Graco")
|
|
46
|
+
|
|
47
|
+
# A single recall
|
|
48
|
+
CPSC.recalls_by(recall_number: "23090").first[:Title]
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Recalls are returned as an `Array` of `Hash`es with the API's field names as
|
|
52
|
+
`Symbol` keys (`:RecallID`, `:RecallNumber`, `:RecallDate`, `:Title`,
|
|
53
|
+
`:Products`, `:Hazards`, `:Remedies`, ...).
|
|
54
|
+
|
|
55
|
+
To iterate instead, use `each_recall_from`. Without a block it returns an
|
|
56
|
+
`Enumerator`:
|
|
25
57
|
|
|
26
58
|
```ruby
|
|
27
|
-
|
|
59
|
+
CPSC.each_recall_from(year: 2023) { |recall| puts recall[:Title] }
|
|
60
|
+
|
|
61
|
+
CPSC.each_recall_from(product_type: "Cribs").lazy.map { |recall| recall[:URL] }.first(5)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Supported filters
|
|
65
|
+
|
|
66
|
+
| Keyword | API parameter |
|
|
67
|
+
| ----------------- | ---------------------- |
|
|
68
|
+
| `year` | `RecallDateStart`/`End` for the whole year |
|
|
69
|
+
| `from`, `to` | `RecallDateStart`, `RecallDateEnd` |
|
|
70
|
+
| `published_from`, `published_to` | `LastPublishDateStart`, `LastPublishDateEnd` |
|
|
71
|
+
| `recall_id` | `RecallID` |
|
|
72
|
+
| `recall_number` | `RecallNumber` |
|
|
73
|
+
| `title` | `RecallTitle` |
|
|
74
|
+
| `product_name` | `ProductName` |
|
|
75
|
+
| `product_type` | `ProductType` |
|
|
76
|
+
| `manufacturer` | `Manufacturer` |
|
|
77
|
+
| `retailer` | `Retailer` |
|
|
78
|
+
| `importer` | `Importer` |
|
|
79
|
+
| `distributor` | `Distributor` |
|
|
80
|
+
| `upc` | `UPC` |
|
|
81
|
+
|
|
82
|
+
The API silently ignores parameters it does not know and returns *every*
|
|
83
|
+
recall, so unknown keywords raise `ArgumentError` instead of being sent.
|
|
84
|
+
`nil` values are omitted.
|
|
85
|
+
|
|
86
|
+
### Errors
|
|
87
|
+
|
|
88
|
+
All errors inherit from `CPSC::Error`:
|
|
89
|
+
|
|
90
|
+
- `CPSC::HTTPError` – the API answered with a non-2xx status (`#status`, `#body`).
|
|
91
|
+
- `CPSC::ParseError` – the API answered with a body that is not valid JSON.
|
|
92
|
+
|
|
93
|
+
Network failures (timeouts, DNS) surface as the usual `Net::*`/`SocketError`
|
|
94
|
+
exceptions. The default timeouts are 10 seconds to open a connection and 60
|
|
95
|
+
seconds to read. To change them, inject a client:
|
|
96
|
+
|
|
97
|
+
```ruby
|
|
98
|
+
client = CPSC::Network::Client.new(CPSC::API::BASE_URL, open_timeout: 5, read_timeout: 120)
|
|
99
|
+
CPSC::API.new(client: client).recalls_by(year: 2024)
|
|
28
100
|
```
|
|
29
101
|
|
|
30
102
|
## Development
|
|
31
103
|
|
|
32
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then
|
|
104
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then run
|
|
105
|
+
`bundle exec rake` to run the tests and RuboCop. You can also run `bin/console`
|
|
106
|
+
for an interactive prompt.
|
|
107
|
+
|
|
108
|
+
Tests use [VCR](https://github.com/vcr/vcr) cassettes stored in
|
|
109
|
+
`test/fixtures/vcr_cassettes`. Delete a cassette to re-record it against the
|
|
110
|
+
live API.
|
|
33
111
|
|
|
34
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
112
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
113
|
+
release a new version, update the version number in `lib/cpsc/version.rb` and
|
|
114
|
+
the `CHANGELOG.md`, then run `bundle exec rake release`, which will create a git
|
|
115
|
+
tag for the version, push git commits and the created tag, and push the `.gem`
|
|
116
|
+
file to [rubygems.org](https://rubygems.org).
|
|
35
117
|
|
|
36
118
|
## Contributing
|
|
37
119
|
|
|
38
|
-
Bug reports and pull requests are welcome on GitHub at
|
|
120
|
+
Bug reports and pull requests are welcome on GitHub at
|
|
121
|
+
https://github.com/3zcurdia/cpsc. This project is intended to be a safe,
|
|
122
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
|
123
|
+
the [code of conduct](https://github.com/3zcurdia/cpsc/blob/main/CODE_OF_CONDUCT.md).
|
|
39
124
|
|
|
40
125
|
## License
|
|
41
126
|
|
|
42
|
-
The gem is available as open source under the terms of the
|
|
127
|
+
The gem is available as open source under the terms of the
|
|
128
|
+
[MIT License](https://opensource.org/licenses/MIT).
|
|
43
129
|
|
|
44
130
|
## Code of Conduct
|
|
45
131
|
|
|
46
|
-
Everyone interacting in the
|
|
132
|
+
Everyone interacting in the CPSC project's codebases, issue trackers, chat rooms
|
|
133
|
+
and mailing lists is expected to follow the
|
|
134
|
+
[code of conduct](https://github.com/3zcurdia/cpsc/blob/main/CODE_OF_CONDUCT.md).
|
data/cpsc.gemspec
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/cpsc/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "cpsc"
|
|
7
|
+
spec.version = CPSC::VERSION
|
|
8
|
+
spec.authors = ["Luis Ezcurdia Razo"]
|
|
9
|
+
spec.email = ["ing.ezcurdia@gmail.com"]
|
|
10
|
+
spec.license = "MIT"
|
|
11
|
+
|
|
12
|
+
spec.summary = "U.S. Consumer Product Safety Commission (CPSC) recalls API wrapper"
|
|
13
|
+
spec.description = "A dependency-free Ruby client for the U.S. Consumer Product Safety Commission " \
|
|
14
|
+
"recalls REST API (saferproducts.gov)."
|
|
15
|
+
spec.homepage = "https://github.com/3zcurdia/cpsc"
|
|
16
|
+
spec.required_ruby_version = ">= 3.3"
|
|
17
|
+
|
|
18
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
19
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
|
20
|
+
spec.metadata["bug_tracker_uri"] = "#{spec.homepage}/issues"
|
|
21
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
|
22
|
+
|
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
25
|
+
spec.files = Dir.chdir(__dir__) do
|
|
26
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
|
27
|
+
f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|github|rubocop)|Gemfile|Rakefile|mise\.toml|AGENTS\.md)})
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
spec.bindir = "exe"
|
|
31
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
32
|
+
spec.require_paths = ["lib"]
|
|
33
|
+
end
|
data/lib/cpsc/api.rb
CHANGED
|
@@ -1,42 +1,110 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "
|
|
4
|
-
|
|
5
|
-
require_relative "network/client"
|
|
6
|
-
require_relative "network/uri_builder"
|
|
3
|
+
require "date"
|
|
4
|
+
require "json"
|
|
7
5
|
|
|
8
6
|
module CPSC
|
|
7
|
+
# Wrapper around the saferproducts.gov Recall REST web service.
|
|
9
8
|
class API
|
|
10
|
-
|
|
9
|
+
BASE_URL = "https://www.saferproducts.gov"
|
|
10
|
+
RECALLS_PATH = "/RestWebServices/Recall"
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
# Keyword filters accepted by #recalls_by, mapped to the API query parameter names.
|
|
13
|
+
FILTERS = {
|
|
14
|
+
recall_id: "RecallID",
|
|
15
|
+
recall_number: "RecallNumber",
|
|
16
|
+
title: "RecallTitle",
|
|
17
|
+
product_name: "ProductName",
|
|
18
|
+
product_type: "ProductType",
|
|
19
|
+
manufacturer: "Manufacturer",
|
|
20
|
+
retailer: "Retailer",
|
|
21
|
+
importer: "Importer",
|
|
22
|
+
distributor: "Distributor",
|
|
23
|
+
upc: "UPC"
|
|
24
|
+
}.freeze
|
|
25
|
+
|
|
26
|
+
# Date range filters accepted by #recalls_by, mapped to the API query parameter names.
|
|
27
|
+
DATE_FILTERS = {
|
|
28
|
+
from: "RecallDateStart",
|
|
29
|
+
to: "RecallDateEnd",
|
|
30
|
+
published_from: "LastPublishDateStart",
|
|
31
|
+
published_to: "LastPublishDateEnd"
|
|
32
|
+
}.freeze
|
|
33
|
+
|
|
34
|
+
def self.recalls_by(**)
|
|
35
|
+
new.recalls_by(**)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.each_recall_from(**, &)
|
|
39
|
+
new.each_recall_from(**, &)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def initialize(client: Network::Client.new(BASE_URL))
|
|
43
|
+
@client = client
|
|
14
44
|
end
|
|
15
45
|
|
|
16
|
-
|
|
17
|
-
|
|
46
|
+
# Returns an Array of recall Hashes (with Symbol keys) matching the filters.
|
|
47
|
+
#
|
|
48
|
+
# recalls_by(year: 2022)
|
|
49
|
+
# recalls_by(from: Date.new(2023, 1, 1), to: "2023-01-31")
|
|
50
|
+
# recalls_by(product_name: "stroller", manufacturer: "Graco")
|
|
51
|
+
#
|
|
52
|
+
# Raises ArgumentError for unknown filters, CPSC::HTTPError for non-2xx
|
|
53
|
+
# responses and CPSC::ParseError for malformed bodies.
|
|
54
|
+
def recalls_by(**filters)
|
|
55
|
+
response = client.get(RECALLS_PATH, query: query(**filters))
|
|
56
|
+
raise HTTPError.new(response.code, response.body) unless response.is_a?(Net::HTTPSuccess)
|
|
57
|
+
|
|
58
|
+
parse(response.body)
|
|
18
59
|
end
|
|
19
60
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
61
|
+
# Yields each recall matching the filters, or returns an Enumerator when no block is given.
|
|
62
|
+
def each_recall_from(**filters, &block)
|
|
63
|
+
return enum_for(__method__, **filters) unless block
|
|
64
|
+
|
|
65
|
+
recalls_by(**filters).each(&block)
|
|
25
66
|
end
|
|
26
67
|
|
|
27
68
|
private
|
|
28
69
|
|
|
29
70
|
attr_reader :client
|
|
30
71
|
|
|
31
|
-
def query(**
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
72
|
+
def query(year: nil, **filters)
|
|
73
|
+
params = { format: "json" }
|
|
74
|
+
params.merge!(year_range(year, filters)) if year
|
|
75
|
+
filters.each do |key, value|
|
|
76
|
+
params[parameter_for(key)] = DATE_FILTERS.key?(key) ? format_date(value) : value
|
|
77
|
+
end
|
|
78
|
+
params
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def year_range(year, filters)
|
|
82
|
+
raise ArgumentError, "year: cannot be combined with from: or to:" if filters.key?(:from) || filters.key?(:to)
|
|
83
|
+
|
|
84
|
+
year = Integer(year)
|
|
85
|
+
{ DATE_FILTERS[:from] => "#{year}-01-01", DATE_FILTERS[:to] => "#{year}-12-31" }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def parameter_for(key)
|
|
89
|
+
FILTERS.fetch(key) do
|
|
90
|
+
DATE_FILTERS.fetch(key) { raise ArgumentError, "unknown filter: #{key.inspect}" }
|
|
38
91
|
end
|
|
39
|
-
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def format_date(value)
|
|
95
|
+
return value.strftime("%Y-%m-%d") if value.respond_to?(:strftime)
|
|
96
|
+
|
|
97
|
+
Date.iso8601(value.to_s).iso8601
|
|
98
|
+
rescue Date::Error
|
|
99
|
+
raise ArgumentError, "invalid date: #{value.inspect} (expected a Date or YYYY-MM-DD)"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def parse(body)
|
|
103
|
+
return [] if body.nil? || body.strip.empty?
|
|
104
|
+
|
|
105
|
+
JSON.parse(body, symbolize_names: true)
|
|
106
|
+
rescue JSON::ParserError => e
|
|
107
|
+
raise ParseError, "invalid JSON from CPSC API: #{e.message}"
|
|
40
108
|
end
|
|
41
109
|
end
|
|
42
110
|
end
|
data/lib/cpsc/errors.rb
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CPSC
|
|
4
|
+
# Base class for every error raised by this gem.
|
|
5
|
+
class Error < StandardError; end
|
|
6
|
+
|
|
7
|
+
# Raised when the API responds with a non-2xx status.
|
|
8
|
+
class HTTPError < Error
|
|
9
|
+
attr_reader :status, :body
|
|
10
|
+
|
|
11
|
+
def initialize(status, body = nil)
|
|
12
|
+
@status = status.to_i
|
|
13
|
+
@body = body
|
|
14
|
+
super("CPSC API responded with HTTP #{status}")
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Raised when the API responds with a body that is not valid JSON.
|
|
19
|
+
class ParseError < Error; end
|
|
20
|
+
end
|
data/lib/cpsc/network/client.rb
CHANGED
|
@@ -1,16 +1,31 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "net/http"
|
|
4
|
+
|
|
3
5
|
module CPSC
|
|
4
6
|
module Network
|
|
5
|
-
#
|
|
7
|
+
# Performs HTTP requests against the CPSC API.
|
|
6
8
|
class Client
|
|
7
|
-
|
|
9
|
+
DEFAULT_OPEN_TIMEOUT = 10
|
|
10
|
+
DEFAULT_READ_TIMEOUT = 60
|
|
11
|
+
|
|
12
|
+
attr_reader :open_timeout, :read_timeout
|
|
13
|
+
|
|
14
|
+
def initialize(base_url, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT)
|
|
8
15
|
@uri_builder = UriBuilder.new(base_url)
|
|
16
|
+
@open_timeout = open_timeout
|
|
17
|
+
@read_timeout = read_timeout
|
|
9
18
|
end
|
|
10
19
|
|
|
11
|
-
def get(path = "",
|
|
12
|
-
uri = @uri_builder.build(path,
|
|
13
|
-
Net::HTTP.
|
|
20
|
+
def get(path = "", query: {})
|
|
21
|
+
uri = @uri_builder.build(path, query:)
|
|
22
|
+
request = Net::HTTP::Get.new(uri)
|
|
23
|
+
request["Accept"] = "application/json"
|
|
24
|
+
request["User-Agent"] = "cpsc-ruby/#{VERSION}"
|
|
25
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https",
|
|
26
|
+
open_timeout:, read_timeout:) do |http|
|
|
27
|
+
http.request(request)
|
|
28
|
+
end
|
|
14
29
|
end
|
|
15
30
|
end
|
|
16
31
|
end
|
|
@@ -1,28 +1,23 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "uri"
|
|
4
|
+
|
|
3
5
|
module CPSC
|
|
4
6
|
module Network
|
|
5
|
-
#
|
|
7
|
+
# Builds request URIs relative to a base URL.
|
|
6
8
|
class UriBuilder
|
|
9
|
+
attr_reader :base_url
|
|
10
|
+
|
|
7
11
|
def initialize(base_url)
|
|
8
12
|
@base_url = base_url
|
|
9
13
|
end
|
|
10
14
|
|
|
11
|
-
def build(path,
|
|
15
|
+
def build(path, query: {})
|
|
12
16
|
uri = URI.join(base_url, path)
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
params = query.compact
|
|
18
|
+
uri.query = URI.encode_www_form(params) unless params.empty?
|
|
15
19
|
uri
|
|
16
20
|
end
|
|
17
|
-
|
|
18
|
-
private
|
|
19
|
-
|
|
20
|
-
attr_reader :base_url
|
|
21
|
-
|
|
22
|
-
def query_items(**options)
|
|
23
|
-
query_items = options.fetch(:query, {}).compact
|
|
24
|
-
URI.encode_www_form(query_items)
|
|
25
|
-
end
|
|
26
21
|
end
|
|
27
22
|
end
|
|
28
23
|
end
|
data/lib/cpsc/version.rb
CHANGED
data/lib/cpsc.rb
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "cpsc/version"
|
|
4
|
+
require_relative "cpsc/errors"
|
|
5
|
+
require_relative "cpsc/network/uri_builder"
|
|
6
|
+
require_relative "cpsc/network/client"
|
|
4
7
|
require_relative "cpsc/api"
|
|
5
8
|
|
|
9
|
+
# Ruby client for the U.S. Consumer Product Safety Commission recalls API.
|
|
6
10
|
module CPSC
|
|
7
|
-
|
|
11
|
+
# Fetches recalls matching the given filters. See CPSC::API#recalls_by.
|
|
12
|
+
def self.recalls_by(**)
|
|
13
|
+
API.recalls_by(**)
|
|
14
|
+
end
|
|
8
15
|
|
|
9
|
-
|
|
10
|
-
|
|
16
|
+
# Iterates over recalls matching the given filters. See CPSC::API#each_recall_from.
|
|
17
|
+
def self.each_recall_from(**, &)
|
|
18
|
+
API.each_recall_from(**, &)
|
|
11
19
|
end
|
|
12
20
|
end
|
data/sig/cpsc.rbs
CHANGED
|
@@ -1,4 +1,58 @@
|
|
|
1
|
-
module
|
|
1
|
+
module CPSC
|
|
2
2
|
VERSION: String
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
type recall = Hash[Symbol, untyped]
|
|
5
|
+
|
|
6
|
+
def self.recalls_by: (**untyped filters) -> Array[recall]
|
|
7
|
+
def self.each_recall_from: (**untyped filters) { (recall) -> void } -> Array[recall]
|
|
8
|
+
| (**untyped filters) -> Enumerator[recall, Array[recall]]
|
|
9
|
+
|
|
10
|
+
class Error < StandardError
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class HTTPError < Error
|
|
14
|
+
attr_reader status: Integer
|
|
15
|
+
attr_reader body: String?
|
|
16
|
+
|
|
17
|
+
def initialize: (String | Integer status, ?String? body) -> void
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
class ParseError < Error
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class API
|
|
24
|
+
BASE_URL: String
|
|
25
|
+
RECALLS_PATH: String
|
|
26
|
+
FILTERS: Hash[Symbol, String]
|
|
27
|
+
DATE_FILTERS: Hash[Symbol, String]
|
|
28
|
+
|
|
29
|
+
def self.recalls_by: (**untyped filters) -> Array[recall]
|
|
30
|
+
def self.each_recall_from: (**untyped filters) { (recall) -> void } -> Array[recall]
|
|
31
|
+
| (**untyped filters) -> Enumerator[recall, Array[recall]]
|
|
32
|
+
|
|
33
|
+
def initialize: (?client: Network::Client) -> void
|
|
34
|
+
def recalls_by: (?year: Integer | String | nil, **untyped filters) -> Array[recall]
|
|
35
|
+
def each_recall_from: (**untyped filters) { (recall) -> void } -> Array[recall]
|
|
36
|
+
| (**untyped filters) -> Enumerator[recall, Array[recall]]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
module Network
|
|
40
|
+
class UriBuilder
|
|
41
|
+
attr_reader base_url: String
|
|
42
|
+
|
|
43
|
+
def initialize: (String base_url) -> void
|
|
44
|
+
def build: (String path, ?query: Hash[untyped, untyped]) -> URI::Generic
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
class Client
|
|
48
|
+
DEFAULT_OPEN_TIMEOUT: Integer
|
|
49
|
+
DEFAULT_READ_TIMEOUT: Integer
|
|
50
|
+
|
|
51
|
+
attr_reader open_timeout: Integer
|
|
52
|
+
attr_reader read_timeout: Integer
|
|
53
|
+
|
|
54
|
+
def initialize: (String base_url, ?open_timeout: Integer, ?read_timeout: Integer) -> void
|
|
55
|
+
def get: (?String path, ?query: Hash[untyped, untyped]) -> Net::HTTPResponse
|
|
56
|
+
end
|
|
57
|
+
end
|
|
4
58
|
end
|
metadata
CHANGED
|
@@ -1,46 +1,42 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: cpsc
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
- Luis Ezcurdia
|
|
8
|
-
autorequire:
|
|
7
|
+
- Luis Ezcurdia Razo
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies: []
|
|
13
|
-
description: U.S. Consumer Product Safety Commission
|
|
12
|
+
description: A dependency-free Ruby client for the U.S. Consumer Product Safety Commission
|
|
13
|
+
recalls REST API (saferproducts.gov).
|
|
14
14
|
email:
|
|
15
15
|
- ing.ezcurdia@gmail.com
|
|
16
16
|
executables: []
|
|
17
17
|
extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
|
19
19
|
files:
|
|
20
|
-
- ".rubocop.yml"
|
|
21
20
|
- CHANGELOG.md
|
|
22
21
|
- CODE_OF_CONDUCT.md
|
|
23
|
-
- Gemfile
|
|
24
|
-
- Gemfile.lock
|
|
25
22
|
- LICENSE.txt
|
|
26
23
|
- README.md
|
|
27
|
-
-
|
|
24
|
+
- cpsc.gemspec
|
|
28
25
|
- lib/cpsc.rb
|
|
29
26
|
- lib/cpsc/api.rb
|
|
27
|
+
- lib/cpsc/errors.rb
|
|
30
28
|
- lib/cpsc/network/client.rb
|
|
31
29
|
- lib/cpsc/network/uri_builder.rb
|
|
32
|
-
- lib/cpsc/serializable.rb
|
|
33
30
|
- lib/cpsc/version.rb
|
|
34
31
|
- sig/cpsc.rbs
|
|
35
|
-
homepage: https://github.com/
|
|
32
|
+
homepage: https://github.com/3zcurdia/cpsc
|
|
36
33
|
licenses:
|
|
37
34
|
- MIT
|
|
38
35
|
metadata:
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
36
|
+
source_code_uri: https://github.com/3zcurdia/cpsc
|
|
37
|
+
changelog_uri: https://github.com/3zcurdia/cpsc/blob/main/CHANGELOG.md
|
|
38
|
+
bug_tracker_uri: https://github.com/3zcurdia/cpsc/issues
|
|
42
39
|
rubygems_mfa_required: 'true'
|
|
43
|
-
post_install_message:
|
|
44
40
|
rdoc_options: []
|
|
45
41
|
require_paths:
|
|
46
42
|
- lib
|
|
@@ -48,15 +44,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
48
44
|
requirements:
|
|
49
45
|
- - ">="
|
|
50
46
|
- !ruby/object:Gem::Version
|
|
51
|
-
version: '3.
|
|
47
|
+
version: '3.3'
|
|
52
48
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
49
|
requirements:
|
|
54
50
|
- - ">="
|
|
55
51
|
- !ruby/object:Gem::Version
|
|
56
52
|
version: '0'
|
|
57
53
|
requirements: []
|
|
58
|
-
rubygems_version:
|
|
59
|
-
signing_key:
|
|
54
|
+
rubygems_version: 4.0.20
|
|
60
55
|
specification_version: 4
|
|
61
|
-
summary: U.S. CPSC API wrapper
|
|
56
|
+
summary: U.S. Consumer Product Safety Commission (CPSC) recalls API wrapper
|
|
62
57
|
test_files: []
|
data/.rubocop.yml
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
AllCops:
|
|
2
|
-
TargetRubyVersion: 3.0
|
|
3
|
-
NewCops: enable
|
|
4
|
-
SuggestExtensions: false
|
|
5
|
-
|
|
6
|
-
Style/StringLiterals:
|
|
7
|
-
Enabled: true
|
|
8
|
-
EnforcedStyle: double_quotes
|
|
9
|
-
|
|
10
|
-
Style/StringLiteralsInInterpolation:
|
|
11
|
-
Enabled: true
|
|
12
|
-
EnforcedStyle: double_quotes
|
|
13
|
-
|
|
14
|
-
Layout/LineLength:
|
|
15
|
-
Max: 120
|
|
16
|
-
|
|
17
|
-
Style/Documentation:
|
|
18
|
-
Enabled: false
|
data/Gemfile
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
source "https://rubygems.org"
|
|
4
|
-
|
|
5
|
-
# Specify your gem's dependencies in cpsc.gemspec
|
|
6
|
-
gemspec
|
|
7
|
-
|
|
8
|
-
gem "rake", "~> 13.0"
|
|
9
|
-
|
|
10
|
-
gem "minitest", "~> 5.0"
|
|
11
|
-
|
|
12
|
-
gem "rubocop", "~> 1.21"
|
|
13
|
-
|
|
14
|
-
gem "vcr", "~> 6.1"
|
|
15
|
-
|
|
16
|
-
gem "webmock", "~> 3.14"
|
data/Gemfile.lock
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
PATH
|
|
2
|
-
remote: .
|
|
3
|
-
specs:
|
|
4
|
-
cpsc (0.1.0)
|
|
5
|
-
|
|
6
|
-
GEM
|
|
7
|
-
remote: https://rubygems.org/
|
|
8
|
-
specs:
|
|
9
|
-
addressable (2.8.1)
|
|
10
|
-
public_suffix (>= 2.0.2, < 6.0)
|
|
11
|
-
ast (2.4.2)
|
|
12
|
-
crack (0.4.5)
|
|
13
|
-
rexml
|
|
14
|
-
hashdiff (1.0.1)
|
|
15
|
-
json (2.6.3)
|
|
16
|
-
minitest (5.18.0)
|
|
17
|
-
parallel (1.22.1)
|
|
18
|
-
parser (3.2.1.1)
|
|
19
|
-
ast (~> 2.4.1)
|
|
20
|
-
public_suffix (5.0.1)
|
|
21
|
-
rainbow (3.1.1)
|
|
22
|
-
rake (13.0.6)
|
|
23
|
-
regexp_parser (2.7.0)
|
|
24
|
-
rexml (3.2.5)
|
|
25
|
-
rubocop (1.48.1)
|
|
26
|
-
json (~> 2.3)
|
|
27
|
-
parallel (~> 1.10)
|
|
28
|
-
parser (>= 3.2.0.0)
|
|
29
|
-
rainbow (>= 2.2.2, < 4.0)
|
|
30
|
-
regexp_parser (>= 1.8, < 3.0)
|
|
31
|
-
rexml (>= 3.2.5, < 4.0)
|
|
32
|
-
rubocop-ast (>= 1.26.0, < 2.0)
|
|
33
|
-
ruby-progressbar (~> 1.7)
|
|
34
|
-
unicode-display_width (>= 2.4.0, < 3.0)
|
|
35
|
-
rubocop-ast (1.27.0)
|
|
36
|
-
parser (>= 3.2.1.0)
|
|
37
|
-
ruby-progressbar (1.13.0)
|
|
38
|
-
unicode-display_width (2.4.2)
|
|
39
|
-
vcr (6.1.0)
|
|
40
|
-
webmock (3.18.1)
|
|
41
|
-
addressable (>= 2.8.0)
|
|
42
|
-
crack (>= 0.3.2)
|
|
43
|
-
hashdiff (>= 0.4.0, < 2.0.0)
|
|
44
|
-
|
|
45
|
-
PLATFORMS
|
|
46
|
-
arm64-darwin-22
|
|
47
|
-
|
|
48
|
-
DEPENDENCIES
|
|
49
|
-
cpsc!
|
|
50
|
-
minitest (~> 5.0)
|
|
51
|
-
rake (~> 13.0)
|
|
52
|
-
rubocop (~> 1.21)
|
|
53
|
-
vcr (~> 6.1)
|
|
54
|
-
webmock (~> 3.14)
|
|
55
|
-
|
|
56
|
-
BUNDLED WITH
|
|
57
|
-
2.4.6
|
data/Rakefile
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "bundler/gem_tasks"
|
|
4
|
-
require "rake/testtask"
|
|
5
|
-
|
|
6
|
-
Rake::TestTask.new(:test) do |t|
|
|
7
|
-
t.libs << "test"
|
|
8
|
-
t.libs << "lib"
|
|
9
|
-
t.test_files = FileList["test/**/test_*.rb"]
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
require "rubocop/rake_task"
|
|
13
|
-
|
|
14
|
-
RuboCop::RakeTask.new
|
|
15
|
-
|
|
16
|
-
task default: %i[test rubocop]
|
data/lib/cpsc/serializable.rb
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "json"
|
|
4
|
-
|
|
5
|
-
module CPSC
|
|
6
|
-
module Serializable
|
|
7
|
-
refine NilClass do
|
|
8
|
-
def to_json(**_options)
|
|
9
|
-
self
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
refine String do
|
|
14
|
-
def to_json(**options)
|
|
15
|
-
return {} if empty?
|
|
16
|
-
|
|
17
|
-
JSON.parse(self, **options)
|
|
18
|
-
rescue JSON::ParserError
|
|
19
|
-
nil
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
end
|