Class: Schatz::Input::IncrExport

Inherits:
Export
  • Object
show all
Includes:
ParserHelper
Defined in:
lib/schatz/input/incr_export.rb

Constant Summary

Constants included from ParserHelper

ParserHelper::CONVERSATION_HTML, ParserHelper::CONVERSATION_ROW, ParserHelper::GCM_NUMBER, ParserHelper::IP_ADDRESS, ParserHelper::LINK, ParserHelper::LINK_HTML, ParserHelper::ONE_HYPHEN, ParserHelper::ONE_OR_MORE_COMMA, ParserHelper::ONE_PORT, ParserHelper::PORTS_WITH_PROTOCOL, ParserHelper::REGEXP_HOST, ParserHelper::REGEXP_HOST_PORTS, ParserHelper::SOURCE_SUBNET

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ParserHelper

#parse_ports_field, #push_connection

Constructor Details

#initialize(options) ⇒ IncrExport

Returns a new instance of IncrExport.



12
13
14
15
# File 'lib/schatz/input/incr_export.rb', line 12

def initialize(options)
  @options        = options
  @file = Nokogiri::HTML(File.open(@options[:file_name], 'r'))
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



10
11
12
# File 'lib/schatz/input/incr_export.rb', line 10

def file
  @file
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/schatz/input/incr_export.rb', line 10

def options
  @options
end

Instance Method Details

#delete_mess(row) ⇒ Object



39
40
41
# File 'lib/schatz/input/incr_export.rb', line 39

def delete_mess(row)
  row.text.gsub(/[\n\t]/, "").gsub(/\u00A0/, " ").gsub(/\r\r/, " ").gsub(/\r/, "")
end

#delete_source_subnets(row) ⇒ Object



71
72
73
74
# File 'lib/schatz/input/incr_export.rb', line 71

def delete_source_subnets(row)
  row.match(SOURCE_SUBNET).to_a.each{|reg| row.gsub!(/#{reg}/, "")} while row =~ SOURCE_SUBNET
  row
end

#find_conv_tableObject



50
51
52
53
54
55
# File 'lib/schatz/input/incr_export.rb', line 50

def find_conv_table
  reg1 = Regexp.new 'Conversation Type'
  reg2 = Regexp.new 'CR/GCM Number'
  reg3 = Regexp.new 'Destination Host'
  find_table([reg1, reg2, reg3])
end


57
58
59
60
61
62
# File 'lib/schatz/input/incr_export.rb', line 57

def find_links_table
  reg1 = Regexp.new 'Intranet Site'
  reg2 = Regexp.new 'Reason and Activity'
  reg3 = Regexp.new 'CR/GCM Number'
  find_table([reg1, reg2, reg3])
end

#find_table(regs) ⇒ Object

Raises:

  • (IncorrectIncrFormat)


64
65
66
67
68
69
# File 'lib/schatz/input/incr_export.rb', line 64

def find_table(regs)
  tables = @file.xpath("/html/body/table")
  conv_table = tables.select {|t| regs.all?{ |regexp| t.text =~ regexp} }
  raise IncorrectIncrFormat if conv_table.size > 1
  tables.index(conv_table.first)
end

#get_conversationsObject



21
22
23
# File 'lib/schatz/input/incr_export.rb', line 21

def get_conversations
  Schatz::Input::IncrLineParser.new(rows_in_conversation_table).connections
end


17
18
19
# File 'lib/schatz/input/incr_export.rb', line 17

def get_links
  Schatz::Input::IncrLineParser.new(rows_in_links_table).links
end

#get_rid_of_header(table_number) ⇒ Object



34
35
36
37
# File 'lib/schatz/input/incr_export.rb', line 34

def get_rid_of_header(table_number)
  rows = @file.xpath("//table[#{table_number+1}]//tr")
  rows.map {|row| delete_mess(row) }
end

#get_table(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
48
# File 'lib/schatz/input/incr_export.rb', line 43

def get_table options={}
  allowed_keys= ["table_id"]
  raise ArgumentError if options.empty?
  options.keys.each {|key| raise IncorrentParameters unless allowed_keys.include? key.to_s}			
  @file.xpath("/html/body/table")[options[:table_id].to_i]
end

#rows_in_conversation_tableObject

eliminate the first row(names of the fields)



26
27
28
# File 'lib/schatz/input/incr_export.rb', line 26

def rows_in_conversation_table
  get_rid_of_header(find_conv_table).select {|row| row =~ CONVERSATION_ROW }
end


30
31
32
# File 'lib/schatz/input/incr_export.rb', line 30

def rows_in_links_table
  get_rid_of_header(find_links_table)
end

#scan_gcm_numbers(rows) ⇒ Object



76
77
78
# File 'lib/schatz/input/incr_export.rb', line 76

def scan_gcm_numbers(rows)
  rows.map{ |row| row.scan(GCM_NUMBER).first }.compact.map(&:downcase)
end