Sha256: de5dea25f947e0a9acdc6031035ecee0ec61fbc06c4184d499689e4162f6c011

Contents?: true

Size: 1021 Bytes

Versions: 4

Compression:

Stored size: 1021 Bytes

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)
      connection.post(nil, options.reverse_merge({
        token: @token,
      }))
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
red_cap-0.3.0 lib/red_cap/client.rb
red_cap-0.2.0 lib/red_cap/client.rb
red_cap-0.1.1 lib/red_cap/client.rb
red_cap-0.1.0 lib/red_cap/client.rb