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.



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

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_body(ary) ⇒ Object



48
49
50
# File 'lib/sqlcached_client/server.rb', line 48

def build_request_body(ary)
  { batch: ary }
end

#format_request(query_id, query_template, params, cache) ⇒ Object



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

def format_request(query_id, query_template, params, cache)
  {
    queryId: query_id,
    queryTemplate: query_template,
    queryParams: params,
    cache: cache
  }
end

#get_sessionObject



63
64
65
66
# File 'lib/sqlcached_client/server.rb', line 63

def get_session
  url = server_url
  Net::HTTP.start(url.host, url.port)
end

#parse_response_body(body) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sqlcached_client/server.rb', line 33

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sqlcached_client/server.rb', line 15

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



69
70
71
72
73
74
# File 'lib/sqlcached_client/server.rb', line 69

def session
  s = get_session
  ret_value = yield(self, s) if block_given?
  s.finish
  ret_value
end