Class: Jarvis::Service

Inherits:
Object
  • Object
show all
Includes:
Hooks
Defined in:
lib/jarvis/service.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Service

Returns a new instance of Service.



7
8
9
10
# File 'lib/jarvis/service.rb', line 7

def initialize(message)
  @message = message
  @response = ""
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object



12
13
14
# File 'lib/jarvis/service.rb', line 12

def method_missing(name, *args, &blk)
  message.public_send(name, *args, &blk)
end

Class Attribute Details

.interpreter_phrasesObject

Returns the value of attribute interpreter_phrases.



28
29
30
# File 'lib/jarvis/service.rb', line 28

def interpreter_phrases
  @interpreter_phrases
end

.invokerObject

Returns the value of attribute invoker.



28
29
30
# File 'lib/jarvis/service.rb', line 28

def invoker
  @invoker
end

.patternObject

Returns the value of attribute pattern.



28
29
30
# File 'lib/jarvis/service.rb', line 28

def pattern
  @pattern
end

.required_environment_variablesObject

Returns the value of attribute required_environment_variables.



28
29
30
# File 'lib/jarvis/service.rb', line 28

def required_environment_variables
  @required_environment_variables
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



6
7
8
# File 'lib/jarvis/service.rb', line 6

def message
  @message
end

#responseObject

Returns the value of attribute response.



6
7
8
# File 'lib/jarvis/service.rb', line 6

def response
  @response
end

Class Method Details

.environment(*args) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/jarvis/service.rb', line 40

def self.environment(*args)
  args.each do |sym|
    str = sym.to_s.upcase
    send :define_method, sym do
      ENV[str] || ENV[str.downcase]
    end
    required_environment_variables << str
  end
end

.interpreter_pattern(pattern) ⇒ Object



51
52
53
# File 'lib/jarvis/service.rb', line 51

def self.interpreter_pattern(pattern)
  @pattern = pattern
end

.invoke_with(method_name) ⇒ Object

class MyService < Jarvis::Service

invoke_with :post_tweet

end



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

def self.invoke_with(method_name)
  send :define_method, :invoke do
    run_hook :before_invoke
    self.response = send method_name
    run_hook :after_invoke
    self.response
  end
end

.phrases(*args) ⇒ Object



31
32
33
34
# File 'lib/jarvis/service.rb', line 31

def self.phrases(*args)
  @interpreter_phrases = args
  reset_interpreter_pattern
end

Instance Method Details

#runObject



16
17
18
# File 'lib/jarvis/service.rb', line 16

def run

end

#validate_environmentObject



20
21
22
23
24
# File 'lib/jarvis/service.rb', line 20

def validate_environment
  self.class.required_environment_variables.each do |v|
    raise UnfitEnvironmentException unless (ENV[v] || ENV[v.downcase])
  end
end