Class: JsonRpc::Rpc::AsyncResult

Inherits:
Object
  • Object
show all
Includes:
EventMachine::Deferrable
Defined in:
lib/json-rpc.rb

Overview

The class RpcDeferrable is useful helps you to build a Json Rpc server into an asynchronous way

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = nil) ⇒ AsyncResult

Returns a new instance of AsyncResult.



134
135
136
137
138
139
# File 'lib/json-rpc.rb', line 134

def initialize env = nil
  @env = env
  @status = 200
  @header = {}
  @header_sent = false
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



132
133
134
# File 'lib/json-rpc.rb', line 132

def env
  @env
end

#headerObject

Returns the value of attribute header.



132
133
134
# File 'lib/json-rpc.rb', line 132

def header
  @header
end

#idObject

Returns the value of attribute id.



132
133
134
# File 'lib/json-rpc.rb', line 132

def id
  @id
end

#responseObject (readonly)

Returns the value of attribute response.



131
132
133
# File 'lib/json-rpc.rb', line 131

def response
  @response
end

#statusObject

Returns the value of attribute status.



132
133
134
# File 'lib/json-rpc.rb', line 132

def status
  @status
end

Instance Method Details

#each(&blk) ⇒ Object

FIXME thin specific



161
162
163
# File 'lib/json-rpc.rb', line 161

def each &blk
  @callback = blk
end

#failed(error_code = :internal_error, msg = nil) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/json-rpc.rb', line 152

def failed error_code = :internal_error, msg = nil
  e = Rpc::error(error_code, id, msg)
  @status = e.status
  send_header
  @callback.call(e.result)
  succeed
end

#reply(obj) ⇒ Object



147
148
149
150
# File 'lib/json-rpc.rb', line 147

def reply obj
  send_header unless @callback
  @callback.call(Rpc::forge_response(obj, @id))
end

#send_headerObject



141
142
143
144
145
# File 'lib/json-rpc.rb', line 141

def send_header
  raise "You should pass env to AsyncResult.new()" unless @env
  @env['async.callback'].call([@status, @header, self]) unless @header_sent
  @header_sent = true
end