class OpenComponents::Component
Wrapper object for a component fetched from an OC registry.
Public
↑ topAttributes
Gets/sets a Hash of headers to send in the component request.
Gets/sets the String name of the component.
Gets/sets a Hash of params to send in the component request.
Gets/sets the desired String version of the component.
Public Class Methods
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
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
Internal
↑ topProtected Instance Methods
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
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
Private Instance Methods
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