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_store_attachments_request(entities, attachments) ⇒ Object
-
#build_tree_request(tree, root_parameters, attachments = nil) ⇒ 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.
- #run_query(session, http_req_body) ⇒ ServerResponses::QueryResponse
-
#session ⇒ Object
Starts an http session yielding the block passed.
- #store_attachments(session, http_req_body) ⇒ Object
Constructor Details
#initialize(config) ⇒ Server
Returns a new instance of Server.
11 12 13 14 |
# File 'lib/sqlcached_client/server.rb', line 11 def initialize(config) @host = config[:host] @port = config[:port] end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
8 9 10 |
# File 'lib/sqlcached_client/server.rb', line 8 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
8 9 10 |
# File 'lib/sqlcached_client/server.rb', line 8 def port @port end |
Instance Method Details
#build_request(ary) ⇒ Hash
Builds a ‘standard’ request body
47 48 49 |
# File 'lib/sqlcached_client/server.rb', line 47 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_store_attachments_request(entities, attachments) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/sqlcached_client/server.rb', line 79 def (entities, ) { resultset: entities, attachments: } end |
#build_tree_request(tree, root_parameters, attachments = nil) ⇒ Object
Builds a request body suitable for a ‘tree’ request
55 56 57 58 59 60 61 |
# File 'lib/sqlcached_client/server.rb', line 55 def build_tree_request(tree, root_parameters, = nil) h = { tree: tree, root_parameters: root_parameters } if !.nil? h[:attachments] = .map(&:to_query_format) end h end |
#get_session ⇒ Net::HTTP
Returns an http session on the server.
87 88 89 90 |
# File 'lib/sqlcached_client/server.rb', line 87 def get_session url = server_url Net::HTTP.start(url.host, url.port) end |
#run_query(session, http_req_body) ⇒ ServerResponses::QueryResponse
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sqlcached_client/server.rb', line 17 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) resp_body = if (resp['Content-Type'] || '') =~ /application\/json/ JSON.parse(resp.body) else resp.body end if 200 == resp.code.to_i ServerResponses::QueryResponse.new(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.
95 96 97 98 99 100 |
# File 'lib/sqlcached_client/server.rb', line 95 def session s = get_session ret_value = yield(self, s) if block_given? s.finish ret_value end |
#store_attachments(session, http_req_body) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/sqlcached_client/server.rb', line 36 def (session, http_req_body) req = Net::HTTP::Post.new() req.set_content_type('application/json') req.body = http_req_body.to_json resp = session.request(req) 201 == resp.code.to_i || raise("Failed to save attachments - server answered with #{resp.body.inspect}") end |