Class: DataRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/data/data_repository.rb

Overview

This class serves as a data repository storing the read data and handling the meta information

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, key = nil) ⇒ DataRepository

initialization

Parameters:

  • filename (String)

    the filepath

  • key (Symbol) (defaults to: nil)

    the attribute for the index



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/data/data_repository.rb', line 16

def initialize(filename, key = nil)
  key = :http_status if (key == nil)
  @index = Hash.new()
  @repository = Array.new()
  begin
    create_entries(read_file(filename))
    create_index(key)
  rescue StandardError => e
    puts "Error: #{e.message}".red
  end
end

Instance Attribute Details

#indexObject (readonly)

Returns Hash returns specific mapping on the given key (values => Array).

Returns:

  • Hash returns specific mapping on the given key (values => Array)



9
10
11
# File 'lib/data/data_repository.rb', line 9

def index
  @index
end

#index_criteriaSymbol (readonly, private)

Returns the symbol that represents the current index.

Returns:

  • (Symbol)

    the symbol that represents the current index



86
87
88
# File 'lib/data/data_repository.rb', line 86

def index_criteria
  @index_criteria
end

#repositoryObject (readonly)

Returns Array an array containing all entries.

Returns:

  • Array an array containing all entries



11
12
13
# File 'lib/data/data_repository.rb', line 11

def repository
  @repository
end

Instance Method Details

#create_entries(data) ⇒ Object (private)

creates Entrys of the parsed data and stores it in the repository

Parameters:

  • data (Array)

    the read data



90
91
92
93
94
# File 'lib/data/data_repository.rb', line 90

def create_entries(data)
  data.each { |line|
    @repository << Entry::Entry.new(line) if (!line.empty?)
  }
end

#create_index(key) ⇒ Object

creates an index, mapping all Entrys

Parameters:

  • key (Symbol)

    the index attribute



73
74
75
76
77
78
79
80
81
# File 'lib/data/data_repository.rb', line 73

def create_index(key)
  @index_criteria = key
  @index = Hash.new() if (!@index.empty?)
  @repository.each { |entry|
      mapped_key = map_key_to_attribute(key, entry)
      @index[mapped_key] = Array.new() if (!@index[mapped_key])
      @index[mapped_key] << entry
  }
end

#get_count_to_key(key) ⇒ Integer

method to get the number of entries for the given key

Parameters:

  • key (String)

    the requested key

Returns:

  • (Integer)

    the number of entries



31
32
33
34
35
36
37
# File 'lib/data/data_repository.rb', line 31

def get_count_to_key(key)
  if (!@index.empty?)
    @index[key].size
  else
    puts "Error: index is empty. No index has been created!".red
  end
end

#get_entries_from_index_to(key) ⇒ Array

method to return all entries with the key from the index

Parameters:

  • key (Object)

    the search value

Returns:

  • (Array)

    an array with all entries machting the key in the index criteria



53
54
55
# File 'lib/data/data_repository.rb', line 53

def get_entries_from_index_to(key)
  get_entries_to(@index_criteria, key)
end

#get_entries_to(criteria, key) ⇒ Array

method to get all entries for a given value in the given entry attribute

Parameters:

  • criteria (Symbol)

    the required entry attribute

  • key (Object)

    the search value

Returns:

  • (Array)

    an array with all entries machting the key in the considered entry attribute



62
63
64
65
66
67
68
69
# File 'lib/data/data_repository.rb', line 62

def get_entries_to(criteria, key)
  result = Array.new()
  @repository.each { |entry|
    attribute = Entry::EntryAttributeFactory.get_attribute_to(criteria, entry)
    result << entry if (key.eql?(attribute))
  }
  return result
end

#get_keys_and_their_countHash

method to retrieve the number of entries for every key

Returns:

  • (Hash)

    a mapping of (key => number of entries)



41
42
43
44
45
46
47
# File 'lib/data/data_repository.rb', line 41

def get_keys_and_their_count
  result = Hash.new()
  @index.each_key { |key|
    result[key] = @index[key].size
  }
  return result
end

#map_key_to_attribute(key, entry) ⇒ Object (private)

method to determine the mapping to the given attribute of the Entry

Parameters:

  • key (Symbol)

    the mapping key

  • entry (Entry)

    the actual considered entry

Returns:

  • (Object)

    the matching object from the Entry



100
101
102
103
104
105
106
107
108
# File 'lib/data/data_repository.rb', line 100

def map_key_to_attribute(key, entry)
  case key
    when :source then entry.source
    when :http_status then entry.http_status
    when :http_request then entry.http_request
  else
    entry.timestamp
  end
end

#read_file(filename) ⇒ Array (private)

calls the FileReader to get the content of the file

Parameters:

  • filename (String)

    filepath

Returns:

  • (Array)

    the data of the file as strings



113
114
115
# File 'lib/data/data_repository.rb', line 113

def read_file(filename)
  RubyUtils::FileReader.new(filename, " ").data
end