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 }



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

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

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

Parameters:

  • ary (Array)

Returns:

  • (Hash)


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

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_tree_request(tree, root_parameters) ⇒ 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



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_sessionNet::HTTP

Returns an http session on the server.

Returns:

  • (Net::HTTP)

    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

#sessionObject

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

Returns:

  • the value returned by the block



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