Class: Xcflushd::StorageKeys

Inherits:
Object
  • Object
show all
Defined in:
lib/xcflushd/storage_keys.rb

Overview

This class defines the interface of the flusher with Redis. It defines how to build all the keys that contain cached reports and authorizations, and also, all the keys used by the pubsub mechanism.

Constant Summary collapse

AUTH_REQUESTS_CHANNEL =

Pubsub channel in which a client publishes for asking about the authorization status of an application.

'xc_channel_auth_requests'.freeze
SET_KEYS_CACHED_REPORTS =

Set that contains the keys of the cached reports

'report_keys'.freeze
SET_KEYS_FLUSHING_REPORTS =

Set that contains the keys of the cached reports to be flushed

'flushing_report_keys'.freeze

Class Method Summary collapse

Class Method Details

.auth_hash_key(service_id, credentials) ⇒ Object

Returns the storage key that contains the cached authorizations for the given { service_id, credentials } pair.



39
40
41
# File 'lib/xcflushd/storage_keys.rb', line 39

def auth_hash_key(service_id, credentials)
  hash_key(:auth, service_id, credentials)
end

.name_key_to_flush(report_key, suffix) ⇒ Object



103
104
105
# File 'lib/xcflushd/storage_keys.rb', line 103

def name_key_to_flush(report_key, suffix)
  "#{KEY_TO_FLUSH_PREFIX}#{report_key}#{suffix}"
end

.pubsub_auth_msg_2_auth_info(msg) ⇒ Object

Returns a hash that contains service_id, credentials, and metric from a message published in the pubsub channel for auth requests. Expected format of the message:

service_id:<service_id>,<credentials>,metric:<metric>.
With all the ',' and ':' in the values escaped.
<credentials> contains the credentials needed for authentication
separated by ','. For example: app_id:my_app_id,user_key:my_user_key.


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/xcflushd/storage_keys.rb', line 65

def pubsub_auth_msg_2_auth_info(msg)
  msg_split = msg.split(/(?<!\\),/)
  service_id = msg_split.first.sub('service_id:'.freeze, ''.freeze)
  creds = Credentials.from(
      msg_split[1..-2].join(',').sub('credentials:'.freeze, ''.freeze))
  metric = msg_split.last.sub('metric:'.freeze, ''.freeze)

  res = { service_id: service_id, credentials: creds, metric: metric }
  res.map do |k, v|
    # Credentials are already unescaped
    [k, v.is_a?(Credentials) ? v : v.gsub("\\,", ','.freeze)
                                    .gsub("\\:", ':'.freeze)]
  end.to_h
end

.pubsub_auths_resp_channel(service_id, credentials, metric) ⇒ Object

Pubsub channel to which the client subscribes to receive a response after asking for an authorization.



51
52
53
54
55
56
# File 'lib/xcflushd/storage_keys.rb', line 51

def pubsub_auths_resp_channel(service_id, credentials, metric)
  AUTH_RESPONSES_CHANNEL_PREFIX +
      "service_id:#{service_id}," +
      "#{credentials.to_sorted_escaped_s}," +
      "metric:#{metric}"
end

.report_hash_key(service_id, credentials) ⇒ Object

Returns the storage key that contains the cached reports for the given { service_id, credentials } pair.



45
46
47
# File 'lib/xcflushd/storage_keys.rb', line 45

def report_hash_key(service_id, credentials)
  hash_key(:report, service_id, credentials)
end

.service_and_creds(key_to_flush, suffix) ⇒ Object

Returns an array of size 2 with a service and the credentials encoded given a key marked as ‘to be flushed’ and its suffix.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/xcflushd/storage_keys.rb', line 82

def service_and_creds(key_to_flush, suffix)
  split_key = key_to_flush.sub("#{KEY_TO_FLUSH_PREFIX}#{REPORT_KEY_PREFIX}", '')
                          .sub(suffix, '')
                          .split(/(?<!\\),/)

  escaped_service = split_key.first
  escaped_creds = split_key[1..-1].join(','.freeze)

  # escaped_service is a string with 'service_id:' followed by the escaped
  # service ID. escaped_creds starts with 'credentials:' and is followed
  # by the escaped credentials.
  service = escaped_service
                .sub('service_id:'.freeze, ''.freeze)
                .gsub("\\,", ','.freeze).gsub("\\:", ':'.freeze)

  creds = Credentials.from(escaped_creds.sub(
      'credentials:'.freeze, ''.freeze))

  [service, creds]
end