Class: Atheme::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/atheme/service.rb

Defined Under Namespace

Classes: Command, Parser

Constant Summary collapse

@@parsers =
Hash.new
@@tmp_commands =
[]
@@tmp_class =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Service

Returns a new instance of Service.



78
79
80
# File 'lib/atheme/service.rb', line 78

def initialize(session)
  @session = session
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/atheme/service.rb', line 82

def method_missing(method, *args, &block)
  raw_output = @session.service_call(service_name, method, *args)
  
  #if an error occurred, just return it to the user
  return raw_output if raw_output.kind_of?(Atheme::Error)

  #building up the response hash...
  response = {raw_output: raw_output}

  #get the parser of the method registered with 'parse :key'
  parser = @@parsers.has_key?(service_name) && @@parsers[service_name][method]

  #no parser is available, return generic Entity which holds only the raw_output
  return Atheme::Entity.new(@session, response, &block) unless parser

  #a responds_with is defined and associates a block
  #we do not need any further command-handling
  #as we only serve the raw_output if the request
  return parser.responder.call(@session, response[:raw_output]) if parser.responder && parser.responder.kind_of?(Proc)

  #add further commands/key-values to the hash registered with 'command :key'
  parser.commands.each do |command|
    response[command.name] = command.call(@session, raw_output)
  end
  
  #create a special Entity registered with resonds_with if available
  return parser.responder.new(@session, response, &block) if parser.responder

  #last but not least, return a generic Entity but with extended commands/values
  return Atheme::Entity.new(@session, response, &block)
end

Class Method Details

.command(name, opts = {}, &block) ⇒ Object



70
71
72
# File 'lib/atheme/service.rb', line 70

def self.command(name, opts={}, &block)
  @@staged_parser.register_command(Atheme::Service::Command.new(name, opts, &block))
end

.inherited(klass) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/atheme/service.rb', line 52

def self.inherited(klass)
  class_name = klass.name.gsub('Atheme::', '')
  Atheme::Session.class_eval <<-RUBY
    def #{class_name.downcase}
      @#{class_name.downcase} ||= #{klass.name}.new(self)
    end
  RUBY

  @@parsers[class_name.downcase] ||= Hash.new
end

.parse(cmd, &block) ⇒ Object



63
64
65
66
67
68
# File 'lib/atheme/service.rb', line 63

def self.parse(cmd, &block)
  service = self.name.sub('Atheme::', '').downcase
  @@parsers[service][cmd] = Atheme::Service::Parser.new
  @@staged_parser = @@parsers[service][cmd]
  yield if block_given?
end

.responds_with(atheme_class = nil, &block) ⇒ Object



74
75
76
# File 'lib/atheme/service.rb', line 74

def self.responds_with(atheme_class=nil, &block)
  @@staged_parser.responder = atheme_class || block
end