Class: SqlcachedClient::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/sqlcached_client/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Server

Returns a new instance of Server.

Parameters:

  • config (Hash)

    something like { host: ‘localhost’, port: 8081 }



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

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/sqlcached_client/server.rb', line 8

def host
  @host
end

#portObject (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

Parameters:

  • ary (Array)

Returns:

  • (Hash)


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

Parameters:

  • query_id (String)

    unique identifier for this query

  • query_template (String)

    the sql template

  • params (Hash)

    the parameter to fill the template

  • cache (Integer)

    number of seconds the data should be cached

Returns:

  • (Hash)


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 build_store_attachments_request(entities, attachments)
  {
    resultset: entities,
    attachments: attachments
  }
end

#build_tree_request(tree, root_parameters, attachments = nil) ⇒ Object

Builds a request body suitable for a ‘tree’ request

Parameters:

  • tree (Hash)
  • root_parameters (Array)

    a vector of actual condition parameters for the root query



55
56
57
58
59
60
61
# File 'lib/sqlcached_client/server.rb', line 55

def build_tree_request(tree, root_parameters, attachments = nil)
  h = { tree: tree, root_parameters: root_parameters }
  if !attachments.nil?
    h[:attachments] = attachments.map(&:to_query_format)
  end
  h
end

#get_sessionNet::HTTP

Returns an http session on the server.

Returns:

  • (Net::HTTP)

    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

#sessionObject

Starts an http session yielding the block passed. Closes the connession when the block returns.

Returns:

  • the value returned by the block



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 store_attachments(session, http_req_body)
  req = Net::HTTP::Post.new(store_attachments_url)
  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