Class: Colloquy::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/colloquy/renderer.rb

Defined Under Namespace

Classes: FlowPool

Constant Summary collapse

DEFAULT_ERROR_MESSAGE =
'This service is not available at present. Please try again later!'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Renderer

Extracts root path from options hash and creates a HashWithIndifferentAccess object using options.



14
15
16
17
# File 'lib/colloquy/renderer.rb', line 14

def initialize(options = {})
  Colloquy.root = options[:path_root] if options[:path_root]
  @options = options.with_indifferent_access
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/colloquy/renderer.rb', line 9

def logger
  @logger
end

Instance Method Details

#apply(flow_name, msisdn, session_id, input = nil, metadata = {}) ⇒ Response

This method is the only endpoint of Renderer used by Server#response. It receives flow_name, msisdn, session_id, input and metadata provided by Server and returns response after doing lots of stuff with the input.

It makes a session key for each received call, fetches a flow instance corresponding to it (which is an instance of some class with module FlowParser mixed in), and pass all this to #apply!



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/colloquy/renderer.rb', line 32

def apply(flow_name, msisdn, session_id, input = nil,  = {})
  response = ''
  flow_state = :notify
  
  begin
    session_key = make_session_key(msisdn, session_id)
    state, session, flow = state_load(flow_name, session_key, )

    response = apply!(flow, state, session, session_key, flow_name, msisdn, session_id, input)
    flow_state = @state[flow_name.to_sym][session_key][:flow_state].to_sym
  rescue Exception => e
    logger.error "Exception: #{e.inspect} in #{e.backtrace[0]} when processing: flow: #{flow_name}, msisdn: #{msisdn}, session_id: #{session_id}, input: #{input}"
    logger.debug e.backtrace.inspect
    
    begin
      logger.debug 'Responding with default error message.'
      flow_for_messages = @flows[flow_name.to_sym]
      
      response = Colloquy::MessageBuilder.to_message(:error_unexpected, flow: flow_for_messages)
      flow_state = :notify
    rescue Exception => e
      logger.error 'An error occured when we tried to render the error message, falling back to default error response'
      logger.error "Exception: #{e.inspect} in #{e.backtrace[0]}"
      logger.debug e.backtrace.inspect
      
      response = DEFAULT_ERROR_MESSAGE
      flow_state = :notify
    end
  end
  
  # We construct a response object
  response = Colloquy::Response.new(response)
  response.flow_state = flow_state
  
  response
end

#flow_exists?(flow_name) ⇒ Boolean



88
89
90
# File 'lib/colloquy/renderer.rb', line 88

def flow_exists?(flow_name)
  @flows[flow_name.to_sym]
end

#prepare!Object

Initializes renderer components if given configuration is valid. The #configure it executes does lots of initializations and configurations. For details see the method.



21
22
23
# File 'lib/colloquy/renderer.rb', line 21

def prepare!
  configure if configuration_valid?
end

#reload_flow!(flow_name) ⇒ Object



82
83
84
85
86
# File 'lib/colloquy/renderer.rb', line 82

def reload_flow!(flow_name)
  @flows[flow_name.to_sym] = nil
  load_flow(flow_name)
  load_messages_into_flow(flow_name)
end

#reload_flows!Object



75
76
77
78
79
80
# File 'lib/colloquy/renderer.rb', line 75

def reload_flows!
  @flows = {}

  load_flows
  load_messages
end

#reload_messages!Object



69
70
71
72
73
# File 'lib/colloquy/renderer.rb', line 69

def reload_messages!
  @messages = {}

  load_messages
end