Class: YARD::Server::Commands::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/yard/server/commands/base.rb

Overview

Since:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Base) initialize(opts = {})

A new instance of Base

Since:

  • 0.6.0



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.command_options = opts
end

Instance Attribute Details

- (Adapter) adapter

The server adapter

Returns:

Since:

  • 0.6.0



26
27
28
# File 'lib/yard/server/commands/base.rb', line 26

def adapter
  @adapter
end

- (String) body

The response body

Returns:

  • (String)

    the response body

Since:

  • 0.6.0



23
24
25
# File 'lib/yard/server/commands/base.rb', line 23

def body
  @body
end

- (Boolean) caching

Whether to cache

Returns:

  • (Boolean)

    whether to cache

Since:

  • 0.6.0



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

Returns:

  • (Hash)

    the options passed to the command's constructor

Since:

  • 0.6.0



8
9
10
# File 'lib/yard/server/commands/base.rb', line 8

def command_options
  @command_options
end

- (Hash{String => String}) headers

Response headers

Returns:

Since:

  • 0.6.0



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

Returns:

  • (String)

    the path after the command base URI

Since:

  • 0.6.0



14
15
16
# File 'lib/yard/server/commands/base.rb', line 14

def path
  @path
end

- (Request) request

Request object

Returns:

  • (Request)

    request object

Since:

  • 0.6.0



11
12
13
# File 'lib/yard/server/commands/base.rb', line 11

def request
  @request
end

- (Numeric) status

Status code

Returns:

  • (Numeric)

    status code

Since:

  • 0.6.0



20
21
22
# File 'lib/yard/server/commands/base.rb', line 20

def status
  @status
end

Instance Method Details

- (Object) cache(data) (protected)

Since:

  • 0.6.0



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)

Since:

  • 0.6.0



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.message if e.message != 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

Since:

  • 0.6.0



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)

Raises:

Since:

  • 0.6.0



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)

Since:

  • 0.6.0



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(options)
  when nil
    cache Templates::Engine.render(options)
  else
    cache object
  end
end

- (Object) run

Raises:

  • (NotImplementedError)

Since:

  • 0.6.0



55
56
57
# File 'lib/yard/server/commands/base.rb', line 55

def run
  raise NotImplementedError
end