Class: Cogibara::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/cogibara/dispatcher.rb

Instance Method Summary collapse

Instance Method Details

#call(keyword, message) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cogibara/dispatcher.rb', line 58

def call(keyword,message)
  begin
    if operators[keyword].is_a? Cogibara::OperatorBase
      response = operator(keyword).receive_message(message)
    else
      response = operator(keyword).process(message)
    end
  rescue Exception
    puts "an error occured! " + ($!).to_s
    Cogibara::say(Cogibara::Message.new("** O Noez, an error has occured in module #{keyword}; #{($!).to_s}  **",message.clientID))
    nil
  end
  # if response.is_a? Hash
  #   if response[:code]
  #     if response[:code] == :confirm
  #       # Cogibara::Responder.new.send_reply(response[:text],message.clientID)
  #       Cogibara::confirmer.add_action(response[:message], response[:success])
  #       response[:message]
  #     else
  #       response
  #     end
  #   else
  #     response
  #   end
  # else
    response
  # end
end

#call_file(keyword, file) ⇒ Object



87
88
89
# File 'lib/cogibara/dispatcher.rb', line 87

def call_file(keyword, file)
  response = operator(keyword).process_file(file)
end

#config_from_yaml(file) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cogibara/dispatcher.rb', line 100

def config_from_yaml(file)
  yml = file
  yml["modules"].each do |mod|
    mod_name = mod["module_name"]
    mod_keywords = mod["keywords"]
    mod_file = mod["file_name"] ? mod["file_name"] : mod["module_name"].downcase.gsub(' ','_')
    mod_class_name = mod["class_name"] ? mod["class_name"] : mod_file.split('_').map{ |w| w.capitalize }.join
    # mod_file += ".rb"

    if mod_keywords.is_a? Array
      register_operator(mod_keywords, {name: mod_name, file_name: mod_file, class_name: mod_class_name, config: mod})
    else
      register_operator([mod_keywords], {name: mod_name, file_name: mod_file, class_name: mod_class_name, config: mod})
    end
  end
end

#file_operator(keyword) ⇒ Object



20
21
22
23
# File 'lib/cogibara/dispatcher.rb', line 20

def file_operator(keyword)
  file_operators[keyword] = file_operators[keyword].new(operator_options[operators[keyword]]) if file_operators[keyword].is_a? Class
  file_operators[keyword]
end

#file_operatorsObject



7
8
9
# File 'lib/cogibara/dispatcher.rb', line 7

def file_operators
  @file_operators ||= Hash.new
end

#operator(keyword) ⇒ Object



15
16
17
18
# File 'lib/cogibara/dispatcher.rb', line 15

def operator(keyword)
  operators[keyword] = operators[keyword].new(operator_options[operators[keyword]]) if operators[keyword].is_a? Class
  operators[keyword]
end

#operator_optionsObject



11
12
13
# File 'lib/cogibara/dispatcher.rb', line 11

def operator_options
  @operator_options ||= Hash.new
end

#operatorsObject



3
4
5
# File 'lib/cogibara/dispatcher.rb', line 3

def operators
  @operators ||= Hash.new
end

#register_operator(keywords, options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cogibara/dispatcher.rb', line 25

def register_operator(keywords, options)
  mod_keywords = keywords
  mod_name = options[:name] || keywords[0]
  mod_file = options[:file_name] ? options[:file_name] : mod_name.downcase.gsub(' ','_')
  mod_class_name = options[:class_name] ? options[:class_name] : mod_file.split('_').map{ |w| w.capitalize }.join
  mod_file += ".rb"
  loc = File.expand_path(File.dirname(__FILE__))
  require "#{loc}/operators/#{mod_file}" if File.exists?("#{loc}/operators/#{mod_file}")

  if Object.const_defined?(mod_class_name)
    mod_class = eval(mod_class_name) ## TODO prooooooobably should do this a different way
    unless (mod_class.method_defined?("process") || mod_class.method_defined?("process_file"))
      puts "process method missing for #{mod_class.to_s} in #{mod_name}"
    else
      operator_options[mod_class] = options[:config] || {}
    end
    if mod_class.method_defined?("process")
      mod_keywords.each do |kw|
        operators[kw] = mod_class
      end
    end
    if mod_class.method_defined?("process_file")
      mod_keywords.each do |kw|
        file_operators[kw] = mod_class
      end
    end
  else
    puts "main class: #{mod_class_name} not defined for module #{mod_name}"
  end
  puts "file: #{loc}/operators/#{mod_file} for module #{mod_name} does not exist, skipping load" if options[:file_name] && !File.exists?("#{loc}/operators/#{mod_file}")
end

#registered?(keyword) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/cogibara/dispatcher.rb', line 91

def registered?(keyword)
  operators.has_key?(keyword)
end

#registered_file?(keyword) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
# File 'lib/cogibara/dispatcher.rb', line 95

def registered_file?(keyword)
  puts "looking for #{keyword} in #{file_operators.keys}"
  file_operators.has_key?(keyword)
end