lib/i2x/jsondetector.rb in i2x-0.0.6 vs lib/i2x/jsondetector.rb in i2x-0.0.7
- old
+ new
@@ -1,16 +1,9 @@
-#require 'helper'
require 'open-uri'
require 'jsonpath'
require 'rest_client'
-require 'csv'
require 'json'
-#require 'seedreader'
-#require 'csvseedreader'
-#require 'sqlseedreader'
-#require 'xmlseedreader'
-#require 'jsonseedreader'
module I2X
# = JSONDetector
#
@@ -22,30 +15,40 @@
##
# == 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|
- @cache = Cashier.verify c, object, c, object[:seed]
+ @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
- JSON.parse(object[:selectors]).each do |selector|
+ object[:selectors].each do |selector|
selector.each do |k,v|
JsonPath.on(element, v).each do |el|
payload[k] = el
end
end
@@ -54,11 +57,11 @@
@payloads.push payload
end
end
rescue Exception => e
-
+ I2X::Config.log.error(self.class.name) {"Loading error: #{e}"}
end
-
+ @cache[:templates]
end
end
end
\ No newline at end of file