Class: Atum::Core::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/atum/core/link.rb

Overview

A link invokes requests with an HTTP server.

Constant Summary collapse

LIMIT_INCREMENT =

The amount limit is increased on each successive fetch in pagination

50

Instance Method Summary collapse

Constructor Details

#initialize(url, link_schema, options = {}) ⇒ Link

Instantiate a link.

Parameters:

  • url (String)

    The URL to use when making requests. Include the username and password to use with HTTP basic auth.

  • link_schema (LinkSchema)

    The schema for this link.

  • options (Hash) (defaults to: {})

    Configuration for the link. Possible keys include:

    • default_headers: Optionally, a set of headers to include in every request made by the client. Default is no custom headers.

    • http_adapter: Optionally, the http adapter to use; for now we accept adapters according to Faraday’s builder: github.com/lostisland/faraday/blob/v0.8.9/lib/faraday/builder.rb e.g. http_adapter: [:rack, Rails.application]



25
26
27
28
29
30
31
32
33
# File 'lib/atum/core/link.rb', line 25

def initialize(url, link_schema, options = {})
  root_url, @path_prefix = unpack_url(url)
  http_adapter = options[:http_adapter] || [:net_http]
  @connection = Faraday.new(url: root_url) do |faraday|
    faraday.adapter(*http_adapter)
  end
  @link_schema = link_schema
  @headers = options[:default_headers] || {}
end

Instance Method Details

#run(*parameters) ⇒ String, ...

Make a request to the server.

Parameters:

  • parameters (Array)

    The list of parameters to inject into the path. A request body can be passed as the final parameter and will always be converted to JSON before being transmitted.

Returns:

  • (String, Object, Enumerator)

    A string for text responses, an object for JSON responses, or an enumerator for list responses.

Raises:

  • (ArgumentError)

    Raised if either too many or too few parameters were provided.



44
45
46
47
48
49
50
51
# File 'lib/atum/core/link.rb', line 44

def run(*parameters)
  options = parameters.pop
  raise ArgumentError, 'options must be a hash' unless options.is_a?(Hash)

  options = default_options.deep_merge(options)
  path = build_path(*parameters)
  Request.new(@connection, @link_schema.method, path, options).request
end