Sha256: 58cfb135f4f05c6ac8a1aef9893b54248ec2b17488408974b65c61aef548037d

Contents?: true

Size: 1.8 KB

Versions: 3

Compression:

Stored size: 1.8 KB

Contents

require 'open-uri'
require 'jsonpath'
require 'rest_client'
require 'json'

module I2X

  # = JSONDetector
  #
  # Detect changes in JSON content files (uses JSONPath).
  #
  class JSONDetector < Detector

    public

    ##
    # == Detect the changes
    #
    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
i2x-0.1.1 lib/i2x/jsondetector.rb
i2x-0.1 lib/i2x/jsondetector.rb
i2x-0.0.7 lib/i2x/jsondetector.rb