Class: Cotcube::Helpers::CacheClient

Inherits:
Object
  • Object
show all
Defined in:
lib/cotcube-helpers/cache_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query = 'keys', timezone: Cotcube::Helpers::CHICAGO, debug: false, deflate: false, update: false) ⇒ CacheClient

Returns a new instance of CacheClient.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
# File 'lib/cotcube-helpers/cache_client.rb', line 8

def initialize(query='keys', timezone: Cotcube::Helpers::CHICAGO, debug: false, deflate: false, update: false)
  raise ArgumentError, "Query must not be empty." if [ nil, '' ].include? query
  raise ArgumentError, "Query '#{query}' is garbage." if query.split('/').size > 2 or not query.match? /\A[a-zA-Z0-9?=\/]+\Z/
  @update = update ? '?update=true' : ''
  @request_headers = {} 
  @request_headers['Accept-Encoding'] = 'deflate' if deflate
  @query = query
  @result = JSON.parse(HTTParty.get("http://100.100.0.14:8081/#{query}#{@update}").body, headers: @request_headers, symbolize_names: true) rescue { error: 1, msg: "Could not parse response for query '#{query}'." }
  retry_once if has_errors?
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



49
50
51
# File 'lib/cotcube-helpers/cache_client.rb', line 49

def query
  @query
end

#resultObject (readonly)

Returns the value of attribute result.



49
50
51
# File 'lib/cotcube-helpers/cache_client.rb', line 49

def result
  @result
end

#updateObject (readonly)

Returns the value of attribute update.



49
50
51
# File 'lib/cotcube-helpers/cache_client.rb', line 49

def update
  @update
end

Instance Method Details

#assetObject



44
45
46
47
# File 'lib/cotcube-helpers/cache_client.rb', line 44

def asset
  entity, asset = query.split('/')
  asset
end

#entityObject



40
41
42
# File 'lib/cotcube-helpers/cache_client.rb', line 40

def entity
  query.split('/').first
end

#has_errors?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/cotcube-helpers/cache_client.rb', line 28

def has_errors?
  result[:error].nil? or result[:error] > 0
end

#payloadObject



36
37
38
# File 'lib/cotcube-helpers/cache_client.rb', line 36

def payload
  has_errors? ? false : @result[:payload]
end

#retry_onceObject



19
20
21
22
23
24
25
26
# File 'lib/cotcube-helpers/cache_client.rb', line 19

def retry_once
  sleep 2
  raw = HTTParty.get("http://100.100.0.14:8081/#{query}#{update}")
  @result = JSON.parse(raw.body, symbolize_names: true) rescue { error: 1, msg: "Could not parse response for query '#{query}'." }
  if has_errors?
    puts "ERROR in parsing response: #{raw[..300]}"
  end
end

#warningsObject



32
33
34
# File 'lib/cotcube-helpers/cache_client.rb', line 32

def warnings
  result[:warnings]
end