Class: Alula::Util
- Inherits:
-
Object
- Object
- Alula::Util
- Defined in:
- lib/alula/util.rb
Overview
Utility methods for the Alula gem
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_dcp_response(response) ⇒ 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
- .upper_camelcase_from_camelcase(camel_cased_word) ⇒ Object
Class Method Details
.camelize(under_scored_word) ⇒ Object
[View source]
33 34 35 36 37 38 |
# File 'lib/alula/util.rb', line 33 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.zero? ? el.downcase : el.capitalize }.join end |
.convert_hex_crc?(value) ⇒ Boolean
Check if BVN hex conversion needed
29 30 31 |
# File 'lib/alula/util.rb', line 29 def convert_hex_crc?(value) [32, 33, 34, 35].include? value end |
.deep_merge(first, second) ⇒ Object
[View source]
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/alula/util.rb', line 53 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_dcp_response(response) ⇒ Object
[View source]
116 117 118 119 120 121 122 |
# File 'lib/alula/util.rb', line 116 def model_errors_from_dcp_response(response) return false unless response.data['error'] || response.data['message'] { model: response.data['message'] } end |
.model_errors_from_response(response) ⇒ Object
[View source]
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 |
# File 'lib/alula/util.rb', line 69 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
[View source]
48 49 50 |
# File 'lib/alula/util.rb', line 48 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
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/alula/util.rb', line 17 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
[View source]
40 41 42 |
# File 'lib/alula/util.rb', line 40 def upper_camelcase(under_scored_word) under_scored_word.to_s.split(/-|_/).map(&:capitalize).join end |
.upper_camelcase_from_camelcase(camel_cased_word) ⇒ Object
[View source]
44 45 46 |
# File 'lib/alula/util.rb', line 44 def upper_camelcase_from_camelcase(camel_cased_word) camel_cased_word.split(/(?=[A-Z])/).map(&:capitalize).join end |