Class: I2X::Detector

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

Overview

Detector

Main change detection class, to be inherited by SQL, CSV, JSON and XML detectors (and others to come).

Direct Known Subclasses

CSVDetector, JSONDetector, SQLDetector, XMLDetector

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent) ⇒ Detector

Returns a new instance of Detector.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/i2x/detector.rb', line 18

def initialize agent
  begin
    @agent = agent
    @payloads = Array.new
    @objects = Array.new
    @help = I2X::Helper.new
    I2X::Config.log.info(self.class.name) {"Started new #{agent.identifier} detector"}
  rescue Exception => e
    I2X::Config.log.error(self.class.name) {"#{e}"}
  end
end

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



16
17
18
# File 'lib/i2x/detector.rb', line 16

def agent
  @agent
end

#contentObject

Returns the value of attribute content.



16
17
18
# File 'lib/i2x/detector.rb', line 16

def content
  @content
end

#identifierObject

Returns the value of attribute identifier.



16
17
18
# File 'lib/i2x/detector.rb', line 16

def identifier
  @identifier
end

#objectsObject

Returns the value of attribute objects.



16
17
18
# File 'lib/i2x/detector.rb', line 16

def objects
  @objects
end

#payloadsObject

Returns the value of attribute payloads.



16
17
18
# File 'lib/i2x/detector.rb', line 16

def payloads
  @payloads
end

#templatesObject

Returns the value of attribute templates.



16
17
18
# File 'lib/i2x/detector.rb', line 16

def templates
  @templates
end

Instance Method Details

#checkupObject

Start original source detection process



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/i2x/detector.rb', line 34

def checkup

  begin


    ##
    # => Process seed data, if available.
    #
    unless @agent.seeds.nil? then
      @agent.seeds.each do |seed|
        case seed[:publisher]
        when 'csv'
          begin
            @sr = I2X::CSVSeedReader.new(@agent, seed)
          rescue Exception => e
            I2X::Config.log.error(self.class.name) {"#{e}"}
          end
        when 'sql'
          begin
            @sr = I2X::SQLSeedReader.new(@agent, seed)
          rescue Exception => e
            I2X::Config.log.error(self.class.name) {"#{e}"}
          end
        when 'xml'
          begin
            @sr = I2X::XMLSeedReader.new(@agent, seed)
          rescue Exception => e
            I2X::Config.log.error(self.class.name) {"#{e}"}
          end
        when 'json'
          begin
            @sr = I2X::JSONSeedReader.new(@agent, seed)
          rescue Exception => e
            I2X::Config.log.error(self.class.name) {"#{e}"}
          end
        end
        begin
          @reads = @sr.read
          @reads.each do |read|
            @objects.push read
          end
        rescue Exception => e
          I2X::Config.log.error(self.class.name) {"#{e}"}
        end
      end

    else
      ##
      # no seeds, simply copy agent data
      object = @help.deep_copy @agent.payload 
      object[:identifier] = @agent.identifier
      object[:cache] = @agent.cache
      object[:seed] = object[:identifier]
      object[:selectors] = @agent.selectors
      unless self.content.nil? then
        object[:content] = self.content
      end
      @objects.push object
    end
  rescue Exception => e
    @response = {:status => 404, :message => "[i2x][Detector] failed to load doc, #{e}"}
    I2X::Config.log.error(self.class.name) {"#{e}"}
  end

  begin
    # increase detected events count


    @templates = Array.new
    @response = { :payload => @payloads, :templates => @templates, :status => 100}
  rescue Exception => e
    @response = {:status => 404, :message => "[i2x][Detector] failed to process queries, #{e}"}
    I2X::Config.log.error(self.class.name) {"#{e}"}
  end
  @response
end