open_fda_api 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +6 -6
- data/lib/open_fda_api/drugs.rb +9 -5
- data/lib/open_fda_api/query_builder.rb +21 -11
- data/lib/open_fda_api/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98cb0c21f6d0b4f002d0e913b36eab334ed077d5db0e34cab62b7fc02d735f79
|
4
|
+
data.tar.gz: b35f1e48a6be0fa33f2695e01223095ca33dff89e5d97d340e768e86fb4690ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5bbdf5fbd2c3244b6f9952e8caf1734b63c96f4ad4977a276b8fecb3d043ffff1c2f66a5de1afdd477aa8d1dd1caf742634d763d13fd1cfa1bae4cd9c0ded735
|
7
|
+
data.tar.gz: 12ef328cbc3216b3c9643131ca2656c62f91103a062c0ae555c6736a0683dd62dc015b75920db90ca70afe8b19d5d51b21a72cb4afc76fb11dccd575dd6e76b9
|
data/Gemfile.lock
CHANGED
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
|
-
|
40
|
-
drugs_api.adverse_events(
|
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
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
|
data/lib/open_fda_api/drugs.rb
CHANGED
@@ -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
|
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(
|
30
|
+
def adverse_events(search: [], sort: [], count: [], skip: 0)
|
28
31
|
endpoint = "/event.json"
|
29
|
-
query = build_query(
|
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(
|
45
|
-
QueryBuilder.new(search:
|
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 =
|
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
|
-
|
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
|
65
|
-
return "" if
|
67
|
+
def build_query_string(query_type:, query_fields:)
|
68
|
+
return "" if query_fields.empty?
|
66
69
|
|
67
|
-
|
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(",")
|
data/lib/open_fda_api/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2022-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|