class REDCap::Client

Constants

File

Public Class Methods

new(url: REDCap.url, token: REDCap.token, per_page: REDCap.per_page) click to toggle source
# File lib/red_cap/client.rb, line 6
def initialize url: REDCap.url, token: REDCap.token, per_page: REDCap.per_page
  @url = url
  @token = token
  @per_page = per_page
end

Public Instance Methods

delete_records(study_ids) click to toggle source
# File lib/red_cap/client.rb, line 35
def delete_records study_ids
  json_api_request(content: "record", action: "delete", records: study_ids)
end
file(record_id, file_id) click to toggle source
# File lib/red_cap/client.rb, line 43
def file record_id, file_id
  response = base_request({
    content: "file",
    action: "export",
    record: record_id,
    field: file_id,
  })
  _, type, filename = *response.headers["content-type"].match(/\A(.+); name=\"(.+)\"\z/)
  File.new(response.body, type, filename)
end
find_record(study_id) click to toggle source
# File lib/red_cap/client.rb, line 27
def find_record study_id
  json_api_request(content: "record", records: study_id).first
end
metadata() click to toggle source
# File lib/red_cap/client.rb, line 39
def metadata
  json_api_request(content: "metadata")
end
records(filter=nil) { |record| ... } click to toggle source
# File lib/red_cap/client.rb, line 12
def records filter=nil
  enumerator = fetch_study_ids(filter).in_groups_of(@per_page, false)
  if block_given?
    enumerator.each do |study_ids|
      json_api_request(content: "record", records: study_ids.join(",")).each do |record|
        yield record
      end
    end
  else
    enumerator.flat_map do |study_ids|
      json_api_request(content: "record", records: study_ids.join(","))
    end
  end
end
save_records(records) click to toggle source
# File lib/red_cap/client.rb, line 31
def save_records records
  json_api_request(content: "record", data: records.to_json, overwriteBehavior: "overwrite")
end

Private Instance Methods

base_request(options) click to toggle source
# File lib/red_cap/client.rb, line 70
def base_request options
  connection = Faraday.new(url: @url)
  connection.options.open_timeout = 300
  connection.options.timeout = 300
  response = connection.post nil, options.reverse_merge(token: @token)
  if response.body =~ /^{"error":"/
    raise Error.new(response.body)
  end
  response
end
fetch_study_ids(filter=nil) click to toggle source
# File lib/red_cap/client.rb, line 58
def fetch_study_ids filter=nil
  json_api_request(content: "record", fields: "study_id", filterLogic: filter)
    .map { |hash| hash["study_id"] }
end
json_api_request(options) click to toggle source
# File lib/red_cap/client.rb, line 63
def json_api_request options
  response = base_request(options.reverse_merge({
    format: "json",
  }))
  JSON.load(response.body)
end