Module: Rack::App::SingletonMethods::Mounting

Included in:
Rack::App::SingletonMethods
Defined in:
lib/rack/app/singleton_methods/mounting.rb

Constant Summary collapse

MOUNT =
Rack::App::SingletonMethods::Mounting

Instance Method Summary collapse

Instance Method Details

#mount(app, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/rack/app/singleton_methods/mounting.rb', line 4

def mount(app, options={})
  case
  when app.is_a?(Class) && app < ::Rack::App
    mount_rack_app(app, options)
  when app.respond_to?(:call)
    mount_rack_interface_compatible_application(app, options)
  else
    raise(NotImplementedError)
  end
end

#mount_directory(directory_path, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rack/app/singleton_methods/mounting.rb', line 34

def mount_directory(directory_path, options={})

  directory_full_path = ::Rack::App::Utils.expand_path(directory_path)

  namespace options[:to] do
    Dir.glob(File.join(directory_full_path, '**', '*')).each do |file_path|
      request_path = file_path.sub(/^#{Regexp.escape(directory_full_path)}/, '')
      get(request_path) { serve_file(file_path) }
      options(request_path) { '' }
    end
  end

  nil
end

#mount_rack_app(app, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rack/app/singleton_methods/mounting.rb', line 15

def mount_rack_app(app, options={})
  options.freeze

  unless app.is_a?(Class) and app <= Rack::App
    raise(ArgumentError, 'Invalid class given for mount, must be a Rack::App')
  end

  cli.merge!(app.cli)

  merge_prop = {
    :namespaces => [@namespaces, options[:to]].flatten,
    :new_ancestor => self
  }

  router.merge_router!(app.router, merge_prop)

  nil
end

#mount_rack_interface_compatible_application(rack_based_app, options = {}) ⇒ Object



56
57
58
59
# File 'lib/rack/app/singleton_methods/mounting.rb', line 56

def mount_rack_interface_compatible_application(rack_based_app, options={})
  request_path = Rack::App::Utils.join(options[:to],Rack::App::Constants::PATH::APPLICATION)
  add_route(::Rack::App::Constants::HTTP::METHOD::ANY, request_path, rack_based_app)
end

#serve_files_from(file_path, options = {}) ⇒ Object



49
50
51
52
53
54
# File 'lib/rack/app/singleton_methods/mounting.rb', line 49

def serve_files_from(file_path, options={})
  file_server = Rack::App::FileServer.new(Rack::App::Utils.expand_path(file_path))
  request_path = Rack::App::Utils.join(options[:to], Rack::App::Constants::PATH::MOUNT_POINT)
  add_route(::Rack::App::Constants::HTTP::METHOD::ANY, request_path, file_server)
  nil
end