Sha256: 7f92f965e130557945bbdea92eba32498b2347dd1ee39e7a9c97967414ed31d8

Contents?: true

Size: 1.64 KB

Versions: 10

Compression:

Stored size: 1.64 KB

Contents

require 'csv'
require 'open-uri'

module ARII

  ##
  # = CSVDetector
  #
  # Detect changes in CSV files (using column numbers).
  #
  class CSVDetector < Detector

    public
    ##
    # == Detect the changes
    #
    def detect object

      ARII::Config.log.debug(self.class.name) {"Monitoring #{object[:uri]}"}
      CSV.new(open(object[:uri]), :headers => :first_row).each do |row|
        begin
          unless object[:cache].nil? then
            @response = Cashier.verify row[object[:cache].to_i], object, row, object[:seed]
          else
            @response = Cashier.verify row[0], object, row, object[:seed]
          end
        rescue Exception => e
          ARII::Config.log.error(self.class.name) {"Loading error: #{e}"}
        end

        begin

          # Process ARIIcache response
          @cache = JSON.parse(@response, {:symbolize_names => true})
          unless @cache[:templates].nil? then
            @cache[:templates].each do |t|
              @templates.push t
            end
          end
          # The actual processing
          #
          if @cache[:cache][:status] == 100 then
            ARII::Config.log.info(self.class.name) {"Not on cache, generating payload"}

            payload = Hash.new

            object[:selectors].each do |selector|
              selector.each do |k,v|
                payload[k] = row[v.to_i]
              end
            end
            # add payload object to payloads list
            @payloads.push payload
          end

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
arii-1.1 lib/arii/csvdetector.rb
arii-1.0.8 lib/arii/csvdetector.rb
arii-1.0.7 lib/arii/csvdetector.rb
arii-1.0.6 lib/arii/csvdetector.rb
arii-1.0.5 lib/arii/csvdetector.rb
arii-1.0.4 lib/arii/csvdetector.rb
arii-1.0.3 lib/arii/csvdetector.rb
arii-1.0.2 lib/arii/csvdetector.rb
arii-1.0.1 lib/arii/csvdetector.rb
arii-1.0 lib/arii/csvdetector.rb