cotcube-helpers 0.2.2.4 → 0.2.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e416bde4f3c3a8dbba8290c74fa4a54d8f0b5f84f61eb9af8e7bd669bab5a43
4
- data.tar.gz: abb4a2842d5b11f2fcb38bbede59bd693b18117547c94d9eb11a2342d1f76665
3
+ metadata.gz: 7496f5774fbcb9b661dac0f9315081a4b5f4d30a8c89334ef13d0639b789ba73
4
+ data.tar.gz: 596ab3ed0cc578c6321beb1b07b6400e109f4537d91cf0903a0c0542b9a962df
5
5
  SHA512:
6
- metadata.gz: 60f1bc2438ef256bc3d772dec73e22a12711e4088a8ffa07463976c0931e149d471f9d7ea93c6930aff3b96fe06d15537cf96319946ed0fb4cbbbdf70573295a
7
- data.tar.gz: b372776b0d180c6007b0c652d1eeb056d1845ac12775a8853e6e9e9ba9de128cf411527c4912174c722d5c6c4705d4524240665e917928ab50bb9ca0a3fe53cf
6
+ metadata.gz: 7447d4f4d2297c60eb37a815b8cd0c91ddd70d1c4af0d2f8bab89ada65c2ac0c328da6e6dd2e99f46d39d75cc765ee4d724598c3d9fdbf42cd15b6a7f30a22fc
7
+ data.tar.gz: 348426b27122a2be123f93f4db63b7d57e032cf2451f6822a10fce94124d9d0f2cf6c9d3621b920daa651e80ec1b69a5e777d4d94a169cb03f6067f13d1d693f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## 0.2.2.5 (December 23, 2021)
2
+ - hash_ext: reworked keys_to_sym!
3
+ - minor changes
4
+ - deep_decode_datetime: added helper for conversion of timestrings in nested arrays and hashed
5
+ - cache_client: rewritten as class
6
+ - gemspec: raised activesupport to version 7
7
+
1
8
  ## 0.2.2.4 (December 07, 2021)
2
9
  - introduced cache_client as client to readcache
3
10
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2.4
1
+ 0.2.2.5
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
27
  spec.require_paths = ['lib']
28
28
 
29
- spec.add_dependency 'activesupport', '~> 6'
29
+ spec.add_dependency 'activesupport', '~> 7'
30
30
  spec.add_dependency 'colorize', '~> 0.8'
31
31
 
32
32
 
@@ -3,26 +3,50 @@ require 'json'
3
3
 
4
4
  module Cotcube
5
5
  module Helpers
6
- def cached(query, timezone: Cotcube::Helpers::CHICAGO, debug: false, deflate: false)
7
- # TODO: set param to enable deflate on transmission via HTTPARRTY Header
8
- request_headers = {}
9
- request_headers['Accept-Encoding' => 'deflate'] if deflate
10
- res = JSON.parse(HTTParty.get("http://100.100.0.14:8081/#{query}").parsed_response, symbolize_names: true) rescue { error: 1, msg: "Could not parse response for query '#{query}'." }
11
- unless res[:error] and res[:error].zero?
12
- puts "ERROR: #{res}"
13
- return false
6
+ class CacheClient
7
+
8
+ def initialize(query='keys', timezone: Cotcube::Helpers::CHICAGO, debug: false, deflate: false, update: false)
9
+ raise ArgumentError, "Query must not be empty." if [ nil, '' ].include? query
10
+ raise ArgumentError, "Query '#{query}' is garbage." if query.split('/').size > 2 or not query.match? /\A[a-zA-Z0-9?=\/]+\Z/
11
+ @update = update ? '?update=true' : ''
12
+ @request_headers = {}
13
+ @request_headers['Accept-Encoding'] = 'deflate' if deflate
14
+ @query = query
15
+ @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}'." }
16
+ retry_once if has_errors?
14
17
  end
15
- #res[:valid_until] = timezone.parse(res[:valid_until])
16
- #res[:modified] = timezone.parse(res[:modified_at])
17
- if debug
18
- puts "Warnings: #{res[:warnings]}"
19
- puts "Modified: #{res[:modified]}"
20
- puts "Valid_un: #{res[:valid_until]}"
21
- puts "payload: #{res[:payload].to_s.size}"
18
+
19
+ def retry_once
20
+ sleep 2
21
+ raw = HTTParty.get("http://100.100.0.14:8081/#{query}#{update}")
22
+ @result = JSON.parse(raw.body, symbolize_names: true) rescue { error: 1, msg: "Could not parse response for query '#{query}'." }
23
+ if has_errors?
24
+ puts "ERROR in parsing response: #{raw[..300]}"
25
+ end
26
+ end
27
+
28
+ def has_errors?
29
+ result[:error].nil? or result[:error] > 0
30
+ end
31
+
32
+ def warnings
33
+ result[:warnings]
22
34
  end
23
- res[:payload]
24
- end
25
35
 
26
- module_function :cached
36
+ def payload
37
+ has_errors? ? false : @result[:payload]
38
+ end
39
+
40
+ def entity
41
+ query.split('/').first
42
+ end
43
+
44
+ def asset
45
+ entity, asset = query.split('/')
46
+ asset
47
+ end
48
+
49
+ attr_reader :query, :result, :update
50
+ end
27
51
  end
28
52
  end
@@ -30,6 +30,8 @@ module Cotcube
30
30
 
31
31
 
32
32
  CHICAGO = Time.find_zone('America/Chicago')
33
+ NEW_YORK = Time.find_zone('America/New_York')
34
+ BERLIN = Time.find_zone('Europe/Berlin')
33
35
 
34
36
  DATE_FMT = '%Y-%m-%d'
35
37
 
@@ -44,7 +44,7 @@ module Cotcube
44
44
  # otherwise times out --- the counterpart here is the subscription within
45
45
  # setup_reply_queue
46
46
  #
47
- def command(command, timeout: 5)
47
+ def command(command, timeout: 10)
48
48
  command = { command: command.to_s } unless command.is_a? Hash
49
49
  command[:timestamp] ||= (Time.now.to_f * 1000).to_i
50
50
  request_id = Digest::SHA256.hexdigest(command.to_json)[..6]
@@ -0,0 +1,25 @@
1
+ module Cotcube
2
+ module Helpers
3
+ VALID_DATETIME_STRING = lambda {|str| str.is_a?(String) and [10,25,29].include?(str.length) and str.count("^0-9:TZ+-= ").zero? }
4
+
5
+ def deep_decode_datetime(data, zone: DateTime)
6
+ case data
7
+ when nil; nil
8
+ when VALID_DATETIME_STRING
9
+ res = nil
10
+ begin
11
+ res = zone.parse(data)
12
+ rescue ArgumentError
13
+ data
14
+ end
15
+ [ DateTime, ActiveSupport::TimeWithZone ].include?(res.class) ? res : data
16
+ when Array; data.map! { |d| deep_decode_datetime(d, zone: zone) }
17
+ when Hash; data.transform_values! { |v| deep_decode_datetime(v, zone: zone) }
18
+ else; data
19
+ end
20
+ end
21
+
22
+ module_function :deep_decode_datetime
23
+ end
24
+ end
25
+
@@ -2,15 +2,15 @@
2
2
 
3
3
  # Monkey patching the Ruby Core class Hash
4
4
  class Hash
5
- def keys_to_sym
6
- each_key do |key|
5
+ def keys_to_sym!
6
+ self.keys.each do |key|
7
7
  case self[key].class.to_s
8
8
  when 'Hash'
9
- self[key].keys_to_sym
9
+ self[key].keys_to_sym!
10
10
  when 'Array'
11
- self[key].map { |el| el.is_a?(Hash) ? el.keys_to_sym : el }
11
+ self[key].map { |el| el.is_a?(Hash) ? el.keys_to_sym! : el }
12
12
  end
13
- self[key.to_sym] = delete(key)
13
+ self["#{key}".to_sym] = delete(key)
14
14
  end
15
15
  self
16
16
  end
@@ -34,7 +34,7 @@ module Cotcube
34
34
  end
35
35
 
36
36
  defaults = {
37
- data_path: '/var/cotcube/' + name,
37
+ data_path: '/var/cotcube/' + name.split('::').last.downcase,
38
38
  }
39
39
 
40
40
  config = defaults.merge(config)
@@ -21,6 +21,7 @@ require_relative 'cotcube-helpers/subpattern'
21
21
  require_relative 'cotcube-helpers/parallelize'
22
22
  require_relative 'cotcube-helpers/simple_output'
23
23
  require_relative 'cotcube-helpers/simple_series_stats'
24
+ require_relative 'cotcube-helpers/deep_decode_datetime'
24
25
  require_relative 'cotcube-helpers/constants'
25
26
  require_relative 'cotcube-helpers/input'
26
27
  require_relative 'cotcube-helpers/output'
@@ -1,6 +1,6 @@
1
1
  #!/bin/bash
2
2
 
3
- export rubyenv=/home/pepe/.rvm/environments/default
3
+ export rubyenv=/home/pepe/.rvm/environments/ruby-2.7.5
4
4
 
5
5
  . $rubyenv
6
6
  cd /home/pepe/GEMS/${1}
@@ -10,6 +10,7 @@ ruby ${2} ${3} ${4} ${5} ${6}
10
10
 
11
11
 
12
12
  exit
13
+
13
14
  for testing run
14
15
  env - `cat /home/pepe/bin/cron_ruby_wrapper.sh | tail -n 6` /bin/bash
15
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cotcube-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2.4
4
+ version: 0.2.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin L. Tischendorf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-07 00:00:00.000000000 Z
11
+ date: 2021-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '6'
19
+ version: '7'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '6'
26
+ version: '7'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: colorize
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -100,6 +100,7 @@ files:
100
100
  - lib/cotcube-helpers/constants.rb
101
101
  - lib/cotcube-helpers/data_client.rb
102
102
  - lib/cotcube-helpers/datetime_ext.rb
103
+ - lib/cotcube-helpers/deep_decode_datetime.rb
103
104
  - lib/cotcube-helpers/enum_ext.rb
104
105
  - lib/cotcube-helpers/expiration.rb
105
106
  - lib/cotcube-helpers/get_id_set.rb