Class: Farscape::Agent

Inherits:
Object
  • Object
show all
Includes:
BaseAgent
Defined in:
lib/farscape/agent.rb,
lib/farscape/client/base_client.rb,
lib/farscape/client/http_client.rb

Defined Under Namespace

Classes: BaseClient, HTTPClient

Constant Summary collapse

PROTOCOL =
:http

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseAgent

#handle_extensions

Constructor Details

#initialize(entry = nil, media = :hale, safe = false, plugin_hash = {}) ⇒ Agent

Returns a new instance of Agent.



30
31
32
33
34
35
36
# 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

Instance Attribute Details

#entry_pointObject (readonly)

Returns the value of attribute entry_point.



13
14
15
# File 'lib/farscape/agent.rb', line 13

def entry_point
  @entry_point
end

#media_typeObject (readonly)

Returns the value of attribute media_type.



12
13
14
# File 'lib/farscape/agent.rb', line 12

def media_type
  @media_type
end

Class Method Details

.configObject



21
22
23
# File 'lib/farscape/agent.rb', line 21

def config
  @farscape_config || {}
end

.config=(farscape_config) ⇒ Object



25
26
27
# File 'lib/farscape/agent.rb', line 25

def config=(farscape_config)
  @farscape_config = farscape_config
end

.instanceObject

Prevents multiple threads from accessing the same agent.



17
18
19
# File 'lib/farscape/agent.rb', line 17

def instance
  Thread.current[:farscape_agent] ||= Agent.new
end

Instance Method Details

#clientObject



76
77
78
# File 'lib/farscape/agent.rb', line 76

def client
  Farscape.clients[PROTOCOL].new(self)
end

#disabled_pluginsObject



100
101
102
# File 'lib/farscape/agent.rb', line 100

def disabled_plugins
  Plugins.disabled_plugins(@plugin_hash[:plugins])
end

#discover_entry_point(key, template_variables = {}) ⇒ Object

Discovers provided a key and template_variables. This method is here to be easily overwritten or monkey-patched if needed.



44
45
46
# 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_pluginsObject



96
97
98
# File 'lib/farscape/agent.rb', line 96

def enabled_plugins
  Plugins.enabled_plugins(@plugin_hash[:plugins])
end

#enter(entry = nil, template_variables = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object

Raises:

  • (error)


59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object

TODO share this information with serialization factory base



71
72
73
74
# 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_stackObject



104
105
106
# File 'lib/farscape/agent.rb', line 104

def middleware_stack
  @plugin_hash[:middleware_stack] ||= Plugins.construct_stack(enabled_plugins)
end

#omitting(name_or_type) ⇒ Object



118
119
120
121
122
123
124
125
126
# 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

#pluginsObject



92
93
94
# File 'lib/farscape/agent.rb', line 92

def plugins
  @plugin_hash[:plugins]
end

#representorObject



38
39
40
# File 'lib/farscape/agent.rb', line 38

def representor
  safe? ? SafeRepresentorAgent : RepresentorAgent
end

#safeObject



80
81
82
# File 'lib/farscape/agent.rb', line 80

def safe
  self.class.new(@entry_point, @media_type, true, @plugin_hash)
end

#safe?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/farscape/agent.rb', line 88

def safe?
  @safe_mode
end

#unsafeObject



84
85
86
# File 'lib/farscape/agent.rb', line 84

def unsafe
  self.class.new(@entry_point, @media_type, false, @plugin_hash)
end

#using(name_or_type) ⇒ Object



108
109
110
111
112
113
114
115
116
# 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