open_fda_api 0.0.5 → 0.0.6

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: b43a53d3d7cd5c3390f024979ac198ba6ee4d79ac0bd43cdf60caaaebfe230bc
4
- data.tar.gz: 7720bc8e73810774e0a88b2b0579b3cfa91d320952d6ab090dac1215c596164e
3
+ metadata.gz: 98cb0c21f6d0b4f002d0e913b36eab334ed077d5db0e34cab62b7fc02d735f79
4
+ data.tar.gz: b35f1e48a6be0fa33f2695e01223095ca33dff89e5d97d340e768e86fb4690ba
5
5
  SHA512:
6
- metadata.gz: c2616cbb3fa9a3c648d6e46084a81e0e5f03e14ab57ca5b08628a3cfb7e16b1fa3b99fddea95d4e1b5ba749e4e946092ccb20b0e5caa6806ba3769a3b95d6839
7
- data.tar.gz: 902d27c4d41762a2557f5ac2a6126df84d3b71ffbe7b82a209210bb88b1acc5783e77f35506ec4d4f50758399a8adfb782c110a1f1b022c8eb975d6c2641b9d5
6
+ metadata.gz: 5bbdf5fbd2c3244b6f9952e8caf1734b63c96f4ad4977a276b8fecb3d043ffff1c2f66a5de1afdd477aa8d1dd1caf742634d763d13fd1cfa1bae4cd9c0ded735
7
+ data.tar.gz: 12ef328cbc3216b3c9643131ca2656c62f91103a062c0ae555c6736a0683dd62dc015b75920db90ca70afe8b19d5d51b21a72cb4afc76fb11dccd575dd6e76b9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- open_fda_api (0.0.4)
4
+ open_fda_api (0.0.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -36,14 +36,14 @@ require 'open_fda_api'
36
36
  client = OpenFdaApi.client
37
37
  drugs_api = client.drugs
38
38
 
39
- arguments = [{"patient.reaction.reactionmeddrapt"=>"fatigue"}, {"occurcountry"=>"ca"}]
40
- drugs_api.adverse_events(search_arguments: arguments) # => {"meta" => {...}, "results" => [...]}
39
+ search_args = [{"patient.patientweight"=>"50", "occurcountry"=>"ca"}]
40
+ drugs_api.adverse_events(search: search_args, skip: 1) # => {"meta" => {...}, "results" => [...]}
41
41
  ```
42
42
 
43
- ### Device (Not Implemented Yet)
44
- ### Food (Not Implemented Yet)
45
- ### Other (Not Implemented Yet)
46
- ### Tobacco (Not Implemented Yet)
43
+ #### Device (Not Implemented Yet)
44
+ #### Food (Not Implemented Yet)
45
+ #### Other (Not Implemented Yet)
46
+ #### Tobacco (Not Implemented Yet)
47
47
 
48
48
 
49
49
 
@@ -22,11 +22,14 @@ module OpenFdaApi
22
22
  # FDA Adverse Event Reporting System (FAERS), a database that contains information on
23
23
  # adverse event and medication error reports submitted to FDA.
24
24
  #
25
- # @param search_arguments [Array<Hash>] Search fields defined in https://open.fda.gov/apis/drug/event/searchable-fields/
25
+ # @param search [Array<Hash>] Search fields defined in https://open.fda.gov/apis/drug/event/searchable-fields/
26
+ # @param sort [Array<Hash>] Sort fields defined in https://open.fda.gov/apis/drug/event/searchable-fields/
27
+ # @param count [Array<Hash>] Count fields defined https://open.fda.gov/apis/drug/event/searchable-fields/
28
+ # @param skip [Integer] Number of results to skip
26
29
  # @return Response from the API parsed as JSON
27
- def adverse_events(search_arguments: [])
30
+ def adverse_events(search: [], sort: [], count: [], skip: 0)
28
31
  endpoint = "/event.json"
29
- query = build_query(search_arguments, self.class.valid_adverse_events_fields)
32
+ query = build_query(search, sort, count, skip, self.class.valid_adverse_events_fields)
30
33
  url = build_url(endpoint, query)
31
34
  make_request(url)
32
35
  end
@@ -41,8 +44,9 @@ module OpenFdaApi
41
44
  URI::HTTPS.build(host: @host, path: @path_base + endpoint, query: query)
42
45
  end
43
46
 
44
- def build_query(search_arguments, valid_search_fields)
45
- QueryBuilder.new(search: search_arguments, valid_search_fields: valid_search_fields).build_query
47
+ def build_query(search, sort, count, skip, valid_search_fields)
48
+ QueryBuilder.new(search: search, sort: sort, count: count, skip: skip,
49
+ valid_search_fields: valid_search_fields).build_query
46
50
  end
47
51
 
48
52
  def make_request(url)
@@ -25,19 +25,22 @@ module OpenFdaApi
25
25
  # Use in combination with limit to paginate results. Currently, the largest allowed value for the skip parameter
26
26
  # is 25000. See Paging if you require paging through larger result sets.
27
27
  class QueryBuilder
28
+ # @param [Hash] valid_search_fields
28
29
  # @param [Array<Hash>] search
30
+ # @param [Array<Hash>] sort
31
+ # @param [Array<Hash>] count
32
+ # @param [Integer] skip
29
33
  def initialize(valid_search_fields:, search: [], sort: [], count: [], skip: 0)
30
34
  validate_arguments!(valid_search_fields, search: search, sort: sort, count: count, skip: skip)
31
- @search = build_search_string(search)
35
+ @search = build_query_string(query_type: "search", query_fields: search)
36
+ @sort = build_query_string(query_type: "sort", query_fields: sort)
37
+ @count = build_query_string(query_type: "count", query_fields: count)
38
+ @skip = build_skip_string(skip)
32
39
  end
33
40
 
34
41
  # @return [String] the query string portion of a request
35
42
  def build_query
36
- # TODO: We currently just build a very basic search string for simple examples like "search=a:b+AND+c:d",
37
- # but it is possible to construct more complex queries and we will need to support that. Sorting, counting,
38
- # setting limits, skipping records, pagination, converting spaces, phrase matching, grouping, and using dates and
39
- # ranges are examples of more complex queries that can be built.
40
- @search
43
+ [@search, @sort, @count, @skip].reject! { |v| v.nil? || v.empty? }.join("&")
41
44
  end
42
45
 
43
46
  private
@@ -61,17 +64,24 @@ module OpenFdaApi
61
64
  raise InvalidQueryArgument, "'count' and 'skip' cannot both be set at the same time!"
62
65
  end
63
66
 
64
- def build_search_string(search)
65
- return "" if search.empty?
67
+ def build_query_string(query_type:, query_fields:)
68
+ return "" if query_fields.empty?
66
69
 
67
- value = search.map do |and_args|
70
+ "#{query_type}=#{build_groupings(query_fields)}"
71
+ end
72
+
73
+ def build_skip_string(skip)
74
+ skip.positive? ? "skip=#{skip}" : ""
75
+ end
76
+
77
+ def build_groupings(fields)
78
+ fields.map do |and_args|
68
79
  "(#{and_args.map { |k, v| "#{k}:#{v.gsub(" ", "+")}" }.join("+AND+")})"
69
80
  end.join("+")
70
-
71
- "search=#{value}"
72
81
  end
73
82
 
74
83
  def get_invalid_fields(valid_search_fields:, fields:)
84
+ # TODO: valid_search_fields define types and we need to check against those
75
85
  fields.map(&:keys).flatten.select do |field|
76
86
  if field.include?(".") # nested field (e.g. patient.patientagegroup)
77
87
  dig_values = field.split(".").join(",properties,").split(",")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenFdaApi
4
- VERSION = "0.0.5"
4
+ VERSION = "0.0.6"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_fda_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
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-20 00:00:00.000000000 Z
11
+ date: 2022-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry