Module: Lightstreamer::PostRequest
- Defined in:
- lib/lightstreamer/post_request.rb
Overview
This module contains helper methods for sending single and bulk POST requests to a Lightstreamer server and handling the possible error responses.
Class Method Summary collapse
-
.bulk_execute(url, bodies) ⇒ Array<LightstreamerError, nil>
Sends a POST request to the specified Lightstreamer URL that concatenates multiple individual POST request bodies into one to avoid sending lots of individual requests.
-
.execute(url, query) ⇒ Object
Sends a POST request to the specified Lightstreamer URL with the given query params.
-
.parse_error(response_lines) ⇒ LightstreamerError?
Parses the next error from the given lines that were returned by a POST request.
-
.request_body(query) ⇒ String
Returns the request body to send for a POST request with the given options.
Class Method Details
.bulk_execute(url, bodies) ⇒ Array<LightstreamerError, nil>
Sends a POST request to the specified Lightstreamer URL that concatenates multiple individual POST request bodies into one to avoid sending lots of individual requests. The return value is an array with one entry per body and indicates the error state returned by the server for that body’s request, or nil if no error occurred.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/lightstreamer/post_request.rb', line 29 def bulk_execute(url, bodies) response = Excon.post url, body: bodies.join("\r\n"), expects: 200, connect_timeout: 15 response_lines = response.body.split("\n").map(&:strip) errors = [] errors << parse_error(response_lines) until response_lines.empty? raise LightstreamerError if errors.size != bodies.size errors rescue Excon::Error => error raise Errors::ConnectionError, error. end |
.execute(url, query) ⇒ Object
Sends a POST request to the specified Lightstreamer URL with the given query params. If an error occurs then a LightstreamerError subclass will be raised.
14 15 16 17 |
# File 'lib/lightstreamer/post_request.rb', line 14 def execute(url, query) errors = bulk_execute url, [request_body(query)] raise errors.first if errors.first end |
.parse_error(response_lines) ⇒ LightstreamerError?
Parses the next error from the given lines that were returned by a POST request. The consumed lines are removed from the passed array.
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/lightstreamer/post_request.rb', line 67 def parse_error(response_lines) first_line = response_lines.shift return nil if first_line == 'OK' return Errors::SyncError.new if first_line == 'SYNC ERROR' if first_line == 'ERROR' error_code = response_lines.shift LightstreamerError.build response_lines.shift, error_code else LightstreamerError.new first_line end end |
.request_body(query) ⇒ String
Returns the request body to send for a POST request with the given options.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/lightstreamer/post_request.rb', line 49 def request_body(query) params = {} query.each do |key, value| next if value.nil? value = value.map(&:to_s).join(' ') if value.is_a? Array params[key] = value end URI.encode_www_form params end |