red_cap 0.10.2 → 0.12.0

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: 6fc5286a5227f33663a6a68e819fe69927d553f2471ddbac24188fc8ad37bc2d
4
- data.tar.gz: 42f7e00abc8da8759f64ae352c5c246194d0b31814f46aeab2643d1ea49d41d1
3
+ metadata.gz: 88f8115fbb2bfe8348227c0d986562831b9a80127652e01b01772dab3079b614
4
+ data.tar.gz: f4adaa045e58e265fc2d47dea594a521361653eca94f9114563162cefd4d6092
5
5
  SHA512:
6
- metadata.gz: 90b142fbcd60c041cab4eab6a86d7f3edb64054a293e80a81289d7b77e5f5bebbf3846698b6a0b3683846b03f833f6c43968a1d8ffceae74958580eba2d32f9e
7
- data.tar.gz: 4c57abb84500a0fabb17b4b90d2aa323aef8a3f10321eb293732623e1139a0712e2f8d9c67cfa6865df84cdbdd7320315a023dbedfdb07782508f866fc18a471
6
+ metadata.gz: 8422a83dbbd9380f01c5ad3c171c50c05e7349d1984a8b4d4f387945e666ad2fe7eaceaba969aac37939ac66cc5ab2dd3a7c0682860872a204a369dba67b10b1
7
+ data.tar.gz: 7f0d8b5760d68accf9d2911644b2175768ba68815e54776ca2eae1c766f8ab1c72ebdf35feab3ab14261bf38e29b4149268815d3bd93f0a5bfdd523ea4c1fcd0
data/.gitignore CHANGED
@@ -10,3 +10,5 @@
10
10
 
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
+ .ruby-version
14
+ .ruby-gemset
@@ -0,0 +1,23 @@
1
+ class REDCap
2
+ class Cache
3
+ def self.fetch url, &block
4
+ key = Digest::MD5.hexdigest(url)
5
+
6
+ dir = "tmp/redcap_cache"
7
+ path = dir + "/#{key}"
8
+
9
+ if REDCap.cache && ::File.exists?(path)
10
+ ::File.read(path)
11
+ else
12
+ raw = block.call
13
+
14
+ if REDCap.cache
15
+ FileUtils.mkdir_p dir
16
+ ::File.write(path, raw)
17
+ end
18
+ raw
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -1,5 +1,6 @@
1
1
  require "json"
2
2
  require "faraday"
3
+ require "red_cap/cache"
3
4
 
4
5
  class REDCap
5
6
  class Client
@@ -40,12 +41,13 @@ class REDCap
40
41
  json_api_request(content: "metadata")
41
42
  end
42
43
 
43
- def file record_id, file_id
44
+ def file record_id, file_id, event: nil
44
45
  response = base_request({
45
46
  content: "file",
46
47
  action: "export",
47
48
  record: record_id,
48
49
  field: file_id,
50
+ event: event,
49
51
  })
50
52
  _, type, filename = *response.headers["content-type"].match(/\A(.+); name=\"(.+)\"/)
51
53
  File.new(response.body, type, filename)
@@ -56,15 +58,23 @@ class REDCap
56
58
  private
57
59
 
58
60
  def fetch_study_ids filter=nil
59
- json_api_request(content: "record", fields: "study_id", filterLogic: filter)
60
- .map { |hash| hash["study_id"] }
61
+ json_api_request({
62
+ content: "record",
63
+ fields: "study_id",
64
+ filterLogic: filter,
65
+ }).map { |hash| hash["study_id"] }
61
66
  end
62
67
 
68
+ require "active_support/core_ext/object/to_query"
63
69
  def json_api_request options
64
- response = base_request(options.reverse_merge({
65
- format: "json",
66
- }))
67
- JSON.load(response.body)
70
+ full_url = @url + "?" + options.to_query
71
+ json = Cache.fetch(full_url) do
72
+ response = base_request(options.reverse_merge({
73
+ format: "json",
74
+ }))
75
+ response.body
76
+ end
77
+ JSON.load(json)
68
78
  end
69
79
 
70
80
  def base_request options
@@ -57,7 +57,6 @@ class REDCap
57
57
  end
58
58
  end
59
59
  class Descriptive < Field; end
60
- class Dropdown < Field; end
61
60
  class Sql < Field; end
62
61
 
63
62
  class File < Field
@@ -96,6 +95,8 @@ class REDCap
96
95
  # default "radio" implementation
97
96
  Radio = RadioButtons
98
97
 
98
+ class Dropdown < RadioButtons; end
99
+
99
100
  class Checkboxes < RadioButtons
100
101
  def value responses
101
102
  selected_options(responses).values
@@ -1,3 +1,3 @@
1
1
  class REDCap
2
- VERSION = "0.10.2"
2
+ VERSION = "0.12.0"
3
3
  end
data/lib/red_cap.rb CHANGED
@@ -8,13 +8,21 @@ class REDCap
8
8
  yield self
9
9
  end
10
10
 
11
- attr_accessor :url, :token, :per_page
11
+ attr_accessor :url, :token, :per_page, :cache
12
12
 
13
13
  def per_page
14
14
  @per_page ||= 100
15
15
  end
16
16
  end
17
17
 
18
+ def initialize url: REDCap.url, token: REDCap.token, per_page: REDCap.per_page
19
+ @url = url
20
+ @token = token
21
+ @per_page = per_page
22
+ end
23
+
24
+ attr_accessor :url, :token, :per_page
25
+
18
26
  def form
19
27
  @form ||= Form.new(client.metadata)
20
28
  end
@@ -43,10 +51,8 @@ class REDCap
43
51
  client.delete_records [study_id]
44
52
  end
45
53
 
46
- private
47
-
48
54
  def client
49
- @client ||= Client.new
55
+ @client ||= Client.new(url: url, token: token, per_page: per_page)
50
56
  end
51
57
  end
52
58
 
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.10.2
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-26 00:00:00.000000000 Z
11
+ date: 2024-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -89,8 +89,6 @@ extra_rdoc_files: []
89
89
  files:
90
90
  - ".gitignore"
91
91
  - ".rspec"
92
- - ".ruby-gemset"
93
- - ".ruby-version"
94
92
  - ".travis.yml"
95
93
  - Gemfile
96
94
  - LICENSE.txt
@@ -99,6 +97,7 @@ files:
99
97
  - bin/console
100
98
  - bin/setup
101
99
  - lib/red_cap.rb
100
+ - lib/red_cap/cache.rb
102
101
  - lib/red_cap/client.rb
103
102
  - lib/red_cap/form.rb
104
103
  - lib/red_cap/form/fields.rb
@@ -123,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
122
  - !ruby/object:Gem::Version
124
123
  version: '0'
125
124
  requirements: []
126
- rubygems_version: 3.0.8
125
+ rubygems_version: 3.5.4
127
126
  signing_key:
128
127
  specification_version: 4
129
128
  summary: Library for connecting to REDCap and parsing forms and data
data/.ruby-gemset DELETED
@@ -1 +0,0 @@
1
- red_cap
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- ruby-2.4.3