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.



163
164
165
166
167
# File 'lib/opensocial/request.rb', line 163

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.



159
160
161
# File 'lib/opensocial/request.rb', line 159

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).



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

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).



179
180
181
182
183
184
185
186
# File 'lib/opensocial/request.rb', line 179

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.



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

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