Class: Rack::HelloWorld

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/hello_world.rb

Instance Method Summary collapse

Instance Method Details

#_call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rack/hello_world.rb', line 7

def _call(env)
  if env['REQUEST_METHOD'] != 'GET'
    return [405, {}, ['']]
  end

  return case env['PATH_INFO']
  when '/echo/accept'
    [200,
      {'Content-Type' => env['HTTP_ACCEPT'], 'Content-Length' => '0'},
      []
    ]
  when '/hello'
    [200,
      # If Content-Length is not given, Rack assumes chunked transfer.
      {'Content-Type' => 'text/plain', 'Content-Length' => '12'},
      ['Hello World!']
    ]
  when '/value'
    @@value ||= 0
    @@value  += 1

    [200,
      {'Content-Type' => 'text/plain', 'ETag' => rand(0xffff).to_s},
      ["#{@@value}"]
    ]
  when '/block'
    n = 17
    [200,
      {'Content-Type' => 'text/plain', 'Content-Length' => n.to_s},
      ['+'*n]
    ]
  when '/code'
    [2.05,
      {'Content-Type' => 'text/plain'},
      []
    ]
  when '/time'
    # Rack::ETag does not add an ETag header, if response code other than
    # 200 or 201, so CoAP/Float return codes do not work, here.
    [200,
      {'Content-Type' => 'text/plain'},
      [Time.now.to_s]
    ]
  when '/cbor'
    require 'json'

    body = JSON.parse(env['rack.input'].read).to_s +
           env['coap.cbor'].to_s

    [200,
      {
        'Content-Type' => 'text/plain',
        'Content-Length' => body.bytesize.to_s
      },
      [body]
    ]
  when '/json'
    require 'json'

    body = {'Hello' => 'World!'}.to_json

    [200,
      {
        'Content-Type' => 'application/json; charset=utf8',
        'Content-Length' => body.bytesize.to_s
      },
      [body]
    ]
  else
    [404, {}, ['']]
  end
end

#call(env) ⇒ Object



3
4
5
# File 'lib/rack/hello_world.rb', line 3

def call(env)
  dup._call(env)
end