Class: Cotcube::Helpers::CacheClient
- Inherits:
-
Object
- Object
- Cotcube::Helpers::CacheClient
- Defined in:
- lib/cotcube-helpers/cache_client.rb
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#update ⇒ Object
readonly
Returns the value of attribute update.
Instance Method Summary collapse
- #asset ⇒ Object
- #entity ⇒ Object
- #has_errors? ⇒ Boolean
-
#initialize(query = 'keys', timezone: Cotcube::Helpers::CHICAGO, debug: false, deflate: false, update: false) ⇒ CacheClient
constructor
A new instance of CacheClient.
- #payload ⇒ Object
- #retry_once ⇒ Object
- #warnings ⇒ Object
Constructor Details
#initialize(query = 'keys', timezone: Cotcube::Helpers::CHICAGO, debug: false, deflate: false, update: false) ⇒ CacheClient
Returns a new instance of CacheClient.
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
#query ⇒ Object (readonly)
Returns the value of attribute query.
49 50 51 |
# File 'lib/cotcube-helpers/cache_client.rb', line 49 def query @query end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
49 50 51 |
# File 'lib/cotcube-helpers/cache_client.rb', line 49 def result @result end |
#update ⇒ Object (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
#asset ⇒ Object
44 45 46 47 |
# File 'lib/cotcube-helpers/cache_client.rb', line 44 def asset entity, asset = query.split('/') asset end |
#entity ⇒ Object
40 41 42 |
# File 'lib/cotcube-helpers/cache_client.rb', line 40 def entity query.split('/').first end |
#has_errors? ⇒ Boolean
28 29 30 |
# File 'lib/cotcube-helpers/cache_client.rb', line 28 def has_errors? result[:error].nil? or result[:error] > 0 end |
#payload ⇒ Object
36 37 38 |
# File 'lib/cotcube-helpers/cache_client.rb', line 36 def payload has_errors? ? false : @result[:payload] end |
#retry_once ⇒ Object
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 |
#warnings ⇒ Object
32 33 34 |
# File 'lib/cotcube-helpers/cache_client.rb', line 32 def warnings result[:warnings] end |