Class: YARD::Server::Commands::Base
- Inherits:
-
Object
- Object
- YARD::Server::Commands::Base
- Defined in:
- lib/yard/server/commands/base.rb
Overview
Instance Attribute Summary (collapse)
-
- (Adapter) adapter
The server adapter.
-
- (String) body
The response body.
-
- (Boolean) caching
Whether to cache.
-
- (Hash) command_options
The options passed to the command's constructor.
-
- (Hash{String => String}) headers
Response headers.
-
- (String) path
The path after the command base URI.
-
- (Request) request
Request object.
-
- (Numeric) status
Status code.
Instance Method Summary (collapse)
- - (Object) cache(data) protected
- - (Object) call(request)
-
- (Base) initialize(opts = {})
constructor
A new instance of Base.
- - (Object) not_found
- - (Object) redirect(url) protected
- - (Object) render(object = nil) protected
- - (Object) run
Constructor Details
- (Base) initialize(opts = {})
A new instance of Base
31 32 33 34 35 36 |
# File 'lib/yard/server/commands/base.rb', line 31 def initialize(opts = {}) opts.each do |key, value| send("#{key}=", value) if respond_to?("#{key}=") end self. = opts end |
Instance Attribute Details
- (Adapter) adapter
The server adapter
26 27 28 |
# File 'lib/yard/server/commands/base.rb', line 26 def adapter @adapter end |
- (String) body
The response body
23 24 25 |
# File 'lib/yard/server/commands/base.rb', line 23 def body @body end |
- (Boolean) caching
Whether to cache
29 30 31 |
# File 'lib/yard/server/commands/base.rb', line 29 def caching @caching end |
- (Hash) command_options
The options passed to the command's constructor
8 9 10 |
# File 'lib/yard/server/commands/base.rb', line 8 def @command_options end |
- (Hash{String => String}) headers
Response headers
17 18 19 |
# File 'lib/yard/server/commands/base.rb', line 17 def headers @headers end |
- (String) path
The path after the command base URI
14 15 16 |
# File 'lib/yard/server/commands/base.rb', line 14 def path @path end |
- (Request) request
Request object
11 12 13 |
# File 'lib/yard/server/commands/base.rb', line 11 def request @request end |
- (Numeric) status
Status code
20 21 22 |
# File 'lib/yard/server/commands/base.rb', line 20 def status @status end |
Instance Method Details
- (Object) cache(data) (protected)
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/yard/server/commands/base.rb', line 68 def cache(data) if caching && adapter.document_root path = File.join(adapter.document_root, request.path.sub(/\.html$/, '') + '.html') path = path.sub(%r{/\.html$}, '.html') FileUtils.mkdir_p(File.dirname(path)) log.debug "Caching data to #{path}" File.open(path, 'wb') {|f| f.write(data) } end self.body = data end |
- (Object) call(request)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/yard/server/commands/base.rb', line 38 def call(request) self.request = request self.path ||= request.path[1..-1] self.headers = {'Content-Type' => 'text/html'} self.body = '' self.status = 200 begin run rescue FinishRequest rescue NotFoundError => e self.body = e. if e. != e.class.to_s self.status = 404 end not_found if status == 404 [status, headers, body.is_a?(Array) ? body : [body]] end |
- (Object) not_found
59 60 61 62 63 64 |
# File 'lib/yard/server/commands/base.rb', line 59 def not_found return unless body.empty? self.body = "Not found: #{request.path}" self.headers['Content-Type'] = 'text/plain' self.headers['X-Cascade'] = 'pass' end |
- (Object) redirect(url) (protected)
90 91 92 93 94 |
# File 'lib/yard/server/commands/base.rb', line 90 def redirect(url) headers['Location'] = url self.status = 302 raise FinishRequest end |
- (Object) render(object = nil) (protected)
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/yard/server/commands/base.rb', line 79 def render(object = nil) case object when CodeObjects::Base cache object.format() when nil cache Templates::Engine.render() else cache object end end |
- (Object) run
55 56 57 |
# File 'lib/yard/server/commands/base.rb', line 55 def run raise NotImplementedError end |