class Farscape::Agent
Constants
- PROTOCOL
Attributes
entry_point[R]
media_type[R]
Public Class Methods
config()
click to toggle source
# File lib/farscape/agent.rb, line 21 def config @farscape_config || {} end
config=(farscape_config)
click to toggle source
# File lib/farscape/agent.rb, line 25 def config=(farscape_config) @farscape_config = farscape_config end
instance()
click to toggle source
Prevents multiple threads from accessing the same agent.
# File lib/farscape/agent.rb, line 17 def instance Thread.current[:farscape_agent] ||= Agent.new end
new(entry = nil, media = :hale, safe = false, plugin_hash = {})
click to toggle source
# File lib/farscape/agent.rb, line 30 def initialize(entry = nil, media = :hale, safe = false, plugin_hash = {}) @entry_point = entry @media_type = media @safe_mode = safe @plugin_hash = plugin_hash.empty? ? default_plugin_hash : plugin_hash handle_extensions end
Public Instance Methods
client()
click to toggle source
# File lib/farscape/agent.rb, line 76 def client Farscape.clients[PROTOCOL].new(self) end
disabled_plugins()
click to toggle source
# File lib/farscape/agent.rb, line 100 def disabled_plugins Plugins.disabled_plugins(@plugin_hash[:plugins]) end
discover_entry_point(key, template_variables = {})
click to toggle source
Discovers provided a key and template_variables. This method is here to be easily overwritten or monkey-patched if needed.
# File lib/farscape/agent.rb, line 44 def discover_entry_point(key, template_variables = {}) Discovery.new.discover(self.class.config, key, template_variables) end
enabled_plugins()
click to toggle source
# File lib/farscape/agent.rb, line 96 def enabled_plugins Plugins.enabled_plugins(@plugin_hash[:plugins]) end
enter(entry = nil, template_variables = {})
click to toggle source
# File lib/farscape/agent.rb, line 48 def enter(entry = nil, template_variables = {}) @entry_point = entry || @entry_point # If provided entry will update our current entry point raise "No Entry Point Provided!" unless @entry_point unless Addressable::URI.parse(@entry_point).absolute? @entry_point = discover_entry_point(@entry_point, template_variables) end response = client.invoke(url: @entry_point, headers: get_accept_header(media_type)) find_exception(response) end
find_exception(response)
click to toggle source
# File lib/farscape/agent.rb, line 59 def find_exception(response) error = client.dispatch_error(response) begin representing = representor.new(media_type, response, self) rescue JSON::ParserError representing = response end raise error.new(representing) if error representing end
get_accept_header(media_type)
click to toggle source
TODO share this information with serialization factory base
# File lib/farscape/agent.rb, line 71 def get_accept_header(media_type) media_types = { hale: 'application/vnd.hale+json' } { 'Accept' => media_types[media_type] } end
middleware_stack()
click to toggle source
# File lib/farscape/agent.rb, line 104 def middleware_stack @plugin_hash[:middleware_stack] ||= Plugins.construct_stack(enabled_plugins) end
omitting(name_or_type)
click to toggle source
# File lib/farscape/agent.rb, line 118 def omitting(name_or_type) disabling_rules, plugins = Plugins.disable(name_or_type, @plugin_hash[:disabling_rules], @plugin_hash[:plugins]) plugin_hash = { disabling_rules: disabling_rules, plugins: plugins, middleware_stack: nil } self.class.new(@entry_point, @media_type, @safe_mode, plugin_hash) end
plugins()
click to toggle source
# File lib/farscape/agent.rb, line 92 def plugins @plugin_hash[:plugins] end
representor()
click to toggle source
# File lib/farscape/agent.rb, line 38 def representor safe? ? SafeRepresentorAgent : RepresentorAgent end
safe()
click to toggle source
# File lib/farscape/agent.rb, line 80 def safe self.class.new(@entry_point, @media_type, true, @plugin_hash) end
safe?()
click to toggle source
# File lib/farscape/agent.rb, line 88 def safe? @safe_mode end
unsafe()
click to toggle source
# File lib/farscape/agent.rb, line 84 def unsafe self.class.new(@entry_point, @media_type, false, @plugin_hash) end
using(name_or_type)
click to toggle source
# File lib/farscape/agent.rb, line 108 def using(name_or_type) disabling_rules, plugins = Plugins.enable(name_or_type, @plugin_hash[:disabling_rules], @plugin_hash[:plugins]) plugin_hash = { disabling_rules: disabling_rules, plugins: plugins, middleware_stack: nil } self.class.new(@entry_point, @media_type, @safe_mode, plugin_hash) end
Private Instance Methods
default_plugin_hash()
click to toggle source
# File lib/farscape/agent.rb, line 130 def default_plugin_hash { plugins: Farscape.plugins.dup, # A hash of plugins keyed by the plugin name disabling_rules: Farscape.disabling_rules.dup, # A list of symbols that are Names or types of plugins middleware_stack: nil } end