Class: ARII::ExcelDetector

Inherits:
Detector show all
Defined in:
lib/arii/exceldetector.rb

Overview

ExcelDetector

Detect changes in excel files .

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 ARII::Detector

Instance Method Details

#detect(object) ⇒ Object

Detect the changes



18
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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/arii/exceldetector.rb', line 18

def detect object

  ARII::Config.log.debug(self.class.name) { "Monitoring #{object[:uri]}" }

  # update headers default behaviour
  begin
    if object[:headers] == ''
      object[:headers] = 0
    end
  rescue Exception => e
    ARII::Config.log.error(self.class.name) { "Processing error on errors: #{e}" }
  end



  # different gems and implementation for XLSX and XLS
  if object[:uri].end_with? 'xlsx'
    ARII::Config.log.debug(self.class.name) { "Loading RubyXL for #{object[:uri]}" }
    book = RubyXL::Parser.parse(open(object[:uri]))


    begin
      if object[:sheet] != ''
        sheet = book[object[:sheet]]
      else
        sheet = book[0]
      end
    rescue Exception => e
      ARII::Config.log.error(self.class.name) { "Processing error on sheet selection: #{e}" }
    end

    ARII::Config.log.debug(self.class.name) { "Initiating data extraction" }
    sheet.extract_data.drop(object[:headers].to_i).each do |row|


      begin
        unless object[:cache].nil?
          @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) { "Processing error on cache verification: #{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}\n#{e.backtrace}" }
      end
    end
  end


  if object[:uri].end_with? 'xls'
    Spreadsheet.client_encoding = 'UTF-8'
    book = Spreadsheet.open(open(object[:uri]))

    if object[:sheet] != ''
      sheet = book.worksheet [object[:sheet]]
    else
      sheet = book.worksheet 0
    end



    sheet.each object[:headers].to_i do |row|
      unless object[:cache].nil?
        @response = Cashier.verify row[object[:cache].to_i], object, row, object[:seed]
      else
        @response = Cashier.verify row[0], object, row, object[:seed]
      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}\n#{e.backtrace}" }
      end


    end
  end

  @cache[:templates]
end