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



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
86
# File 'lib/cogibara/dispatcher.rb', line 59

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



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

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

#config_from_yaml(yml) ⇒ Object



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

def config_from_yaml(yml)
  yml = YAML.load_file(yml) if yml.is_a? String
  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



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

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



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

def file_operators
  @file_operators ||= Hash.new
end

#operator(keyword) ⇒ Object



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

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

#operator_optionsObject



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

def operator_options
  @operator_options ||= Hash.new
end

#operatorsObject



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

def operators
  @operators ||= Hash.new
end

#register_operator(keywords, options) ⇒ Object



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
56
# File 'lib/cogibara/dispatcher.rb', line 26

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)


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

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

#registered_file?(keyword) ⇒ Boolean

Returns:

  • (Boolean)


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

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