Class: I2X::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/i2x/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent) ⇒ Agent

Returns a new instance of Agent.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/i2x/agent.rb', line 7

def initialize agent
  begin
    @identifier = agent[:identifier]
    @publisher = agent[:publisher]
    @payload = agent[:payload]
    @cache = agent[:payload][:cache]
    @seeds = agent[:seeds]
    @selectors = agent[:payload][:selectors]
    I2X::Config.log.debug(self.class.name) {"Agent #{@identifier} initialized"}
  rescue Exception => e
    I2X::Config.log.error(self.class.name) {"Unable to initialize agent. #{e}"}
  end

end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



5
6
7
# File 'lib/i2x/agent.rb', line 5

def cache
  @cache
end

#contentObject

Returns the value of attribute content.



5
6
7
# File 'lib/i2x/agent.rb', line 5

def content
  @content
end

#identifierObject

Returns the value of attribute identifier.



5
6
7
# File 'lib/i2x/agent.rb', line 5

def identifier
  @identifier
end

#payloadObject

Returns the value of attribute payload.



5
6
7
# File 'lib/i2x/agent.rb', line 5

def payload
  @payload
end

#publisherObject

Returns the value of attribute publisher.



5
6
7
# File 'lib/i2x/agent.rb', line 5

def publisher
  @publisher
end

#seedsObject

Returns the value of attribute seeds.



5
6
7
# File 'lib/i2x/agent.rb', line 5

def seeds
  @seeds
end

#selectorsObject

Returns the value of attribute selectors.



5
6
7
# File 'lib/i2x/agent.rb', line 5

def selectors
  @selectors
end

#templatesObject

Returns the value of attribute templates.



5
6
7
# File 'lib/i2x/agent.rb', line 5

def templates
  @templates
end

Instance Method Details

#executeObject

> Perform the actual agent monitoring tasks.



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
57
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
86
87
88
89
90
# File 'lib/i2x/agent.rb', line 26

def execute
@checkup = {}

case @publisher
when 'sql'
  begin
    @d = I2X::SQLDetector.new(self)
  rescue Exception => e
    @response = {:status => 400, :error => e}
    I2X::Config.log.error(self.class.name) {"#{e}"}
  end
when 'csv'
  begin
    @d = I2X::CSVDetector.new(self)
  rescue Exception => e
    @response = {:status => 400, :error => e}
    I2X::Config.log.error(self.class.name) {"#{e}"}
  end
when 'xml'
  begin
    @d = I2X::XMLDetector.new(self)
  rescue Exception => e
    @response = {:status => 400, :error => e}
    I2X::Config.log.error(self.class.name) {"#{e}"}
  end
when 'json'
  begin
    @d = I2X::JSONDetector.new(self)
  rescue Exception => e
    @response = {:status => 400, :error => e}
    I2X::Config.log.error(self.class.name) {"#{e}"}
  end
end


  # Start checkup
  begin
    unless content.nil? then
      @d.content = content
    end
    @checkup = @d.checkup
  rescue Exception => e
    I2X::Config.log.error(self.class.name) {"Checkup error: #{e}"}
  end

  # Start detection
  begin
    @d.objects.each do |object|
      @d.detect object
    end

    @checkup[:templates] = @d.templates.uniq
  rescue Exception => e
    I2X::Config.log.error(self.class.name) {"Detection error: #{e}"}
  end

  begin
    if @checkup[:status] == 100 then
      process @checkup
    end
  rescue Exception => e
    I2X::Config.log.error(self.class.name) {"Process error: #{e}"}
  end
  response = {:status => @checkup[:status], :message => "[i2x][Checkup][execute] All OK."}     
end

#process(checkup) ⇒ Object

> Process agent checks.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/i2x/agent.rb', line 97

def process checkup
  begin
    checkup[:templates].each do |template|
      I2X::Config.log.info(self.class.name) {"Delivering to #{template} template."}
      checkup[:payload].each do |payload|
        I2X::Config.log.debug(self.class.name) {"Processing #{payload}."}
        response = RestClient.post "#{I2X::Config.host}postman/deliver/#{template}.js", payload
        case response.code
        when 200

        else
          I2X::Config.log.warn(self.class.name) {"unable to deliver \"#{payload}\" to \"#{template}\""}
        end
      end
    end
  rescue Exception => e
    I2X::Config.log.error(self.class.name) {"Processing error: #{e}"}
  end
  
end