Class: Parsefiles::Folders

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/parsefiles/folders.rb

Constant Summary collapse

VALID_TYPES =
"{txt,log,gz}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

included

Constructor Details

#initialize(folder, options = {}) ⇒ Folders

Returns a new instance of Folders.



13
14
15
16
17
# File 'lib/parsefiles/folders.rb', line 13

def initialize(folder,options={})
  @folder = check_location(folder)
  @files = get_files_with_date
  @options = options
end

Instance Attribute Details

#filesObject

Returns the value of attribute files.



9
10
11
# File 'lib/parsefiles/folders.rb', line 9

def files
  @files
end

#folderObject

Returns the value of attribute folder.



9
10
11
# File 'lib/parsefiles/folders.rb', line 9

def folder
  @folder
end

Instance Method Details

#calc_start_date(days_back) ⇒ Object



56
57
58
59
# File 'lib/parsefiles/folders.rb', line 56

def calc_start_date(days_back)
  date = Date.today - days_back
  date.strftime('%m-%d-%Y')
end

#check_location(folder) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/parsefiles/folders.rb', line 19

def check_location(folder)
  if folder.match(/.*\.log$|txt$|gz$/i)
    "./#{folder}"
  else
    folder = remove_trailing_slash(folder)
    "#{folder}/*."+VALID_TYPES
  end
end

#get_end_dateObject



51
52
53
54
# File 'lib/parsefiles/folders.rb', line 51

def get_end_date
  Folders.validate_date_format(@options["end_date"])
  @options["end_date"] if @options["end_date"]
end

#get_files_in_rangeObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/parsefiles/folders.rb', line 32

def get_files_in_range
  start_date = get_start_date
  end_date = get_end_date

  result = []
  @files.each_with_index do |file,index|
    result << file[0] if Folders.date_in_range(file[1],start_date,end_date)
  end
  result
end

#get_start_dateObject



43
44
45
46
47
48
49
# File 'lib/parsefiles/folders.rb', line 43

def get_start_date
  if @options["days_back"].to_i > 1
    days_back
  else
    start_date
  end
end

#remove_trailing_slash(folder) ⇒ Object



28
29
30
# File 'lib/parsefiles/folders.rb', line 28

def remove_trailing_slash(folder)
  folder.gsub(/(.*)\/$/,'\1')
end