Class: YARD::Server::Commands::Base
- Inherits:
-
Object
- Object
- YARD::Server::Commands::Base
- Defined in:
- lib/yard/server/commands/base.rb
Overview
Direct Known Subclasses
Instance Attribute Summary collapse
-
#adapter ⇒ Adapter
The server adapter.
-
#body ⇒ String
The response body.
-
#caching ⇒ Boolean
Whether to cache.
-
#command_options ⇒ Hash
The options passed to the command’s constructor.
-
#headers ⇒ Hash{String => String}
Response headers.
-
#path ⇒ String
The path after the command base URI.
-
#request ⇒ Request
Request object.
-
#status ⇒ Numeric
Status code.
Instance Method Summary collapse
- #cache(data) ⇒ Object protected
- #call(request) ⇒ Object
-
#initialize(opts = {}) ⇒ Base
constructor
A new instance of Base.
- #not_found ⇒ Object
- #redirect(url) ⇒ Object protected
- #render(object = nil) ⇒ Object protected
- #run ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Base
Returns 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
Returns the server adapter.
26 27 28 |
# File 'lib/yard/server/commands/base.rb', line 26 def adapter @adapter end |
#body ⇒ String
Returns the response body.
23 24 25 |
# File 'lib/yard/server/commands/base.rb', line 23 def body @body end |
#caching ⇒ Boolean
Returns whether to cache.
29 30 31 |
# File 'lib/yard/server/commands/base.rb', line 29 def caching @caching end |
#command_options ⇒ Hash
Returns the options passed to the command’s constructor.
8 9 10 |
# File 'lib/yard/server/commands/base.rb', line 8 def @command_options end |
#headers ⇒ Hash{String => String}
Returns response headers.
17 18 19 |
# File 'lib/yard/server/commands/base.rb', line 17 def headers @headers end |
#path ⇒ String
Returns 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
Returns request object.
11 12 13 |
# File 'lib/yard/server/commands/base.rb', line 11 def request @request end |
#status ⇒ Numeric
Returns status code.
20 21 22 |
# File 'lib/yard/server/commands/base.rb', line 20 def status @status end |
Instance Method Details
#cache(data) ⇒ Object (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 |
#call(request) ⇒ Object
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 |
#not_found ⇒ Object
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 |
#redirect(url) ⇒ Object (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 |
#render(object = nil) ⇒ Object (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 |
#run ⇒ Object
55 56 57 |
# File 'lib/yard/server/commands/base.rb', line 55 def run raise NotImplementedError end |