Class: OpenSocial::RpcRequest

Inherits:
Request
  • Object
show all
Defined in:
lib/opensocial/request.rb

Constant Summary

Constants inherited from Request

OpenSocial::Request::GET

Instance Attribute Summary collapse

Attributes inherited from Request

#connection, #guid, #key, #pid, #selector

Instance Method Summary collapse

Constructor Details

#initialize(connection, requests = {}) ⇒ RpcRequest

Initializes an RpcRequest with the supplied connection and an optional hash of requests.



175
176
177
178
179
# File 'lib/opensocial/request.rb', line 175

def initialize(connection, requests = {})
  @connection = connection

  @requests = requests
end

Instance Attribute Details

#requestsObject

Defines the requests sent in the single RpcRequest. The requests are stored a key/value pairs.



171
172
173
# File 'lib/opensocial/request.rb', line 171

def requests
  @requests
end

Instance Method Details

#add(requests = {}) ⇒ Object

Adds one or more requests to the RpcRequest. Expects a hash of key/value pairs (key used to refernece the data when it returns => the Request).



183
184
185
# File 'lib/opensocial/request.rb', line 183

def add(requests = {})
  @requests.merge!(requests)
end

#send(unescape = true) ⇒ Object

Sends an RpcRequest to the OpenSocial endpoint by constructing JSON for the POST body and delegating the request to send_request. If an RpcRequest is sent with an empty list of requests, an exception is thrown. The response JSON is optionally unescaped (defaulting to true).



191
192
193
194
195
196
197
198
# File 'lib/opensocial/request.rb', line 191

def send(unescape = true)
  if @requests.length == 0
    raise RequestException.new("RPC request requires a non-empty hash " +
                               "of requests in order to be sent.")
  end

  json = send_request(request_json, unescape)
end

#send_request(post_data, unescape) ⇒ Object

Sends an RpcRequest to the OpenSocial endpoint by constructing the service URI and dispatching the request. This method is public so that an arbitrary POST body can be constructed and sent. The response JSON is optionally unescaped.



204
205
206
207
208
209
# File 'lib/opensocial/request.rb', line 204

def send_request(post_data, unescape)
  uri = @connection.service_uri(@connection.container[:rpc], nil, nil, nil)
  data = dispatch(uri, post_data)

  parse_response(data, unescape)
end