Class: Rack::Combobot::Combination

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/combobot/combination.rb

Defined Under Namespace

Classes: PathError

Instance Method Summary collapse

Constructor Details

#initialize(root, file_names) ⇒ Combination

Returns a new instance of Combination.



6
7
8
9
10
# File 'lib/rack/combobot/combination.rb', line 6

def initialize(root, file_names)
  @root = root

  @file_contents = fetch_file_contents file_names if file_names
end

Instance Method Details

#combineObject



26
27
28
# File 'lib/rack/combobot/combination.rb', line 26

def combine
  @combo ||= @file_contents.join
end

#fetch_file_contents(file_names = []) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rack/combobot/combination.rb', line 12

def fetch_file_contents(file_names = [])
  file_names.map do |file_name|

    raise PathError if file_name.include?('..') || file_name.include?("~")

    root_prefix = ::File.expand_path(".", @root) + "/"
    file_path   = ::File.expand_path(file_name, @root)

    raise PathError unless file_path.start_with?(root_prefix) && ::File.exist?(file_path)

    file_content = ::File.open(file_path, 'r') { |f| f.read }
  end
end