Class: Brief::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/brief/server.rb,
lib/brief/server/socket.rb

Defined Under Namespace

Modules: Handlers Classes: Gateway, Route, Socket

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(briefcase, options = {}) ⇒ Server

Returns a new instance of Server.



4
5
6
7
# File 'lib/brief/server.rb', line 4

def initialize(briefcase, options={})
  @briefcase = briefcase
  @options = options
end

Instance Attribute Details

#briefcaseObject (readonly)

Returns the value of attribute briefcase.



2
3
4
# File 'lib/brief/server.rb', line 2

def briefcase
  @briefcase
end

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/brief/server.rb', line 2

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  request = Brief::Server::Route.new(env, briefcase, options)
  status, headers, body = request.respond()

  body = body.to_json if body.is_a?(Hash)
  body = body.to_json if body.is_a?(Array)
  body = body.as_json.to_json if body.is_a?(Brief::Model)

  body = "" if body.nil?

  headers["Content-Length"]                 = Rack::Utils.bytesize(body).to_s
  headers["Access-Control-Allow-Origin"]    = "*"
  headers["Access-Control-Allow-Methods"]   = "GET, POST, PUT"
  headers["X-BRIEF-HANDLER"] = request.send(:handler).try(:to_s)

  [status, headers, [body]]
end