Class: Context

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

Instance Method Summary collapse

Constructor Details

#initialize(method, me, env, body) ⇒ Context

Returns a new instance of Context.



29
30
31
32
33
34
# File 'lib/webfx.rb', line 29

def initialize(method, me, env, body)
  @method = method
  @me = me
  @env = env
  @body = body
end

Instance Method Details

#fail(message) ⇒ Object



66
67
68
# File 'lib/webfx.rb', line 66

def fail(message)
  respond [500, {'Content-Type' => 'text/plain'},  message]
end

#respond(response) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/webfx.rb', line 36

def respond(response)
  @env['async.callback'].call [response[0], response[1], @body]
  @body.call [response[2]]
  
  if response[0] < 400
    @body.succeed
  else
    @body.fail
  end
end

#succeed(result) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/webfx.rb', line 47

def succeed(result)
  if result.kind_of? MyResult
    respond result.env 
    return
  end

  case @method
    when 'get', 'post'
      if result.kind_of? String and  ['{', '['].include? result.slice(0, 1) 
        json = result
      else
        json = result.to_json
      end
      respond [200, {'Content-Type' => 'application/json'}, json]
    else 
      respond [200, {'Content-Type' => 'application/json'}, {:result => :success}.to_json]
  end
end