red_cap 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7d5dfa5cee79acd4e53556829793b591d6f63149
4
+ data.tar.gz: 67711da2449a588359bfd5ea063557df95d8538f
5
+ SHA512:
6
+ metadata.gz: e8c93ad56c780a58380aa2dab9082576922dc96002272501b408db283df7b142a8460a0f996a7c99ae1016303018e6625fb1fbaec7aae9e660da7136f42257d5
7
+ data.tar.gz: bddc8d3b30d9233a3123b6e2037c13a9d26842cebc899e58dafb0fa295ee474c9a86a810d18c12ba775739a8162189eaf1fb8832b9728045dbca740984e34f4e
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.3
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in red_cap.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Micah Geisel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # REDCap
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/red_cap`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'red_cap'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install red_cap
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/red_cap.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "red_cap"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,48 @@
1
+ require "json"
2
+ require "faraday"
3
+
4
+ module REDCap
5
+ class Client
6
+ def initialize url: REDCap.url, token: REDCap.token
7
+ @url = url
8
+ @token = token
9
+ end
10
+
11
+ def records
12
+ json_api_request(content: "record")
13
+ end
14
+
15
+ def metadata
16
+ json_api_request(content: "metadata")
17
+ end
18
+
19
+ def file record_id, file_id
20
+ response = base_request({
21
+ content: "file",
22
+ action: "export",
23
+ record: record_id,
24
+ field: file_id,
25
+ })
26
+ _, type, filename = *response.headers["content-type"].match(/\A(.+); name=\"(.+)\"\z/)
27
+ File.new(response.body, type, filename)
28
+ end
29
+
30
+ File = Struct.new(:data, :type, :filename)
31
+
32
+ private
33
+
34
+ def json_api_request options
35
+ response = base_request(options.reverse_merge({
36
+ format: "json",
37
+ }))
38
+ JSON.load(response.body)
39
+ end
40
+
41
+ def base_request options
42
+ connection = Faraday.new(url: @url)
43
+ connection.post(nil, options.reverse_merge({
44
+ token: @token,
45
+ }))
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,155 @@
1
+ require "active_support/core_ext/object/blank"
2
+
3
+ module REDCap
4
+ class Form
5
+ class Field < Struct.new(:form, :attributes, :responses)
6
+ KEYS = [
7
+ :field_name,
8
+ :form_name,
9
+ :section_header,
10
+ :field_type,
11
+ :field_label,
12
+ :select_choices_or_calculations,
13
+ :field_note,
14
+ :text_validation_type_or_show_slider_number,
15
+ :text_validation_min,
16
+ :text_validation_max,
17
+ :identifier,
18
+ :branching_logic,
19
+ :required_field,
20
+ :custom_alignment,
21
+ :question_number,
22
+ :matrix_group_name,
23
+ :matrix_ranking,
24
+ :field_annotation,
25
+ ].each do |key|
26
+ define_method key do
27
+ attributes[key.to_s]
28
+ end
29
+ end
30
+
31
+ def value
32
+ responses[field_name]
33
+ end
34
+
35
+ # field type inquiry methods
36
+ def method_missing method, *args, **kwargs, &block
37
+ if method.to_s.ends_with?("?")
38
+ field_type == method.to_s.chomp("?")
39
+ else
40
+ super
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def associated_fields_for_key key
47
+ form.fields.select do |field|
48
+ field.branching_logic == %{[#{field_name}(#{key})]="1"}
49
+ end
50
+ end
51
+ end
52
+
53
+ class Text < Field; end
54
+ class Notes < Field; end
55
+ class Dropdown < Field; end
56
+ class Sql < Field; end
57
+
58
+ class File < Field
59
+ def value
60
+ if responses[field_name].present?
61
+ field_name
62
+ end
63
+ end
64
+ end
65
+
66
+ class RadioButtons < Field
67
+ def value
68
+ options[responses[field_name]]
69
+ end
70
+
71
+ def options
72
+ select_choices_or_calculations
73
+ .split("|")
74
+ .reduce({}) do |options, pair|
75
+ _, key, value = *pair.match(/\A(\d+),(.+)\z/)
76
+ options.merge key => value
77
+ end
78
+ end
79
+ end
80
+
81
+ # default "radio" implementation
82
+ Radio = RadioButtons
83
+
84
+ class Checkboxes < RadioButtons
85
+ def value
86
+ selected_options.values
87
+ end
88
+
89
+ private
90
+
91
+ def selected_options
92
+ options.select do |key, value|
93
+ responses["#{field_name}___#{key}"] == "1"
94
+ end
95
+ end
96
+ end
97
+
98
+ class CheckboxesWithOther < Checkboxes
99
+ def value
100
+ selected_options.map do |key, value|
101
+ if key == "501" # Other
102
+ "#{value}: #{other_text_field&.value}"
103
+ else
104
+ value
105
+ end
106
+ end
107
+ end
108
+
109
+ def other_text_field
110
+ associated_fields_for_key("501").find(&:text?)
111
+ end
112
+ end
113
+
114
+ # default "checkbox" implementation
115
+ Checkbox = CheckboxesWithOther
116
+
117
+ class CheckboxesWithRadioButtonsOrOther < CheckboxesWithOther
118
+ def value
119
+ radio_values = selected_options.keys.map do |key|
120
+ radio_field_for(key).value
121
+ end
122
+
123
+ Hash[super.zip(radio_values)]
124
+ end
125
+
126
+ private
127
+
128
+ def radio_field_for key
129
+ associated_fields_for_key(key).find(&:radio?)
130
+ end
131
+ end
132
+
133
+ class CheckboxesWithCheckboxesOrOther < CheckboxesWithOther
134
+ def value
135
+ left = selected_options.values
136
+
137
+ right = selected_options.keys.map do |key|
138
+ checkbox_fields_for(key).map(&:value)
139
+ end
140
+
141
+ if selected_options.keys.include?("501")
142
+ right[-1] = [other_text_field&.value]
143
+ end
144
+
145
+ Hash[left.zip(right)]
146
+ end
147
+
148
+ private
149
+
150
+ def checkbox_fields_for key
151
+ associated_fields_for_key(key).select(&:checkbox?)
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,49 @@
1
+ require "red_cap/form/fields"
2
+ require "active_support/core_ext/string/inflections"
3
+
4
+ module REDCap
5
+ class Form
6
+ def initialize data_dictionary, responses
7
+ @data_dictionary = data_dictionary
8
+ @responses = responses
9
+ end
10
+
11
+ attr_reader :data_dictionary, :responses
12
+
13
+ # field accessors
14
+ def method_missing method, *args, **kwargs, &block
15
+ key = method.to_s
16
+ field_class = kwargs[:as]
17
+ if field_class.is_a?(Symbol)
18
+ field_class = lookup_field_class(field_class.to_s)
19
+ end
20
+ if field = find_field(key, field_class)
21
+ field.value
22
+ else
23
+ super
24
+ end
25
+ end
26
+
27
+ def find_field key, field_class=nil
28
+ field = fields.find { |field| field.field_name == key }
29
+ field = field_class.new(self, field.attributes, responses) if field_class
30
+ field
31
+ end
32
+
33
+ def fields
34
+ @fields ||= data_dictionary.map do |attributes|
35
+ klass = lookup_field_class(attributes["field_type"])
36
+ klass.new(self, attributes, responses)
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def lookup_field_class field_type
43
+ self.class.const_get field_type.camelize, false
44
+ rescue NameError
45
+ puts "Unimplemented field type: #{field_type}. Falling back to Text."
46
+ Text
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module REDCap
2
+ VERSION = "0.1.0"
3
+ end
data/lib/red_cap.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "red_cap/version"
2
+ require "red_cap/client"
3
+ require "red_cap/form"
4
+
5
+ module REDCap
6
+ def self.configure
7
+ yield self
8
+ end
9
+
10
+ cattr_accessor :url, :token
11
+ end
12
+
data/red_cap.gemspec ADDED
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "red_cap/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "red_cap"
8
+ spec.version = REDCap::VERSION
9
+ spec.authors = ["Micah Geisel"]
10
+ spec.email = ["micah@botandrose.com"]
11
+
12
+ spec.summary = %q{Library for connecting to REDCap and parsing forms and data}
13
+ spec.description = %q{Library for connecting to REDCap and parsing forms and data}
14
+ spec.homepage = "https://github.com/botandrose/red_cap"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "faraday"
25
+ spec.add_dependency "activesupport"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.16"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: red_cap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Micah Geisel
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Library for connecting to REDCap and parsing forms and data
84
+ email:
85
+ - micah@botandrose.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/red_cap.rb
100
+ - lib/red_cap/client.rb
101
+ - lib/red_cap/form.rb
102
+ - lib/red_cap/form/fields.rb
103
+ - lib/red_cap/version.rb
104
+ - red_cap.gemspec
105
+ homepage: https://github.com/botandrose/red_cap
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.6.14
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Library for connecting to REDCap and parsing forms and data
129
+ test_files: []