Sha256: e1bf36e649409e92110af82632d697813d193553e56cc7c33d4b5cebe071ffa6

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

require "json"
require "faraday"

module REDCap
  class Client
    def initialize url: REDCap.url, token: REDCap.token
      @url = url
      @token = token
    end

    def records
      json_api_request(content: "record")
    end

    def metadata
      json_api_request(content: "metadata")
    end

    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

    File = Struct.new(:data, :type, :filename)

    private

    def json_api_request options
      response = base_request(options.reverse_merge({
        format: "json",
      }))
      JSON.load(response.body)
    end

    def base_request options
      connection = Faraday.new(url: @url)
      response = connection.post(nil, options.reverse_merge({
        token: @token,
      }))
      if error_message = response.body[/<error>(.+?)<\/error>/, 1]
        raise Error.new(error_message)
      end
      response
    end

    class Error < StandardError; end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
red_cap-0.5.0 lib/red_cap/client.rb
red_cap-0.4.0 lib/red_cap/client.rb