Class: SqlcachedClient::Server
- Inherits:
-
Object
- Object
- SqlcachedClient::Server
- Defined in:
- lib/sqlcached_client/server.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#build_request(ary) ⇒ Hash
Builds a ‘standard’ request body.
-
#build_request_item(query_id, query_template, params, cache) ⇒ Hash
Formats the parameters passed in the way the server expects.
-
#build_tree_request(tree, root_parameters) ⇒ Object
Builds a request body suitable for a ‘tree’ request.
-
#get_session ⇒ Net::HTTP
An http session on the server.
-
#initialize(config) ⇒ Server
constructor
A new instance of Server.
- #parse_response_body(body) ⇒ Object
- #run_query(session, http_req_body) ⇒ Object
-
#session ⇒ Object
Starts an http session yielding the block passed.
Constructor Details
#initialize(config) ⇒ Server
Returns a new instance of Server.
10 11 12 13 |
# File 'lib/sqlcached_client/server.rb', line 10 def initialize(config) @host = config[:host] @port = config[:port] end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
7 8 9 |
# File 'lib/sqlcached_client/server.rb', line 7 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
7 8 9 |
# File 'lib/sqlcached_client/server.rb', line 7 def port @port end |
Instance Method Details
#build_request(ary) ⇒ Hash
Builds a ‘standard’ request body
51 52 53 |
# File 'lib/sqlcached_client/server.rb', line 51 def build_request(ary) { batch: ary } end |
#build_request_item(query_id, query_template, params, cache) ⇒ Hash
Formats the parameters passed in the way the server expects
69 70 71 72 73 74 75 76 |
# File 'lib/sqlcached_client/server.rb', line 69 def build_request_item(query_id, query_template, params, cache) { query_id: query_id, query_template: query_template, query_params: params, cache: cache } end |
#build_tree_request(tree, root_parameters) ⇒ Object
Builds a request body suitable for a ‘tree’ request
59 60 61 |
# File 'lib/sqlcached_client/server.rb', line 59 def build_tree_request(tree, root_parameters) { tree: tree, root_parameters: root_parameters } end |
#get_session ⇒ Net::HTTP
Returns an http session on the server.
79 80 81 82 |
# File 'lib/sqlcached_client/server.rb', line 79 def get_session url = server_url Net::HTTP.start(url.host, url.port) end |
#parse_response_body(body) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sqlcached_client/server.rb', line 34 def parse_response_body(body) if body.is_a?(Array) body.map { |item| parse_response_body(item) } elsif body.is_a?(Hash) if (resultset = body['resultset']).is_a?(String) JSON.parse(resultset) else resultset end else body end end |
#run_query(session, http_req_body) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/sqlcached_client/server.rb', line 16 def run_query(session, http_req_body) req = Net::HTTP::Post.new(data_batch_url) req.set_content_type('application/json') req.body = http_req_body.to_json resp = session.request(req) if 'application/json' == resp['Content-Type'] resp_body = parse_response_body(JSON.parse(resp.body)) else resp_body = resp.body end if 200 == resp.code.to_i resp_body else raise "Got HTTP response #{resp.code} from server - #{resp_body.inspect}" end end |
#session ⇒ Object
Starts an http session yielding the block passed. Closes the connession when the block returns.
87 88 89 90 91 92 |
# File 'lib/sqlcached_client/server.rb', line 87 def session s = get_session ret_value = yield(self, s) if block_given? s.finish ret_value end |