Class: JSTP::Engine

Inherits:
Object
  • Object
show all
Includes:
Discoverer::Reader
Defined in:
lib/jstp/engine.rb

Defined Under Namespace

Classes: NotAControllerError, NotPermittedError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test = false) ⇒ Engine

Returns a new instance of Engine.



5
6
7
8
# File 'lib/jstp/engine.rb', line 5

def initialize test = false
  @config = Configuration.instance
  from.send @config.strategy.inbound unless test
end

Class Method Details

.environment(argument = nil) ⇒ Object



89
90
91
# File 'lib/jstp/engine.rb', line 89

def self.environment argument = nil
  Configuration.instance.environment argument
end

.environment=(argument) ⇒ Object



93
94
95
# File 'lib/jstp/engine.rb', line 93

def self.environment= argument
  Configuration.instance.environment = argument
end

.gateway(argument = nil) ⇒ Object



97
98
99
# File 'lib/jstp/engine.rb', line 97

def self.gateway argument = nil
  Configuration.instance.gateway argument
end

.gateway=(argument) ⇒ Object



101
102
103
# File 'lib/jstp/engine.rb', line 101

def self.gateway= argument
  Configuration.instance.gateway = argument
end

.hostname(argument = nil) ⇒ Object



81
82
83
# File 'lib/jstp/engine.rb', line 81

def self.hostname argument = nil
  Configuration.instance.hostname argument
end

.hostname=(argument) ⇒ Object



85
86
87
# File 'lib/jstp/engine.rb', line 85

def self.hostname= argument
  Configuration.instance.hostname = argument
end

.logger(argument = nil) ⇒ Object



73
74
75
# File 'lib/jstp/engine.rb', line 73

def self.logger argument = nil
  Configuration.instance.logger argument
end

.logger=(argument) ⇒ Object



77
78
79
# File 'lib/jstp/engine.rb', line 77

def self.logger= argument
  Configuration.instance.logger = argument
end

.port(argument = nil) ⇒ Object

DSL for configuration



57
58
59
# File 'lib/jstp/engine.rb', line 57

def self.port argument = nil
  Configuration.instance.port argument
end

.port=(argument) ⇒ Object



61
62
63
# File 'lib/jstp/engine.rb', line 61

def self.port= argument
  Configuration.instance.port = argument
end

.strategy(argument = nil) ⇒ Object



65
66
67
# File 'lib/jstp/engine.rb', line 65

def self.strategy argument = nil
  Configuration.instance.strategy argument
end

.strategy=(argument) ⇒ Object



69
70
71
# File 'lib/jstp/engine.rb', line 69

def self.strategy= argument
  Configuration.instance.strategy = argument
end

Instance Method Details

#clientsObject



49
50
51
# File 'lib/jstp/engine.rb', line 49

def clients
  @clients ||= {}
end

#discover_resource(resource_stack) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jstp/engine.rb', line 30

def discover_resource resource_stack
  response = []
  response << resource_stack.shift
  class_stack = "::#{self.class}::"
  query = []
  resource_stack.each do |item|
    begin
      capitalized = item[0].upcase + item[1..-1]
      eval(class_stack + capitalized)
      class_stack += "#{capitalized}::"
    rescue NameError, SyntaxError
      query << item
    end
  end
  response << eval(class_stack[0..-3])
  response << query
  return response
end

#dispatch(message, client) ⇒ Object

Processes the dispatch in the new REST engine way



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jstp/engine.rb', line 11

def dispatch message, client
  host, the_class, query = discover_resource message["resource"].clone
  if @config.gateway && host != @config.hostname && host != 'localhost'
    # May be we should gateway this message, if this wasn't aimed at us
    message.to.send @config.strategy.outbound
  else
    if the_class.ancestors.include? JSTP::Controller
      resource = the_class.new message, query, self
      resource.send message["method"].downcase.to_sym
    else
      if @config.environment == :development
        raise NotAControllerError, "The resource class #{the_class} for #{message["resource"].join("/")} was found, but is not a JSTP::Controller"
      else
        raise NotPermittedError, "The selected resource is forbidden for this type of request"
      end
    end
  end
end