Class: TreasureData::Command::TextParser

Inherits:
Object
  • Object
show all
Defined in:
lib/td/command/import.rb

Instance Method Summary collapse

Constructor Details

#initialize(names, regexp, time_format) ⇒ TextParser

Returns a new instance of TextParser.



189
190
191
192
193
# File 'lib/td/command/import.rb', line 189

def initialize(names, regexp, time_format)
  @names = names
  @regexp = regexp
  @time_format = time_format
end

Instance Method Details

#call(file, path, &block) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/td/command/import.rb', line 195

def call(file, path, &block)
  i = 0
  file.each_line {|line|
    i += 1
    begin
      line.rstrip!
      m = @regexp.match(line)
      unless m
        raise "invalid log format at #{path}:#{i}"
      end

      record = {}

      cap = m.captures
      @names.each_with_index {|name,cap_i|
        if value = cap[cap_i]
          if name == "time"
            value = parse_time(value).to_i
          end
          record[name] = value
        end
      }

      block.call(record)

    rescue
      $stderr.puts "  skipped: #{$!}: #{line.dump}"
    end
  }
end

#parse_time(value) ⇒ Object



227
228
229
# File 'lib/td/command/import.rb', line 227

def parse_time(value)
  Time.strptime(value, @time_format)
end