Class: Farscape::PlatformResources

Inherits:
Object
  • Object
show all
Defined in:
lib/farscape/platform_resources.rb

Defined Under Namespace

Classes: Agent, NotFound

Constant Summary collapse

ROOT_ITEMS_KEY =
'items'.freeze

Class Method Summary collapse

Class Method Details

.agentObject



7
8
9
10
11
# File 'lib/farscape/platform_resources.rb', line 7

def self.agent
  @agent ||= Agent.new # This is only done once in the application because is expensive
  # @agent is global to the application. We dup it so each thread has its own copy
  Thread.current[:farscape_agent] ||= @agent.dup
end

.items_for(resource_name, template_variables = {}) ⇒ Object



23
24
25
26
27
# File 'lib/farscape/platform_resources.rb', line 23

def self.items_for(resource_name, template_variables = {})
  root_document = root_for(resource_name, template_variables)
  attributes = root_document_data(resource_name, root_document)
  attributes[ROOT_ITEMS_KEY] || []
end

.log_error(message, e) ⇒ Object



90
91
92
93
# File 'lib/farscape/platform_resources.rb', line 90

def self.log_error(message, e)
  Farscape.logger.error("Hippocrates Error, class #{e.class} \n #{message}: '#{e.message}' \n #{e.backtrace.join("\n")}")
  nil
end

.rescuing_farscape(resource_name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/farscape/platform_resources.rb', line 45

def self.rescuing_farscape(resource_name)
  yield
rescue Net::ReadTimeout => e
  log_error("Network read error accessing #{resource_name}", e)
rescue Farscape::Exceptions::NotFound => e
  log_error("Resource not found accessing #{resource_name}", e)
rescue Farscape::Exceptions::Forbidden => e
  log_error("Forbidden access accessing #{resource_name}", e)
rescue Farscape::Exceptions::InternalServerError => e
  log_error("The server responded with an internal server error when accessing #{resource_name}", e)
rescue Farscape::Discovery::KeyNotFound => e
  log_error("The resource #{resource_name} is not registered in our Hypermedia discovery service", e)
rescue StandardError => e
  Farscape.logger.error("Unknown error class '#{e.class}'")
  log_error("Unknown error accessing #{resource_name}", e)
end

.root_document_data(resource_name, root_document) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/farscape/platform_resources.rb', line 29

def self.root_document_data(resource_name, root_document)
  if root_document.respond_to?(:attributes)
    Farscape.logger.debug("Called #{resource_name} and got #{root_document.attributes[ROOT_ITEMS_KEY]}")
    root_document.attributes
  elsif root_document.respond_to?(:body) # Farscape will happily return a Faraday Response when it can not be parsed
    Farscape.logger.error("The document retrieved for #{resource_name} is not valid JSON. We got: >#{root_document.body}<")
    {}
  else
    Farscape.logger.error("Unknown response trying to access #{resource_name}. We got: >#{root_document}<")
    {}
  end
rescue StandardError => e
  log_error("Error accessing the attributes on the root of #{resource_name}", e)
  {}
end

.root_for(resource_name, template_variables = {}) ⇒ Object



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

def self.root_for(resource_name, template_variables = {})
  rescuing_farscape(resource_name) do
    Farscape.logger.info("Discovering #{resource_name} providing data >#{template_variables}<")
    root = agent.discover(resource_name, template_variables)
    Farscape.logger.info("Accessing the root #{root} for #{resource_name}")
    agent.instance_variable_set(:@entry_point, root)
    agent.enter(root)
  end
end