Class: EventMachine::JsonRPC::Server::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/em-jsonrpc/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, id, rpc_method, params) ⇒ Request

Returns a new instance of Request.



150
151
152
153
154
155
# File 'lib/em-jsonrpc/server.rb', line 150

def initialize(conn, id, rpc_method, params)
  @conn = conn
  @id = id
  @rpc_method = rpc_method
  @params = params
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



148
149
150
# File 'lib/em-jsonrpc/server.rb', line 148

def id
  @id
end

#paramsObject (readonly)

Returns the value of attribute params.



148
149
150
# File 'lib/em-jsonrpc/server.rb', line 148

def params
  @params
end

#rpc_methodObject (readonly)

Returns the value of attribute rpc_method.



148
149
150
# File 'lib/em-jsonrpc/server.rb', line 148

def rpc_method
  @rpc_method
end

Instance Method Details

#reply_custom_error(code, message) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/em-jsonrpc/server.rb', line 193

def reply_custom_error(code, message)
  return nil if @conn.error?
  unless code.is_a? Integer and (-32099..-32000).include? code
    raise ArgumentError, "code must be an integer between -32099 and -32000"
  end
  @conn.reply_error(@id, code, message)
end

#reply_internal_error(message = nil) ⇒ Object



178
179
180
181
# File 'lib/em-jsonrpc/server.rb', line 178

def reply_internal_error(message=nil)
  return nil if @conn.error?
  @conn.reply_error(@id, CODE_INTERNAL_ERROR, message || MSG_INTERNAL_ERROR)
end

#reply_invalid_params(message = nil) ⇒ Object



188
189
190
191
# File 'lib/em-jsonrpc/server.rb', line 188

def reply_invalid_params(message=nil)
  return nil if @conn.error?
  @conn.reply_error(@id, CODE_INVALID_PARAMS, message || MSG_INVALID_PARAMS)
end

#reply_method_not_found(message = nil) ⇒ Object



183
184
185
186
# File 'lib/em-jsonrpc/server.rb', line 183

def reply_method_not_found(message=nil)
  return nil if @conn.error?
  @conn.reply_error(@id, CODE_METHOD_NOT_FOUND, message || MSG_METHOD_NOT_FOUND)
end

#reply_result(result) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/em-jsonrpc/server.rb', line 157

def reply_result(result)
  return nil if @conn.error?

  response = {
    KEY_JSONRPC => VALUE_VERSION,
    KEY_ID => @id,
    KEY_RESULT => result
  }

  # Send the response in chunks (good in case of a big response).
  begin
    @conn.encoder.encode(response) do |chunk|
      @conn.send_data(chunk)
    end
    return true
  rescue Yajl::EncodeError => e
    reply_internal_error "response encode error: #{e.message}"
    return false
  end
end