Class: Moonrope::DocServer

Inherits:
Object
  • Object
show all
Defined in:
lib/moonrope/doc_server.rb

Defined Under Namespace

Classes: Generator

Constant Summary collapse

CONTENT_TYPES =
{
  'css' => 'text/css',
  'js' => 'text/javascript',
  'svg' => 'image/svg+xml'
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, base, options = {}) ⇒ DocServer

Returns a new instance of DocServer.



23
24
25
26
27
# File 'lib/moonrope/doc_server.rb', line 23

def initialize(app, base, options = {})
  @app = app
  @base = base
  @options = options
end

Class Attribute Details

.path_regexObject

Set the default path regex which should be matched for requests for API docmentation. By default, this is /api/docs/.



17
18
19
# File 'lib/moonrope/doc_server.rb', line 17

def path_regex
  @path_regex ||= /\A\/#{Moonrope::Request.path_prefix}docs\/([\w\.]+)\/?([\w\/\-\.]+)?/
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



29
30
31
# File 'lib/moonrope/doc_server.rb', line 29

def base
  @base
end

Instance Method Details

#call(env) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/moonrope/doc_server.rb', line 64

def call(env)
  if env['PATH_INFO'] =~ self.class.path_regex
    version = $1
    doc_path = $2
    request = Rack::Request.new(env)
    generator = Generator.new(@base, :host => "#{request.scheme}://#{request.host_with_port}", :version => version, :prefix => env['PATH_INFO'].split('/')[1])

    if @options[:reload_on_each_request]
      @base.load
    end

    file = nil
    content_type = nil

    case doc_path
    when nil, ""
      return [302, {'Location' => "#{env['PATH_INFO']}/welcome"}, ['']]
    when /\Awelcome\z/, /\Aindex\.html\z/
      file = generator.generate_file(doc_path, 'index')
    when /\Acontrollers\/(\w+)(\.html)?\z/
      if controller = @base.controller($1.to_sym)
        file = generator.generate_file(doc_path, 'controller', :controller => controller)
      end
    when /\Acontrollers\/(\w+)\/(\w+)(\.html)?\z/
      if controller = @base.controller($1.to_sym)
        if action = controller.action($2.to_sym)
          file = generator.generate_file(doc_path, 'action', :controller => controller, :action => action)
        end
      end
    when /\Astructures\/(\w+)(\.html)?\z/
      if structure = @base.structure($1.to_sym)
        file = generator.generate_file(doc_path, 'structure', :structure => structure)
      end
    when /\Aauthenticators\/(\w+)(\.html)?\z/
      if authenticator = @base.authenticators[$1.to_sym]
        file = generator.generate_file(doc_path, 'authenticator', :authenticator => authenticator)
      end
    when /\Aassets\/([\w]+)\.([a-z]+)\z/
      path = File.join(generator.template_root_path, 'assets', "#{$1}.#{$2}")
      if File.exist?(path)
        file = File.read(path)
        content_type = CONTENT_TYPES[$2] || 'text/plain'
      end
    end

    if file
      [200, {
        'Content-Type' => content_type || 'text/html',
        'Content-Length' => file.bytesize.to_s},
      [file]]
    else
      [404, {}, ['Not found']]
    end
  else
    return @app.call(env)
  end
end