Module: AtlasRb::FaradayHelper

Included in:
Authentication, Reset, Resource
Defined in:
lib/atlas_rb/faraday_helper.rb

Overview

HTTP transport helpers shared by every resource class.

Every Atlas request reads two environment variables:

  • ATLAS_URL — base URL of the Atlas API (e.g. https://atlas.example.edu).
  • ATLAS_TOKEN — bearer token used in the Authorization header.

Most calls also identify the acting user via a User: NUID <nuid> header. Resource classes typically pass nuid = nil (anonymous / system context); Authentication is the only place where a real NUID is currently threaded through.

The module is mixed in via extend, so its methods become class methods on the host (e.g. AtlasRb::Work.connection({})).

Instance Method Summary collapse

Instance Method Details

#connection(params, nuid = nil) ⇒ Faraday::Connection

Build a JSON-content Faraday connection to the Atlas API.

Examples:

Fetching a community

AtlasRb::Community.connection({}).get('/communities/abc123')

Parameters:

  • params (Hash)

    query-string / body params to attach to the request. Resource classes use this to pass things like parent_id:, work_id:, or metadata: without manually serializing.

  • nuid (String, nil) (defaults to: nil)

    optional Northeastern University ID to send in the User header. Defaults to nil (no NUID context).

Returns:

  • (Faraday::Connection)

    a connection that follows redirects and uses Faraday's default adapter.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/atlas_rb/faraday_helper.rb', line 31

def connection(params, nuid=nil)
  Faraday.new(
    url: ENV.fetch("ATLAS_URL", nil),
    params: params,
    headers: {
      "Content-Type" => "application/json",
      "Authorization" => "Bearer #{ENV.fetch("ATLAS_TOKEN", nil)}",
      "User" => "NUID #{nuid}"
    }
  ) do |f|
    f.response :follow_redirects
    f.adapter Faraday.default_adapter
  end
end

#multipart(nuid = nil) ⇒ Faraday::Connection

Build a multipart Faraday connection used for binary and XML uploads.

The same ATLAS_URL / ATLAS_TOKEN env vars apply. Unlike #connection, the Content-Type is set automatically by the multipart middleware, and callers pass a payload hash whose values may include Faraday::Multipart::FilePart instances.

Examples:

Posting a binary blob

payload = {
  work_id: "w-123",
  binary: Faraday::Multipart::FilePart.new(File.open("scan.pdf"),
                                            "application/octet-stream",
                                            "scan.pdf")
}
AtlasRb::Blob.multipart({}).post('/files/', payload)

Parameters:

  • nuid (String, nil) (defaults to: nil)

    optional NUID for the User header.

Returns:

  • (Faraday::Connection)

    a multipart-aware connection.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/atlas_rb/faraday_helper.rb', line 64

def multipart(nuid=nil)
  Faraday.new(
    url: ENV.fetch("ATLAS_URL", nil),
    headers: {
      "Authorization" => "Bearer #{ENV.fetch("ATLAS_TOKEN", nil)}",
      "User" => "NUID #{nuid}"
    }
  ) do |f|
    f.request :multipart
    f.request :url_encoded
  end
end