A Ruby gem for interacting with the REDCap API
Add this line to your application's Gemfile:
gem 'redcap', github: 'peterclark/redcap'And then execute:
$ bundle
You can initialize a Redcap instance in one of three ways:
# inside .env file
# REDCAP_HOST=http://yourhost.com
# REDCAP_TOKEN=12345
redcap = Redcap.newRedcap.configure do |config|
config.host = "http://yourhost.com"
config.token = 1234
end
redcap = Redcap.newredcap = Redcap.new host: 'http://yourhost.com', token: 1234# name the class after your REDCap project
# ex. Survey, Volunteer, Trial, People
class People < Redcap::Record
endbob = People.find 1people = People.where id: [1,4,5]bob = People.where(first_name: 'Bob').firstpeople = People.allpeople = People.select(:first_name, :age)names = People.pluck(:first_name)
# ['Joe', 'Sal', 'Luke']ids = People.ids
# [1,2,3, ... ]total = People.count
# 125bob = People.where(first_name: 'bob').first
bob.last_name = 'Smith'
bob.savejoe = People.new(first_name: 'Joe', email: '[email protected]')
joe.savejoe = People.find(joe.id)
joe.destroy
# 1 (number of records deleted)joe = People.new(first_name: 'Joe', email: '[email protected]')
joe.save
ray = People.new(first_name: 'Ray', email: '[email protected]')
ray.save
People.delete_all [joe.id, ray.id]
# 2 (number of records deleted)over40 = People.gt age: 40under30 = People.lt age: 30over40 = People.gte age: 41under30 = People.lte age: 29# set log to true on the client
People.client.log = trueSetting REDCAP_CACHE to ON inside your .env file will cache all calls to Redcap. A full cache flush will occur when any record is updated or created.
# inside .env
# REDCAP_CACHE=ONIf REDCAP_CACHE is set to ON, the cache can be manually flushed by calling flush_cache on the client.
People.client.flush_cacheredcap = Redcap.new
# Get project info
redcap.project
# Get all records and all fields
redcap.records
# Get all records and a subset of fields
redcap.records fields: %w(first_name age)
# Get all records and all fields matching a filter
redcap.records filter: '[age] > 40'
# Get all records and a subset of fields matching a filter
redcap.records fields: %w(email age), filter: '[age] < 35'
# Get records with the given ids and a subset of fields matching a filter
redcap.records records: [1,4], fields: %w(email age), filter: '[age] < 35'- Method chaining
People.where(age: 40).select(:first_name)
- Create
RedcapRecordmodule as alternative to inheritance
include Redcap
- Destroy a record
After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
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.
Bug reports and pull requests are welcome on GitHub at https://github.com/peterclark/redcap.
The gem is available as open source under the terms of the MIT License.