Class: GyozaApp

Inherits:
Object
  • Object
show all
Defined in:
lib/gyoza-languages/gyoza_app.rb

Overview

A wrapper for a web server. Works with rackup to start a new HTTP server.

Direct Known Subclasses

GyozaLanguageApp

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#handlerObject

Returns the value of attribute handler.



13
14
15
# File 'lib/gyoza-languages/gyoza_app.rb', line 13

def handler
  @handler
end

#portObject

Returns the value of attribute port.



13
14
15
# File 'lib/gyoza-languages/gyoza_app.rb', line 13

def port
  @port
end

Instance Method Details

#call(env) ⇒ Object

The method invoked by Puma when receiving an HTTP request.

Uses get, post, put, patch and delete methods to separate requests. If the REQUEST_METHOD does not match any of the previously mentioned methods, returns the 405 HTTP status code.

Arguments:

env: the environment variables at the time of receiving the request


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gyoza-languages/gyoza_app.rb', line 58

def call(env)
  method = env['REQUEST_METHOD']
  path = env['REQUEST_PATH']
  query = StringUtils.query_string_to_hash(env['QUERY_STRING'])
  case method
  when 'GET'
    get(path, query, env)
  when 'POST'
    post(path, query, env)
  when 'PUT'
    put(path, query, env)
  when 'PATCH'
    patch(path, query, env)
  when 'DELETE'
    delete(path, query, env)
  when 'HEAD'
    head(path, query, env)
  else
    response 405
  end
end

#response(code, body = nil, headers = {}) ⇒ Object

Formats a new HTTP response from the given HTTP status code.

Arguments:

code: the status code
body: the data sent to the client. The method will first try to
      convert the data in Json (unless it is String). If it fails,
      it will return the data accordingly
headers: the headers to pass to the response.
         By default, this value is empty, and will be populated
         with the server name and current date


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/gyoza-languages/gyoza_app.rb', line 156

def response(code, body = nil, headers = {})
  if code < 100 || code > 599
    raise GyozaError.invalidStatusCode(code)
  end

  actual_headers = {}
  headers.each do |key, value|
    actual_headers[key.to_s.titleize] = value
  end
  headers = actual_headers

  headers['Server'] = GyozaLanguages::SERVER_NAME
  headers['Date'] = Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT')

  unless body.nil?
    if body.is_a?(String)
      headers['Content-Type'] = 'text/plain'
    else
      body = JSON.dump(body)
      headers['Content-Type'] = 'application/json'
    end
  end

  [code, headers, body.nil? ? [] : [body]]
end

#start(port = GyozaLanguages::DEFAULT_PORT) ⇒ Object

Starts a new server at the specified port using a Rackup handler which is stored in the ‘handler’ attribute.

If the server is already running (A.K.A. the handler attribute is set), a GyozaError is raised.

Arguments:

port: the port to start the server at


23
24
25
26
27
28
29
30
31
# File 'lib/gyoza-languages/gyoza_app.rb', line 23

def start(port = GyozaLanguages::DEFAULT_PORT)
  if @handler.nil?
    @handler = Rackup::Handler.default
    @port = port
    @handler.run(self, :Port => port)
  else
    raise GyozaError.serverAlreadyStarted(port)
  end
end

#stopObject

Stops the server.

If the server is not running (A.K.A. the handler attribute is not set), a GyozaError is raised.



37
38
39
40
41
42
43
44
45
# File 'lib/gyoza-languages/gyoza_app.rb', line 37

def stop
  if @handler.nil?
    raise GyozaError.serverNotStarted
  else
    @handler.stop
    @handler = nil
    @port = nil
  end
end