Module: Bike::Response

Defined in:
lib/_response.rb

Overview

Author

Akira FUNAI

Copyright

Copyright © 2009 Akira FUNAI

Class Method Summary collapse

Class Method Details

.forbidden(result = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/_response.rb', line 49

def forbidden(result = {})
  body = result[:body] || 'Forbidden'
  [
    403,
    {
      'Content-Type'   => 'text/html',
      'Content-Length' => body.size.to_s,
    },
    [body],
  ]
end

.no_content(result = {}) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/_response.rb', line 26

def no_content(result = {})
  [
    204,
    (result[:headers] || {}),
    []
  ]
end

.not_found(result = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/_response.rb', line 61

def not_found(result = {})
  body = result[:body] || 'Not Found'
  [
    404,
    {
      'Content-Type'   => 'text/html',
      'Content-Length' => body.size.to_s,
    },
    [body]
  ]
end

.ok(result = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/_response.rb', line 10

def ok(result = {})
  body = result[:body].to_s
  return not_found(result) if body.empty?
  [
    200,
    (
      result[:headers] ||
      {
        'Content-Type'   => 'text/html',
        'Content-Length' => body.size.to_s,
      }
    ),
    [body],
  ]
end

.see_other(result = {}) ⇒ Object



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

def see_other(result = {})
  body = <<_html
<a href="#{result[:location]}">see other</a>
_html
  [
    303,
    {
      'Content-Type'   => 'text/html',
      'Content-Length' => body.size.to_s,
      'Location'       => result[:location],
    },
    [body]
  ]
end

.unprocessable_entity(result = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/_response.rb', line 73

def unprocessable_entity(result = {})
  body = result[:body].to_s
  return not_found(result) if body.empty?
  [
    422,
    (
      result[:headers] ||
      {
        'Content-Type'   => 'text/html',
        'Content-Length' => body.size.to_s,
      }
    ),
    [body],
  ]
end