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.new
Redcap.configure do |config|
config.host = "http://yourhost.com"
config.token = 1234
end
redcap = Redcap.new
redcap = Redcap.new host: 'http://yourhost.com', token: 1234
# name the class after your REDCap project
# ex. Survey, Volunteer, Trial, People
class People < Redcap::Record
end
bob = People.find 1
people = People.where id: [1,4,5]
bob = People.where(first_name: 'Bob').first
people = People.all
people = People.select(:first_name, :age)
names = People.pluck(:first_name)
# ['Joe', 'Sal', 'Luke']
ids = People.ids
# [1,2,3, ... ]
total = People.count
# 125
bob = People.where(first_name: 'bob').first
bob.last_name = 'Smith'
bob.save
joe = People.new(first_name: 'Joe', email: 'joe@google.com')
joe.save
joe = People.find(joe.id)
joe.destroy
# 1 (number of records deleted)
joe = People.new(first_name: 'Joe', email: 'joe@google.com')
joe.save
ray = People.new(first_name: 'Ray', email: 'ray@google.com')
ray.save
People.delete_all [joe.id, ray.id]
# 2 (number of records deleted)
over40 = People.gt age: 40
under30 = People.lt age: 30
over40 = People.gte age: 41
under30 = People.lte age: 29
# set log to true on the client
People.client.log = true
Setting 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=ON
If REDCAP_CACHE
is set to ON
, the cache can be manually flushed by calling flush_cache
on the client.
People.client.flush_cache
redcap = 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
RedcapRecord
module 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.