Module: GoodData::Helpers

Extended by:
Hashie::Extensions::StringifyKeys::ClassMethods, Hashie::Extensions::SymbolizeKeys::ClassMethods
Defined in:
lib/gooddata/helpers/csv_helper.rb,
lib/gooddata/helpers/erb_helper.rb,
lib/gooddata/helpers/data_helper.rb,
lib/gooddata/helpers/auth_helpers.rb,
lib/gooddata/helpers/crypto_helper.rb,
lib/gooddata/helpers/global_helpers.rb,
lib/gooddata/helpers/global_helpers_params.rb

Defined Under Namespace

Modules: AuthHelper, CryptoHelper, ErbHelper Classes: Csv, DataSource, DeepMergeableHash

Constant Summary collapse

AES_256_CBC_CIPHER =
'aes-256-cbc'
ENCODED_PARAMS_KEY =
'gd_encoded_params'
ENCODED_HIDDEN_PARAMS_KEY =
'gd_encoded_hidden_params'

Class Method Summary collapse

Class Method Details

.create_lookup(collection, on) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/gooddata/helpers/global_helpers_params.rb', line 204

def create_lookup(collection, on)
  lookup = {}
  if on.is_a?(Array)
    collection.each do |e|
      key = e.values_at(*on)
      lookup[key] = [] unless lookup.key?(key)
      lookup[key] << e
    end
  else
    collection.each do |e|
      key = e[on]
      lookup[key] = [] unless lookup.key?(key)
      lookup[key] << e
    end
  end
  lookup
end

.decode_params(params, options = {}) ⇒ Hash

Decodes params as they came from the platform.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :resolve_reference_params (Boolean)

    Resolve reference parameters in gd_encoded_params or not

Returns:

  • (Hash)

    Decoded parameters



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/gooddata/helpers/global_helpers_params.rb', line 61

def decode_params(params, options = {})
  key = ENCODED_PARAMS_KEY.to_s
  hidden_key = ENCODED_HIDDEN_PARAMS_KEY.to_s
  data_params = params[key] || '{}'
  hidden_data_params = if params.key?(hidden_key) && params[hidden_key].nil?
                         "{\"#{hidden_key}\" : null}"
                       elsif params.key?(hidden_key)
                         params[hidden_key]
                       else
                         '{}'
                       end

  reference_values = []
  # Replace reference parameters by the actual values. Use backslash to escape a reference parameter, e.g: \${not_a_param},
  # the ${not_a_param} will not be replaced
  if options[:resolve_reference_params]
    data_params, reference_values = resolve_reference_params(data_params, params)
    hidden_data_params, = resolve_reference_params(hidden_data_params, params)
  end

  begin
    parsed_data_params = data_params.is_a?(Hash) ? data_params : JSON.parse(data_params)
  rescue JSON::ParserError => exception
    reason = exception.message
    reference_values.each { |secret_value| reason.gsub!("\"#{secret_value}\"", '"***"') }
    raise exception.class, "Error reading json from '#{key}', reason: #{reason}"
  end

  begin
    parsed_hidden_data_params = hidden_data_params.is_a?(Hash) ? hidden_data_params : JSON.parse(hidden_data_params)
  rescue JSON::ParserError => exception
    raise exception.class, "Error reading json from '#{hidden_key}'"
  end

  # Add the nil on ENCODED_HIDDEN_PARAMS_KEY
  # if the data was retrieved from API You will not have the actual values so encode -> decode is not losless. The nil on the key prevents the server from deleting the key
  parsed_hidden_data_params[ENCODED_HIDDEN_PARAMS_KEY] = nil unless parsed_hidden_data_params.empty?

  params.delete(key)
  params.delete(hidden_key)
  params = GoodData::Helpers.deep_merge(params, parsed_data_params)
  params = GoodData::Helpers.deep_merge(params, parsed_hidden_data_params)

  if options[:convert_pipe_delimited_params]
    convert_pipe_delimited_params = lambda do |args|
      args = args.select { |k, _| k.include? "|" }
      lines = args.keys.map do |k|
        hash = {}
        last_a = nil
        last_e = nil
        k.split("|").reduce(hash) do |a, e|
          last_a = a
          last_e = e
          a[e] = {}
        end
        last_a[last_e] = args[k]
        hash
      end

      lines.reduce({}) do |a, e|
        GoodData::Helpers.deep_merge(a, e)
      end
    end

    pipe_delimited_params = convert_pipe_delimited_params.call(params)
    params.delete_if do |k, _|
      k.include?('|')
    end
    params = GoodData::Helpers.deep_merge(params, pipe_delimited_params)
  end

  params
end

.decrypt(database64, key) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/gooddata/helpers/global_helpers.rb', line 249

def decrypt(database64, key)
  if key.nil? || key.empty?
    GoodData.logger.warn('WARNING: No encryption key provided.')
    return 'no_key_provided'
  end

  data = Base64.decode64(database64)

  cipher = OpenSSL::Cipher::Cipher.new(AES_256_CBC_CIPHER)
  cipher.decrypt
  cipher.key = cipher_key = Digest::SHA256.digest(key)
  random_iv = data[0..15] # extract iv from first 16 bytes
  data = data[16..data.size - 1]
  cipher.iv = Digest::SHA256.digest(random_iv + cipher_key)[0..15]
  decrypted = cipher.update(data)
  decrypted << cipher.final
  decrypted
end

.deep_dup(an_object) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/gooddata/helpers/global_helpers.rb', line 174

def deep_dup(an_object)
  case an_object
  when Array
    an_object.map { |it| GoodData::Helpers.deep_dup(it) }
  when Hash
    an_object.each_with_object(an_object.dup) do |(key, value), hash|
      hash[GoodData::Helpers.deep_dup(key)] = GoodData::Helpers.deep_dup(value)
    end
  when Object
    an_object.duplicable? ? an_object.dup : an_object
  end
end

.deep_merge(source, target) ⇒ Object



187
188
189
# File 'lib/gooddata/helpers/global_helpers.rb', line 187

def deep_merge(source, target)
  GoodData::Helpers::DeepMergeableHash[source].deep_merge(target)
end

.diff(old_list, new_list, options = {}) ⇒ Hash

A helper which allows you to diff two lists of objects. The objects can be arbitrary objects as long as they respond to to_hash because the diff is eventually done on hashes. It allows you to specify several options to allow you to limit on what the sameness test is done

four keys. :added contains the list that are in new_list but were not in the old_list :added contains the list that are in old_list but were not in the new_list :same contains objects that are in both lists and they are the same :changed contains list of objects that changed along ith original, the new one and the list of changes

Parameters:

  • old_list (Array<Object>)

    List of objects that serves as a base for comparison

  • new_list (Array<Object>)

    List of objects that is compared agianst the old_list

Returns:

  • (Hash)

    A structure that contains the result of the comparison. There are



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/gooddata/helpers/global_helpers_params.rb', line 149

def diff(old_list, new_list, options = {})
  old_list = old_list.map(&:to_hash)
  new_list = new_list.map(&:to_hash)

  fields = options[:fields]
  lookup_key = options[:key]

  old_lookup = Hash[old_list.map { |v| [v[lookup_key], v] }]

  res = {
    :added => [],
    :removed => [],
    :changed => [],
    :same => []
  }

  new_list.each do |new_obj|
    old_obj = old_lookup[new_obj[lookup_key]]
    if old_obj.nil?
      res[:added] << new_obj
      next
    end

    if fields
      sliced_old_obj = old_obj.slice(*fields)
      sliced_new_obj = new_obj.slice(*fields)
    else
      sliced_old_obj = old_obj
      sliced_new_obj = new_obj
    end
    if sliced_old_obj != sliced_new_obj
      difference = sliced_new_obj.to_a - sliced_old_obj.to_a
      differences = Hash[*difference.mapcat { |x| x }]
      res[:changed] << {
        old_obj: old_obj,
        new_obj: new_obj,
        diff: differences
      }
    else
      res[:same] << old_obj
    end
  end

  new_lookup = Hash[new_list.map { |v| [v[lookup_key], v] }]
  old_list.each do |old_obj|
    new_obj = new_lookup[old_obj[lookup_key]]
    if new_obj.nil?
      res[:removed] << old_obj
      next
    end
  end

  res
end

.encode_hidden_params(params) ⇒ Hash

Encodes hidden parameters for passing them to GD execution platform.

Parameters:

  • params (Hash)

    Parameters to be encoded

Returns:

  • (Hash)

    Encoded parameters



53
54
55
# File 'lib/gooddata/helpers/global_helpers_params.rb', line 53

def encode_hidden_params(params)
  encode_params(params, ENCODED_HIDDEN_PARAMS_KEY)
end

.encode_params(params, data_key) ⇒ Hash

Encodes parameters for passing them to GD execution platform. Core types are kept and complex types (arrays, structures, etc) are JSON encoded into key hash "gd_encoded_params" or "gd_encoded_hidden_params", depending on the 'hidden' method param. The two different keys are used because the params and hidden params are merged by the platform and if we use the same key, the param would be overwritten.

Core types are following:

  • Boolean (true, false)
  • Fixnum
  • Float
  • Nil
  • String

Parameters:

  • params (Hash)

    Parameters to be encoded

Returns:

  • (Hash)

    Encoded parameters



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gooddata/helpers/global_helpers_params.rb', line 28

def encode_params(params, data_key)
  res = {}
  nested = {}
  core_types = [FalseClass, Integer, Float, NilClass, TrueClass, String]
  params.each do |k, v|
    if core_types.include?(v.class)
      res[k] = v
    else
      nested[k] = v
    end
  end
  res[data_key] = nested.to_json unless nested.empty?
  res
end

.encode_public_params(params) ⇒ Hash

Encodes public parameters for passing them to GD execution platform.

Parameters:

  • params (Hash)

    Parameters to be encoded

Returns:

  • (Hash)

    Encoded parameters



46
47
48
# File 'lib/gooddata/helpers/global_helpers_params.rb', line 46

def encode_public_params(params)
  encode_params(params, ENCODED_PARAMS_KEY)
end

.encrypt(data, key) ⇒ Object

encrypts data with the given key. returns a binary data with the unhashed random iv in the first 16 bytes



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/gooddata/helpers/global_helpers.rb', line 225

def encrypt(data, key)
  cipher = OpenSSL::Cipher::Cipher.new(AES_256_CBC_CIPHER)
  cipher.encrypt
  cipher.key = key = Digest::SHA256.digest(key)
  random_iv = cipher.random_iv
  cipher.iv = Digest::SHA256.digest(random_iv + key)[0..15]
  encrypted = cipher.update(data)
  encrypted << cipher.final
  # add unhashed iv to front of encrypted data

  Base64.encode64(random_iv + encrypted)
end

.error(msg) ⇒ Object



40
41
42
43
# File 'lib/gooddata/helpers/global_helpers.rb', line 40

def error(msg)
  GoodData.logger.error(msg)
  exit 1
end

.find_goodfile(pwd = `pwd`.strip!, options = {}) ⇒ Object

FIXME: Windows incompatible



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gooddata/helpers/global_helpers.rb', line 46

def find_goodfile(pwd = `pwd`.strip!, options = {})
  root = Pathname(options[:root] || '/')
  pwd = Pathname(pwd).expand_path
  loop do
    gf = pwd + '.gooddata'
    return gf if File.exist?(gf)
    pwd = pwd.parent
    break if root == pwd
  end
  nil
end

.get_path(an_object, path = [], default = nil) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/gooddata/helpers/global_helpers.rb', line 85

def get_path(an_object, path = [], default = nil)
  return an_object if path.empty?
  return default if an_object.nil?

  path.reduce(an_object) do |a, e|
    a && a.key?(e) ? a[e] : default
  end
end

.hash_dfs(thing, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/gooddata/helpers/global_helpers.rb', line 102

def hash_dfs(thing, &block)
  if !thing.is_a?(Hash) && !thing.is_a?(Array) # rubocop:disable Style/GuardClause
  elsif thing.is_a?(Array)
    thing.each do |child|
      hash_dfs(child, &block)
    end
  else
    thing.each do |key, val|
      yield(thing, key)
      hash_dfs(val, &block)
    end
  end
end

.home_directoryObject



98
99
100
# File 'lib/gooddata/helpers/global_helpers.rb', line 98

def home_directory
  running_on_windows? ? ENV['USERPROFILE'] : ENV['HOME']
end

.interpolate_error_message(error) ⇒ Object



167
168
169
170
171
172
# File 'lib/gooddata/helpers/global_helpers.rb', line 167

def interpolate_error_message(error)
  return unless error && error['error'] && error['error']['message']
  message = error['error']['message']
  params = error['error']['parameters']
  sprintf(message, *params)
end

.interpolate_error_messages(errors) ⇒ Object



163
164
165
# File 'lib/gooddata/helpers/global_helpers.rb', line 163

def interpolate_error_messages(errors)
  errors.map { |e| interpolate_error_message(e) }
end

.join(master, slave, on, on2, options = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/gooddata/helpers/global_helpers.rb', line 123

def join(master, slave, on, on2, options = {})
  full_outer = options[:full_outer]
  inner = options[:inner]

  lookup = create_lookup(slave, on2)
  marked_lookup = {}
  results = master.reduce([]) do |a, line|
    matching_values = lookup[line.values_at(*on)] || []
    marked_lookup[line.values_at(*on)] = 1
    if matching_values.empty? && !inner
      a << line.to_hash
    else
      matching_values.each do |matching_value|
        a << matching_value.to_hash.merge(line.to_hash)
      end
    end
    a
  end

  if full_outer
    (lookup.keys - marked_lookup.keys).each do |key|
      GoodData.logger.info(lookup[key])
      results << lookup[key].first.to_hash
    end
  end
  results
end

.last_uri_part(uri) ⇒ Object



94
95
96
# File 'lib/gooddata/helpers/global_helpers.rb', line 94

def last_uri_part(uri)
  uri.split('/').last
end

.parse_http_exception(e) ⇒ Object



201
202
203
# File 'lib/gooddata/helpers/global_helpers.rb', line 201

def parse_http_exception(e)
  JSON.parse(e.response)
end

.prepare_mapping(what, for_what = nil, options = {}) ⇒ Array<GoodData::MdObject>

It takes what should be mapped to what and creates a mapping that is suitable for other internal methods. This means looking up the objects and returning it as array of pairs. The input can be given in several ways

  1. Hash. For example it could look like => 'label.state.id'

2 Arrays. In such case the arrays are zipped together. First item will be swapped for the first item in the second array etc. ['label.states.name'], ['label.state.id']

Parameters:

  • what (Hash | Array)

    List/Hash of objects to be swapped

  • for_what (Array) (defaults to: nil)

    List of objects to be swapped

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gooddata/helpers/global_helpers.rb', line 71

def prepare_mapping(what, for_what = nil, options = {})
  project = options[:project] || (for_what.is_a?(Hash) && for_what[:project]) || fail('Project has to be provided')
  mapping = if what.is_a?(Hash)
              whats = what.keys
              to_whats = what.values
              whats.zip(to_whats)
            elsif what.is_a?(Array) && for_what.is_a?(Array)
              whats.zip(to_whats)
            else
              [[what, for_what]]
            end
  mapping.pmap { |f, t| [project.objects(f), project.objects(t)] }
end

.running_on_a_mac?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/gooddata/helpers/global_helpers.rb', line 155

def running_on_a_mac?
  RUBY_PLATFORM =~ /-darwin\d/
end

.running_on_windows?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/gooddata/helpers/global_helpers.rb', line 151

def running_on_windows?
  RUBY_PLATFORM =~ /mswin32|mingw32/
end

.simple_decrypt(database64, key) ⇒ Object

Simple decrypt data with given key



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/gooddata/helpers/global_helpers.rb', line 269

def simple_decrypt(database64, key)
  if key.nil? || key.empty?
    GoodData.logger.warn('WARNING: No encryption key provided.')
    return 'no_key_provided'
  end

  data = Base64.decode64(database64)

  cipher = OpenSSL::Cipher::Cipher.new(AES_256_CBC_CIPHER)
  cipher.decrypt
  cipher.key = key
  decrypted = cipher.update(data)
  decrypted << cipher.final
  decrypted
end

.simple_encrypt(data, key) ⇒ Object

Simple encrypt data with given key



239
240
241
242
243
244
245
246
247
# File 'lib/gooddata/helpers/global_helpers.rb', line 239

def simple_encrypt(data, key)
  cipher = OpenSSL::Cipher::Cipher.new(AES_256_CBC_CIPHER)
  cipher.encrypt
  cipher.key = key
  encrypted = cipher.update(data)
  encrypted << cipher.final

  Base64.encode64(encrypted)
end

.stringify_values(value) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/gooddata/helpers/global_helpers_params.rb', line 222

def stringify_values(value)
  case value
  when nil
    value
  when Hash
    Hash[
      value.map do |k, v|
        [k, stringify_values(v)]
      end
    ]
  when Array
    value.map do |v|
      stringify_values(v)
    end
  else
    value.to_s
  end
end

.titleize(str) ⇒ Object



116
117
118
119
120
121
# File 'lib/gooddata/helpers/global_helpers.rb', line 116

def titleize(str)
  titleized = str.gsub(/[\.|_](.)/, &:upcase)
  titleized = titleized.tr('_', ' ')
  titleized[0] = titleized[0].upcase
  titleized
end

.to_boolean(param) ⇒ Boolean

Turns a boolean or string 'true' into boolean. Useful for bricks.

Parameters:

Returns:

  • (Boolean)

    Returns true or false if the input is 'true' or true



219
220
221
# File 'lib/gooddata/helpers/global_helpers.rb', line 219

def to_boolean(param)
  param == 'true' || param == true ? true : false
end

.underline(x) ⇒ Object



159
160
161
# File 'lib/gooddata/helpers/global_helpers.rb', line 159

def underline(x)
  '=' * x.size
end

.undot(params) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/gooddata/helpers/global_helpers.rb', line 191

def undot(params)
  # for each key-value config given
  params.map do |k, v|
    # dot notation to hash
    k.split('__').reverse.reduce(v) do |memo, obj|
      GoodData::Helper.DeepMergeableHash[{ obj => memo }]
    end
  end
end

.zeroes(m, n, val = 0) ⇒ Array<Array>

Creates a matrix with zeroes in all places. It is implemented as an Array of Arrays. First rows then columns.

Parameters:

  • m (Integer)

    Number of rows

  • n (Integer)

    Number of cols

  • val (Integer) (defaults to: 0)

    Alternatively can fill in positions with different values than zeroes. Defualt is zero.

Returns:

  • (Array<Array>)

    Returns a matrix of zeroes



211
212
213
# File 'lib/gooddata/helpers/global_helpers.rb', line 211

def zeroes(m, n, val = 0)
  m.times.map { n.times.map { val } }
end