class OpenComponents::Component

Wrapper object for a component fetched from an OC registry.

Public

↑ top

Attributes

headers[RW]

Gets/sets a Hash of headers to send in the component request.

href[R]

Returns

Returns the String URL of the requested component.

name[RW]

Gets/sets the String name of the component.

params[RW]

Gets/sets a Hash of params to send in the component request.

registry_version[R]

Returns

Returns the String version of the component as served by the registry.

render_mode[R]

Returns

Returns the String render mode of the component as served by the registry. Generally either ‘rendered` or `unrendered`.

type[R]

Returns

Returns the String type of the component as served by the registry.

version[RW]

Gets/sets the desired String version of the component.

Public Class Methods

new(name, opts = {}) click to toggle source

Initializes a new Component subclass.

name

The String name of the component to request.

opts

A Hash of options to use when requesting the component (default: {}).

:params

A Hash of parameters to send in the component request (optional, default: {}).

:version

The String version of the component to request (optional, default: ”).

:headers

A Hash of HTTP request headers to include in the component request (optional, default: {}).

# File lib/opencomponents/component.rb, line 42
def initialize(name, opts = {})
  @name    = name
  @params  = opts[:params]  || {}
  @version = opts[:version] || ''
  @headers = opts[:headers] || {}
end

Public Instance Methods

flush!() click to toggle source

Resets all component attributes from a registry response to ‘nil`.

Examples

component.flush!
# => #<OpenComponents::RenderedComponent: ... >

Returns

Returns the reset Component.

# File lib/opencomponents/component.rb, line 63
def flush!
  flush_variables_whitelist.each do |var|
    instance_variable_set(var, nil)
  end

  self
end
reload!() click to toggle source

Resets all component attributes and reloads them from a registry response.

Examples

component.reload!
# => #<OpenComponents::RenderedComponent: ... >

Returns

Returns the reloaded Component.

# File lib/opencomponents/component.rb, line 80
def reload!
  flush!
  load
end
request_version() click to toggle source

Returns

Returns the String value of ‘requestVersion` from a component response, `nil` if not present.

# File lib/opencomponents/component.rb, line 51
def request_version
  @request_version == '' ? nil : @request_version
end

Internal

↑ top

Protected Instance Methods

load() click to toggle source

Executes a component request and sets attributes common to all component render modes. Public-facing ‘#load` methods exist on Component subclasses.

Returns

Returns the Component.

# File lib/opencomponents/component.rb, line 92
def load
  @href             = response_data['href']
  @registry_version = response_data['version']
  @request_version  = response_data['requestVersion']
  @type             = response_data['type']
  @render_mode      = response_data['renderMode']

  self
end
response() click to toggle source

Executes a component request against the configured registry.

Returns

Returns a response body String.

Raises OpenComponents::ComponentNotFound if the registry responds with a 404.

Raises OpenComponents::RegistryTimeout if the request times out.

# File lib/opencomponents/component.rb, line 115
def response
  request_headers = headers.merge(params: params)

  RestClient::Request.execute(
    method: :get,
    url: url,
    timeout: OpenComponents.config.timeout,
    headers: request_headers
  )
rescue RestClient::ResourceNotFound => e
  fail ComponentNotFound, e.message
rescue RestClient::RequestTimeout => e
  fail RegistryTimeout, e.message
end
response_data() click to toggle source

Helper method for converting and memoizing registry response data.

Returns

Returns a Hash of registry response data.

# File lib/opencomponents/component.rb, line 134
def response_data
  @_response_data ||= JSON.parse(response)
end
url() click to toggle source

Builds the URL to send a component request to.

Returns

Returns the String URL to request a component from.

# File lib/opencomponents/component.rb, line 105
def url
  File.join(OpenComponents.config.registry, name, version)
end

Private Instance Methods

flush_variables_whitelist() click to toggle source

Whitelists instance variable names allowed to be reset when calling ‘#flush!`.

Returns

Returns an Array of instance variable Symbols allowed to be reset.

# File lib/opencomponents/component.rb, line 144
def flush_variables_whitelist
  instance_variables - [:@name, :@version, :@params, :@headers]
end