open_fda_api 0.0.4 → 0.0.5

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: c2a7513c32091c436f4641cf0e32a64b51e0ad8ae1f600579a54526c868ece7c
4
- data.tar.gz: 797198f936396fbe4a1610996ab7a58fe2fc744017ed4634c0347f345659ce15
3
+ metadata.gz: b43a53d3d7cd5c3390f024979ac198ba6ee4d79ac0bd43cdf60caaaebfe230bc
4
+ data.tar.gz: 7720bc8e73810774e0a88b2b0579b3cfa91d320952d6ab090dac1215c596164e
5
5
  SHA512:
6
- metadata.gz: 468e135f99f59470fdad06055b1849978f9b95fe40aa4b415a8b9485ecc6a17fa4b18f23a06e233dd108f193e5cf2ef335756e327be8ecf5eebd94fd2b8ccb4a
7
- data.tar.gz: 2d42fdf47f92181a0d4ebaaaae6eab84c8d5e868ffec47fd2cfa30b41e66ac036bd2c0bb9cdf40eaf0fc1b5f597ad16921a786c5a883c60561a81f594b989556
6
+ metadata.gz: c2616cbb3fa9a3c648d6e46084a81e0e5f03e14ab57ca5b08628a3cfb7e16b1fa3b99fddea95d4e1b5ba749e4e946092ccb20b0e5caa6806ba3769a3b95d6839
7
+ data.tar.gz: 902d27c4d41762a2557f5ac2a6126df84d3b71ffbe7b82a209210bb88b1acc5783e77f35506ec4d4f50758399a8adfb782c110a1f1b022c8eb975d6c2641b9d5
data/CHANGELOG.md CHANGED
@@ -1,9 +1,12 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.0.5] - 2022-01-20
4
+ - Validate, against search fields given to us from openFDA API, when building queries.
5
+
3
6
  ## [0.0.4] - 2022-01-19
4
- - Fix version string in changelog
5
- - Update Query Builder to group search arguments properly
6
- - Update Query Builder to replace spaces with pluses in search values as openFDA API documentation requires
7
+ - Fix version string in changelog.
8
+ - Update Query Builder to group search arguments properly.
9
+ - Update Query Builder to replace spaces with pluses in search values as openFDA API documentation requires.
7
10
 
8
11
  ## [0.0.3] - 2022-01-19
9
12
  - Update version again because CHANGELOG wasn't updated along with the 0.0.2 release.
data/Gemfile.lock CHANGED
@@ -7,10 +7,15 @@ GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.2)
10
+ coderay (1.1.3)
10
11
  diff-lcs (1.5.0)
12
+ method_source (1.0.0)
11
13
  parallel (1.21.0)
12
14
  parser (3.1.0.0)
13
15
  ast (~> 2.4.1)
16
+ pry (0.14.1)
17
+ coderay (~> 1.1)
18
+ method_source (~> 1.0)
14
19
  rainbow (3.1.1)
15
20
  rake (13.0.6)
16
21
  regexp_parser (2.2.0)
@@ -47,6 +52,7 @@ PLATFORMS
47
52
 
48
53
  DEPENDENCIES
49
54
  open_fda_api!
55
+ pry
50
56
  rake (~> 13.0)
51
57
  rspec (~> 3.0)
52
58
  rubocop (~> 1.7)
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "net/http"
4
4
  require "json"
5
+ require "yaml"
5
6
  require "open_fda_api/query_builder"
6
7
 
7
8
  module OpenFdaApi
@@ -25,19 +26,23 @@ module OpenFdaApi
25
26
  # @return Response from the API parsed as JSON
26
27
  def adverse_events(search_arguments: [])
27
28
  endpoint = "/event.json"
28
- query = build_query(search_arguments)
29
+ query = build_query(search_arguments, self.class.valid_adverse_events_fields)
29
30
  url = build_url(endpoint, query)
30
31
  make_request(url)
31
32
  end
32
33
 
34
+ def self.valid_adverse_events_fields
35
+ @valid_adverse_events_fields ||= ::YAML.load_file("#{__dir__}/adverse_events_fields.yml")
36
+ end
37
+
33
38
  private
34
39
 
35
40
  def build_url(endpoint, query)
36
41
  URI::HTTPS.build(host: @host, path: @path_base + endpoint, query: query)
37
42
  end
38
43
 
39
- def build_query(search_arguments)
40
- QueryBuilder.new(search: search_arguments).build_query
44
+ def build_query(search_arguments, valid_search_fields)
45
+ QueryBuilder.new(search: search_arguments, valid_search_fields: valid_search_fields).build_query
41
46
  end
42
47
 
43
48
  def make_request(url)
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenFdaApi
4
+ class InvalidQueryArgument < ArgumentError; end
5
+
4
6
  # A helper to build queries against the openFDA API
5
7
  #
6
8
  # The API supports five query parameters. The basic building block of queries is the search parameter.
@@ -24,7 +26,8 @@ module OpenFdaApi
24
26
  # is 25000. See Paging if you require paging through larger result sets.
25
27
  class QueryBuilder
26
28
  # @param [Array<Hash>] search
27
- def initialize(search: [])
29
+ def initialize(valid_search_fields:, search: [], sort: [], count: [], skip: 0)
30
+ validate_arguments!(valid_search_fields, search: search, sort: sort, count: count, skip: skip)
28
31
  @search = build_search_string(search)
29
32
  end
30
33
 
@@ -39,6 +42,25 @@ module OpenFdaApi
39
42
 
40
43
  private
41
44
 
45
+ def validate_arguments!(valid_search_fields, search:, sort:, count:, skip:)
46
+ # `search` keys must exist in adverse_events_fields.yml
47
+ invalid_fields = get_invalid_fields(valid_search_fields: valid_search_fields, fields: search)
48
+ raise InvalidQueryArgument, "'search' has invalid fields: #{invalid_fields}" if invalid_fields.any?
49
+
50
+ # `sort` keys must exist in adverse_events_fields.yml
51
+ invalid_fields = get_invalid_fields(valid_search_fields: valid_search_fields, fields: sort)
52
+ raise InvalidQueryArgument, "'sort' has invalid fields: #{invalid_fields}" if invalid_fields.any?
53
+
54
+ # `count` keys must exist in adverse_events_fields.yml
55
+ invalid_fields = get_invalid_fields(valid_search_fields: valid_search_fields, fields: count)
56
+ raise InvalidQueryArgument, "'count' has invalid fields: #{invalid_fields}" if invalid_fields.any?
57
+
58
+ # `count` and `skip` cannot be set at the same time
59
+ return unless count_and_skip_set?(count, skip)
60
+
61
+ raise InvalidQueryArgument, "'count' and 'skip' cannot both be set at the same time!"
62
+ end
63
+
42
64
  def build_search_string(search)
43
65
  return "" if search.empty?
44
66
 
@@ -48,5 +70,20 @@ module OpenFdaApi
48
70
 
49
71
  "search=#{value}"
50
72
  end
73
+
74
+ def get_invalid_fields(valid_search_fields:, fields:)
75
+ fields.map(&:keys).flatten.select do |field|
76
+ if field.include?(".") # nested field (e.g. patient.patientagegroup)
77
+ dig_values = field.split(".").join(",properties,").split(",")
78
+ valid_search_fields["properties"].dig(*dig_values).nil?
79
+ else
80
+ !valid_search_fields["properties"].keys.include?(field.to_s)
81
+ end
82
+ end
83
+ end
84
+
85
+ def count_and_skip_set?(count, skip)
86
+ skip.positive? && !count.empty?
87
+ end
51
88
  end
52
89
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenFdaApi
4
- VERSION = "0.0.4"
4
+ VERSION = "0.0.5"
5
5
  end
data/open_fda_api.gemspec CHANGED
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
28
28
 
29
29
  # Uncomment to register a new dependency of your gem
30
30
  # spec.add_dependency "example-gem", "~> 1.0"
31
+ spec.add_development_dependency "pry"
31
32
 
32
33
  # For more information and examples about making a new gem, checkout our
33
34
  # guide at: https://bundler.io/guides/creating_gem.html
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_fda_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hebron George
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-19 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description:
14
28
  email:
15
29
  - hebrontgeorge@gmail.com