Module: Cloudinary::Analytics

Extended by:
Analytics
Included in:
Analytics
Defined in:
lib/cloudinary/analytics.rb

Constant Summary collapse

QUERY_KEY =
'_a'
ALGO_VERSION =

The version of the algorithm

'B'
SDK_CODE =

Cloudinary Ruby SDK

'C'
CHARS =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
BINARY_PAD_SIZE =
6

Instance Method Summary collapse

Instance Method Details

#encode_version(version) ⇒ String

Encodes a semVer-like version string.

Example:

input:      '1.24.0'
explode:    ['1','24','0']
pad:        ['01','24','00']
reverse:    ['00', '24', '01']
implode:    '002401'
int:        2401
binary:     '100101100001'
padded:     '000000100101100001'
str_split:  ['000000', '100101', '100001']
getKey:     ['A', 'l', 'h']
implode:    'Alh'

Parameters:

  • version (String)

    Can be either x.y.z or x.y

Returns:

  • (String)

    A string built from 3 characters of the base64 table

Raises:

  • (RangeError)

    when version is larger than 43.21.26



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cloudinary/analytics.rb', line 116

def encode_version(version)
  parts = version.split('.')

  padded_parts  = parts.map { |part| part.rjust(2, '0') }
  number        = padded_parts.reverse.join.to_i
  padded_binary = int_to_padded_bin(number, parts.length * BINARY_PAD_SIZE)

  raise RangeError, 'Version must be smaller than 43.21.26' if padded_binary.length % BINARY_PAD_SIZE != 0

  encoded_chars = padded_binary.chars.each_slice(BINARY_PAD_SIZE).map { |slice| get_key(slice.join) }

  encoded_chars.join
end

#get_key(binary_value) ⇒ Array, Object

Gets the key for binary value.

Parameters:

  • binary_value (String)

    The value.

Returns:



135
136
137
138
139
# File 'lib/cloudinary/analytics.rb', line 135

def get_key(binary_value)
  @char_codes ||= initialize_char_codes

  @char_codes[binary_value] || ''
end

#initialize_char_codesObject



141
142
143
144
145
# File 'lib/cloudinary/analytics.rb', line 141

def initialize_char_codes
  char_codes = {}
  CHARS.chars.each_with_index { |char, idx| char_codes[int_to_padded_bin(idx, BINARY_PAD_SIZE)] = char }
  char_codes
end

#int_to_padded_bin(integer, pad_num) ⇒ String

Converts integer to left padded binary string.

Parameters:

  • integer (Integer)

    The input.

  • pad_num (Integer)

    The num of padding chars.

Returns:

  • (String)

    The padded binary string.



153
154
155
# File 'lib/cloudinary/analytics.rb', line 153

def int_to_padded_bin(integer, pad_num)
  integer.to_s(2).rjust(pad_num, '0')
end

#product(product) ⇒ void

This method returns an undefined value.

Sets the product code.

Used for integrations.

Parameters:

  • product (String)

    The product code to set. ‘A’ is for the official SDK. ‘B’ for integrations.



53
54
55
# File 'lib/cloudinary/analytics.rb', line 53

def product(product)
  @product = product
end

#sdk_analytics_query_paramString

Gets the SDK analytics signature query parameter.

Returns:

  • (String)

    The SDK signature query parameter.



25
26
27
# File 'lib/cloudinary/analytics.rb', line 25

def sdk_analytics_query_param
  "#{QUERY_KEY}=#{self.sdk_analytics_signature}"
end

#sdk_analytics_signatureString

Gets the SDK signature by encoding the SDK version and tech version.

Returns:

  • (String)

    The SDK signature.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cloudinary/analytics.rb', line 32

def sdk_analytics_signature
  return @signature unless @signature.nil?

  begin
    @signature = ALGO_VERSION + @product + @sdk_code + encode_version(@sdk_version) + encode_version(@tech_version)
  rescue RangeError
    @signature = 'E'
  end

  @signature
end

#sdk_code(sdk_code) ⇒ void

This method returns an undefined value.

Sets the SDK code.

Used for integrations.

Parameters:

  • sdk_code (String)

    The SDK code to set.



66
67
68
# File 'lib/cloudinary/analytics.rb', line 66

def sdk_code(sdk_code)
  @sdk_code = sdk_code
end

#sdk_version(sdk_version) ⇒ void

This method returns an undefined value.

Sets the SDK version.

Used for integrations.

Parameters:

  • sdk_version (String)

    The SDK version to set (MAJOR.MINOR.PATCH), for example: “1.0.0”.



79
80
81
# File 'lib/cloudinary/analytics.rb', line 79

def sdk_version(sdk_version)
  @sdk_version = sdk_version
end

#tech_version(tech_version) ⇒ void

This method returns an undefined value.

Sets the tech version.

Used for integrations.

Parameters:

  • tech_version (String)

    The tech version to set (MAJOR.MINOR), for example: “1.0”.



92
93
94
# File 'lib/cloudinary/analytics.rb', line 92

def tech_version(tech_version)
  @tech_version = tech_version.split('.').first(2).join('.')
end