class Farscape::PlatformResources

Constants

ROOT_ITEMS_KEY

Public Class Methods

agent() click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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) { || ... } click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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