Class: I2X::JSONDetector

Inherits:
Detector show all
Defined in:
lib/i2x/jsondetector.rb

Overview

JSONDetector

Detect changes in JSON content files (uses JSONPath).

Instance Attribute Summary

Attributes inherited from Detector

#agent, #content, #identifier, #objects, #payloads, #templates

Instance Method Summary collapse

Methods inherited from Detector

#checkup, #initialize

Constructor Details

This class inherits a constructor from I2X::Detector

Instance Method Details

#detect(object) ⇒ Object

Detect the changes



19
20
21
22
23
24
25
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
# File 'lib/i2x/jsondetector.rb', line 19

def detect object
  I2X::Config.log.info(self.class.name) {"Monitoring #{object[:uri]}"} unless object[:uri].nil?

  begin
    if object[:uri] == '' then
      @doc = object[:content]
    else
      url = RestClient.get object[:uri]
      @doc = url.to_str
    end
    JsonPath.on(@doc,object[:query]).each do |element|
      JsonPath.on(element, object[:cache]).each do |c|
        @response = Cashier.verify c, object, c, object[:seed]
      end

       # Process i2x cache response
       @cache = JSON.parse(@response, {:symbolize_names => true})
       unless @cache[:templates].nil? then
        @cache[:templates].each do |t|
          @templates.push t
        end
      end

      ##
      # If not on cache, add to payload for processing
      #
      if @cache[:status] == 100 then
        I2X::Config.log.info(self.class.name) {"Not on cache, generating payload"}
        # add row data to payload from selectors (key => key, value => column name)
        payload = Hash.new
        object[:selectors].each do |selector|
          selector.each do |k,v|
            JsonPath.on(element, v).each do |el|
              payload[k] = el
            end
          end
        end
        # add payload object to payloads list
        @payloads.push payload
      end

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