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(identifier) ⇒ Detector

Returns a new instance of Detector.



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

def initialize identifier
  begin
    @agent = Agent.find_by! identifier: identifier
    @payloads = Array.new
    @objects = Array.new
    @help = I2X::Helper.new
  rescue Exception => 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

Instance Method Details

#checkupObject

Start original source detection process



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

def checkup
  # update checkup time
  @agent.update_check_at @help.datetime

  begin

    ##
    # => Process seed data, if available.
    #
    if @agent.seeds.size != 0 then
      @agent.seeds.each do |seed|
        case seed[:publisher]
        when 'csv'
          begin
            @sr = I2X::CSVSeedReader.new(@agent, seed)
          rescue Exception => e
            
          end
        when 'sql'
          begin
            @sr = I2X::SQLSeedReader.new(@agent, seed)
          rescue Exception => e
            
          end
        when 'xml'
          begin
            @sr = I2X::XMLSeedReader.new(@agent, seed)
          rescue Exception => e
            
          end
        when 'json'
          begin
            @sr = I2X::JSONSeedReader.new(@agent, seed)
          rescue Exception => e

          end
        end
        begin
          @reads = @sr.read
          @reads.each do |read|
            @objects.push read
          end
        rescue Exception => e
          
        end
      end

    else
      ##
      # no seeds, simply copy agent data
      object = @help.deep_copy @agent[:payload]
      object[:identifier] = @agent[:identifier]
      object[:seed] = object[:identifier]
      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::Slog.exception e
  end

  begin
    # increase detected events count
    @agent.increment!(:events_count, @payloads.size)
    @response = { :payload => @payloads, :status => 100}
  rescue Exception => e
    @response = {:status => 404, :message => "[i2x][Detector] failed to process queries, #{e}"}
    I2X::Slog.exception e
  end
  @response
end