Class: Alula::Util
- Inherits:
-
Object
- Object
- Alula::Util
- Defined in:
- lib/alula/util.rb
Constant Summary collapse
- CAMELIZE_MAPPING =
Some fields use odd casing (abbreviations mostly) that require a strict mapping for camelization to work properly
{ 'ce_arming_supervision_trouble_zones' => 'CEArmingSupervisionTroubleZones', 'support_url' => 'supportURL' }.freeze
Class Method Summary collapse
- .camelize(under_scored_word) ⇒ Object
-
.convert_hex_crc?(value) ⇒ Boolean
Check if BVN hex conversion needed.
- .deep_merge(first, second) ⇒ Object
- .model_errors_from_response(response) ⇒ Object
- .split_on_word(string, separator_match = /-|_/, &block) ⇒ Object
-
.underscore(camel_cased_word) ⇒ Object
Based on ActiveSupport’s underscore method activesupport/lib/active_support/inflector/methods.rb, line 90.
- .upper_camelcase(under_scored_word) ⇒ Object
Class Method Details
.camelize(under_scored_word) ⇒ Object
29 30 31 32 33 |
# File 'lib/alula/util.rb', line 29 def camelize(under_scored_word) return CAMELIZE_MAPPING[under_scored_word.to_s] if CAMELIZE_MAPPING.key?(under_scored_word.to_s) words = under_scored_word.to_s.split(/-|_/) words.each_with_index.map{ |el, i| i == 0 ? el.downcase : el.capitalize }.join end |
.convert_hex_crc?(value) ⇒ Boolean
Check if BVN hex conversion needed
25 26 27 |
# File 'lib/alula/util.rb', line 25 def convert_hex_crc?(value) [32, 33, 34, 35].include? value end |
.deep_merge(first, second) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/alula/util.rb', line 44 def deep_merge(first, second) merger = proc do |key, v1, v2| if Hash === v1 && Hash === v2 v1.merge(v2, &merger) elsif Array === v1 && Array === v2 v1 | v2 elsif [:undefined, nil, :nil].include?(v2) v1 else v2 end end first.merge(second.to_h, &merger) end |
.model_errors_from_response(response) ⇒ Object
60 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 |
# File 'lib/alula/util.rb', line 60 def model_errors_from_response(response) errors = response.data['errors'] return false unless errors error_object = errors.each_with_object({}) do |err, collector| # # This is duplicitive but it ensures that the model # itself will have _some_ kind of error detail if we need to print that out. collector[:model] ||= [] collector[:model] << err['detail'] # Error objects contain a bunch of info, but all the good stuff # is stored in a sub-object named meta # # meta looks something like this # { # fieldName: 'field error description', # context: { # // extra info about the request # } # } # We need the fieldName to join errors to fields, and context gives extra # info that's good for debugging. err['meta']&.each_pair do |attribute, val| if attribute == 'context' collector[:context] ||= [] collector[:context] << val else underscore_field = Alula::Util.underscore(attribute).to_sym collector[underscore_field] = val end end end # # De-duplicate context so we don't have the same info (requestId) # existing for every field that failed. error_object[:context] = error_object[:context].uniq.compact if error_object[:context] # # We have UI clients that depend on this being a string. # TODO: Update clients and let this be an array? error_object[:model] = error_object[:model].uniq.compact.join(', ') error_object end |
.split_on_word(string, separator_match = /-|_/, &block) ⇒ Object
39 40 41 |
# File 'lib/alula/util.rb', line 39 def split_on_word(string, separator_match = /-|_/, &block) string.to_s.split(separator_match) end |
.underscore(camel_cased_word) ⇒ Object
Based on ActiveSupport’s underscore method activesupport/lib/active_support/inflector/methods.rb, line 90
14 15 16 17 18 19 20 21 22 |
# File 'lib/alula/util.rb', line 14 def underscore(camel_cased_word) return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/ word = camel_cased_word.to_s.gsub(/::/, '/') word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end |
.upper_camelcase(under_scored_word) ⇒ Object
35 36 37 |
# File 'lib/alula/util.rb', line 35 def upper_camelcase(under_scored_word) under_scored_word.to_s.split(/-|_/).map(&:capitalize).join end |