Class: Streamer::FactProviders::CSVProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/streamer/fact_providers/csv_provider.rb

Overview

CSVProvider implements Finder Provider interface

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ CSVProvider

Returns a new instance of CSVProvider.



9
10
11
# File 'lib/streamer/fact_providers/csv_provider.rb', line 9

def initialize(path)
  @path = path
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/streamer/fact_providers/csv_provider.rb', line 7

def key
  @key
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/streamer/fact_providers/csv_provider.rb', line 7

def path
  @path
end

Instance Method Details

#field_number(field) ⇒ Object



39
40
41
# File 'lib/streamer/fact_providers/csv_provider.rb', line 39

def field_number(field)
  headers.index(field) + 1
end

#find(key) ⇒ Object



13
14
15
16
17
# File 'lib/streamer/fact_providers/csv_provider.rb', line 13

def find(key)
  @key = key
  return hash_result if segments.size == 2
  return scalar_result if segments.size == 3
end

#hash_resultObject



23
24
25
# File 'lib/streamer/fact_providers/csv_provider.rb', line 23

def hash_result
  Hash[headers.zip CSV.parse_line(lines)]
end

#headersObject



35
36
37
# File 'lib/streamer/fact_providers/csv_provider.rb', line 35

def headers
  @headers ||= CSV.parse_line(`head -n 1 #{path}`)
end

#linesObject



31
32
33
# File 'lib/streamer/fact_providers/csv_provider.rb', line 31

def lines
  `#{processing_statement}`.strip
end

#processing_statementObject



43
44
45
46
47
48
49
# File 'lib/streamer/fact_providers/csv_provider.rb', line 43

def processing_statement
  segs = segments
  return unless segs.size > 1 && segs.size < 4
  <<-STMT
awk -F'","|,' '($#{field_number(segs[0])} ~ /^#{segs[1]}$/){print $0}' #{path}
  STMT
end

#scalar_resultObject



27
28
29
# File 'lib/streamer/fact_providers/csv_provider.rb', line 27

def scalar_result
  hash_result[segments[2]]
end

#segmentsObject



19
20
21
# File 'lib/streamer/fact_providers/csv_provider.rb', line 19

def segments
  key.split('.')
end