Class: Gopher::DirectoryHandler

Inherits:
Handler show all
Defined in:
lib/gopher.rb

Instance Attribute Summary collapse

Attributes inherited from Handler

#application

Instance Method Summary collapse

Methods inherited from Handler

#host, #port, #with

Constructor Details

#initialize(path, selector = '') ⇒ DirectoryHandler

Returns a new instance of DirectoryHandler.

Raises:



118
119
120
121
122
123
# File 'lib/gopher.rb', line 118

def initialize(path, selector = '')
  raise DirectoryNotFound unless File.directory? path
  @selector = selector
  @base = File.expand_path(path)
  @index = YAML.load_file(index_file) if File.exist?(index_file)
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



116
117
118
# File 'lib/gopher.rb', line 116

def base
  @base
end

#indexObject

Returns the value of attribute index.



116
117
118
# File 'lib/gopher.rb', line 116

def index
  @index
end

#selectorObject

Returns the value of attribute selector.



116
117
118
# File 'lib/gopher.rb', line 116

def selector
  @selector
end

Instance Method Details

#call(*args) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/gopher.rb', line 125

def call(*args)
  path = File.join(base, *args)
  if File.directory? path
    return DirectoryHandler.new(path, File.join(selector, args)).with(application).to_map
  elsif File.file? path
    return File.open(path)
  else
    raise NotFound
  end
end

#to_mapObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/gopher.rb', line 136

def to_map
  MapContext.with_block(host, port, self) do |handler|
    if handler.index
      paragraph handler.index['description']
      handler.index['entries'].each do |txt, path|
        link txt, File.join(handler.selector, path)
      end
    else
      Dir["#{handler.base}/*"].each do |path|
        basename = File.basename(path)
        if File.directory? path
          map basename, File.join(handler.selector, basename)
        else
          link basename, File.join(handler.selector, basename)
        end
      end
    end
    text Time.now
  end
end