red_cap 0.8.0 → 0.10.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aff79d0a3d8f11d798bc3ea044f6e9ecaf6120f104b8b9d619a4c9b4f8f08db3
4
- data.tar.gz: 7171c90ea289a8706c800d0df102065b84cb9cdc7754afdd0c5b4d327925d7a4
3
+ metadata.gz: 6fc5286a5227f33663a6a68e819fe69927d553f2471ddbac24188fc8ad37bc2d
4
+ data.tar.gz: 42f7e00abc8da8759f64ae352c5c246194d0b31814f46aeab2643d1ea49d41d1
5
5
  SHA512:
6
- metadata.gz: 53a38cf69401b1820e148d42d0c32ac748f4b034a4f98bba960c74e808f78f0d6e209a35e7c6df648cc8650ddab6a32f8a204c33de96c73c18b0f85455d89ffa
7
- data.tar.gz: d05eee71134492e2925db33fc85705ed3c77cd167e5d2dbbf9105860f83fc7b3280a099e288f167ab257d32009a08e780625ee35532c817d95ed51ff716fc680
6
+ metadata.gz: 90b142fbcd60c041cab4eab6a86d7f3edb64054a293e80a81289d7b77e5f5bebbf3846698b6a0b3683846b03f833f6c43968a1d8ffceae74958580eba2d32f9e
7
+ data.tar.gz: 4c57abb84500a0fabb17b4b90d2aa323aef8a3f10321eb293732623e1139a0712e2f8d9c67cfa6865df84cdbdd7320315a023dbedfdb07782508f866fc18a471
@@ -1,7 +1,7 @@
1
1
  require "json"
2
2
  require "faraday"
3
3
 
4
- module REDCap
4
+ class REDCap
5
5
  class Client
6
6
  def initialize url: REDCap.url, token: REDCap.token, per_page: REDCap.per_page
7
7
  @url = url
@@ -24,6 +24,18 @@ module REDCap
24
24
  end
25
25
  end
26
26
 
27
+ def find_record study_id
28
+ json_api_request(content: "record", records: study_id).first
29
+ end
30
+
31
+ def save_records records
32
+ json_api_request(content: "record", data: records.to_json, overwriteBehavior: "overwrite")
33
+ end
34
+
35
+ def delete_records study_ids
36
+ json_api_request(content: "record", action: "delete", records: study_ids)
37
+ end
38
+
27
39
  def metadata
28
40
  json_api_request(content: "metadata")
29
41
  end
@@ -35,7 +47,7 @@ module REDCap
35
47
  record: record_id,
36
48
  field: file_id,
37
49
  })
38
- _, type, filename = *response.headers["content-type"].match(/\A(.+); name=\"(.+)\"\z/)
50
+ _, type, filename = *response.headers["content-type"].match(/\A(.+); name=\"(.+)\"/)
39
51
  File.new(response.body, type, filename)
40
52
  end
41
53
 
@@ -60,8 +72,8 @@ module REDCap
60
72
  connection.options.open_timeout = 300
61
73
  connection.options.timeout = 300
62
74
  response = connection.post nil, options.reverse_merge(token: @token)
63
- if error_message = response.body[/<error>(.+?)<\/error>/, 1]
64
- raise Error.new(error_message)
75
+ if response.body =~ /^{"error":"/
76
+ raise Error.new(response.body)
65
77
  end
66
78
  response
67
79
  end
@@ -1,6 +1,6 @@
1
1
  require "active_support/core_ext/object/blank"
2
2
 
3
- module REDCap
3
+ class REDCap
4
4
  class Form
5
5
  class Field < Struct.new(:attributes, :options, :associated_fields)
6
6
  KEYS = [
data/lib/red_cap/form.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require "red_cap/form/fields"
2
2
  require "active_support/core_ext/string/inflections"
3
3
 
4
- module REDCap
4
+ class REDCap
5
5
  class Form
6
6
  def initialize data_dictionary
7
7
  @data_dictionary = data_dictionary
@@ -1,3 +1,3 @@
1
- module REDCap
2
- VERSION = "0.8.0"
1
+ class REDCap
2
+ VERSION = "0.10.2"
3
3
  end
data/lib/red_cap.rb CHANGED
@@ -2,7 +2,7 @@ require "red_cap/version"
2
2
  require "red_cap/client"
3
3
  require "red_cap/form"
4
4
 
5
- module REDCap
5
+ class REDCap
6
6
  class << self
7
7
  def configure
8
8
  yield self
@@ -14,5 +14,39 @@ module REDCap
14
14
  @per_page ||= 100
15
15
  end
16
16
  end
17
+
18
+ def form
19
+ @form ||= Form.new(client.metadata)
20
+ end
21
+
22
+ def find study_id
23
+ client.find_record study_id
24
+ end
25
+
26
+ def all &block
27
+ client.records &block
28
+ end
29
+
30
+ def where conditions, &block
31
+ filters = conditions.reduce([]) do |filters, (field, value)|
32
+ filters << "[#{field}]=#{value}"
33
+ end
34
+ client.records(filters.join(" AND "), &block)
35
+ end
36
+
37
+ def update study_id, attributes
38
+ record = attributes.merge(study_id: study_id).stringify_keys
39
+ client.save_records [record]
40
+ end
41
+
42
+ def delete study_id
43
+ client.delete_records [study_id]
44
+ end
45
+
46
+ private
47
+
48
+ def client
49
+ @client ||= Client.new
50
+ end
17
51
  end
18
52
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: red_cap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.10.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-14 00:00:00.000000000 Z
11
+ date: 2021-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  requirements: []
126
- rubygems_version: 3.0.4
126
+ rubygems_version: 3.0.8
127
127
  signing_key:
128
128
  specification_version: 4
129
129
  summary: Library for connecting to REDCap and parsing forms and data