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

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.



14
15
16
17
18
19
20
# File 'lib/farscape/agent.rb', line 14

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.



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

def entry_point
  @entry_point
end

#media_typeObject (readonly)

Returns the value of attribute media_type.



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

def media_type
  @media_type
end

Instance Method Details

#clientObject



50
51
52
# File 'lib/farscape/agent.rb', line 50

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

#disabled_pluginsObject



74
75
76
# File 'lib/farscape/agent.rb', line 74

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

#enabled_pluginsObject



70
71
72
# File 'lib/farscape/agent.rb', line 70

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

#enter(entry = entry_point) ⇒ Object



26
27
28
29
30
31
# File 'lib/farscape/agent.rb', line 26

def enter(entry = entry_point)
  @entry_point ||= entry
  raise "No Entry Point Provided!" unless entry
  response = client.invoke(url: entry, headers: get_accept_header(media_type))
  find_exception(response)
end

#find_exception(response) ⇒ Object

Raises:

  • (error)


33
34
35
36
37
38
39
40
41
42
# File 'lib/farscape/agent.rb', line 33

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



45
46
47
48
# File 'lib/farscape/agent.rb', line 45

def get_accept_header(media_type)
  media_types = { hale: 'application/vnd.hale+json' }
  { 'Accept' => media_types[media_type] }
end

#middleware_stackObject



78
79
80
# File 'lib/farscape/agent.rb', line 78

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

#omitting(name_or_type) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/farscape/agent.rb', line 92

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



66
67
68
# File 'lib/farscape/agent.rb', line 66

def plugins
  @plugin_hash[:plugins]
end

#representorObject



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

def representor
  safe? ? SafeRepresentorAgent : RepresentorAgent
end

#safeObject



54
55
56
# File 'lib/farscape/agent.rb', line 54

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

#safe?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/farscape/agent.rb', line 62

def safe?
  @safe_mode
end

#unsafeObject



58
59
60
# File 'lib/farscape/agent.rb', line 58

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

#using(name_or_type) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/farscape/agent.rb', line 82

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