Module: AgnosticBackend::Utilities::InstanceMethods
- Defined in:
- lib/agnostic_backend/utilities.rb
Instance Method Summary collapse
- #convert_bool_values_to_string_in(document) ⇒ Object
- #convert_to(type, value) ⇒ Object
- #convert_to_boolean(value) ⇒ Object
- #exponential_backoff_base ⇒ Object
- #exponential_backoff_max_attempts ⇒ Object
- #exponential_backoff_max_time ⇒ Object
- #exponential_backoff_sleep_time(max, base, attempts) ⇒ Object
- #flat_hash(document, delimiter) ⇒ Object
- #flatten(document, delimiter = '__') ⇒ Object
- #is_boolean?(value) ⇒ Boolean
- #is_date?(value) ⇒ Boolean
- #is_float?(value) ⇒ Boolean
- #is_integer?(value) ⇒ Boolean
- #reject_blank_values_from(flat_document) ⇒ Object
- #transform_nested_values(hash, proc) ⇒ Object
- #unflatten(document, delimiter = '__') ⇒ Object
- #unflatten_hash(hash, delimiter) ⇒ Object
- #value_for_key(document, key) ⇒ Object
- #with_exponential_backoff(error, &block) ⇒ Object
Instance Method Details
#convert_bool_values_to_string_in(document) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/agnostic_backend/utilities.rb', line 125 def convert_bool_values_to_string_in(document) document.each do |k, v| if v.is_a?(TrueClass) document[k] = 'true' elsif v.is_a?(FalseClass) document[k] = 'false' end end end |
#convert_to(type, value) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 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 |
# File 'lib/agnostic_backend/utilities.rb', line 135 def convert_to(type, value) case type when :integer if value.is_a?(Fixnum) value elsif is_integer?(value) value.to_i else value end when :date, :date_array if value.is_a?(Time) value elsif is_date?(value) value.to_time.utc else value end when :double if value.is_a?(Float) value elsif is_float?(value) value.to_f else value end when :boolean if value.is_a?(TrueClass) || value.is_a?(FalseClass) value elsif is_boolean?(value) convert_to_boolean(value) else value end when :string,:string_array,:text,:text_array if value.is_a?(String) value else value.to_s end else value end end |
#convert_to_boolean(value) ⇒ Object
196 197 198 199 200 201 202 203 |
# File 'lib/agnostic_backend/utilities.rb', line 196 def convert_to_boolean(value) case value when 'true' true when 'false' false end end |
#exponential_backoff_base ⇒ Object
38 39 40 |
# File 'lib/agnostic_backend/utilities.rb', line 38 def exponential_backoff_base @exponential_backoff_max_time ||= 0.2 end |
#exponential_backoff_max_attempts ⇒ Object
34 35 36 |
# File 'lib/agnostic_backend/utilities.rb', line 34 def exponential_backoff_max_attempts @exponential_backoff_max_time ||= 10 end |
#exponential_backoff_max_time ⇒ Object
30 31 32 |
# File 'lib/agnostic_backend/utilities.rb', line 30 def exponential_backoff_max_time @exponential_backoff_max_time ||= 4 end |
#exponential_backoff_sleep_time(max, base, attempts) ⇒ Object
42 43 44 45 |
# File 'lib/agnostic_backend/utilities.rb', line 42 def exponential_backoff_sleep_time(max, base, attempts) temp = [max, base * 2 ** attempts].min.to_f temp/2 + rand(0.0...temp/2) end |
#flat_hash(document, delimiter) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/agnostic_backend/utilities.rb', line 48 def flat_hash(document, delimiter) flatten_doc = {} document.each do |k, v| if v.is_a? Hash # if we have a nested hash, traverse the hash until we find a value # When a value is found, we return it and merge the keys with the key of the previous recursion iteration flat_hash(v, delimiter).map do |doc_k, doc_v| flatten_doc["#{k}"+ delimiter + "#{doc_k}"] = doc_v end else flatten_doc[k.to_s] = v end end return flatten_doc end |
#flatten(document, delimiter = '__') ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/agnostic_backend/utilities.rb', line 47 def flatten(document, delimiter = '__') def flat_hash(document, delimiter) flatten_doc = {} document.each do |k, v| if v.is_a? Hash # if we have a nested hash, traverse the hash until we find a value # When a value is found, we return it and merge the keys with the key of the previous recursion iteration flat_hash(v, delimiter).map do |doc_k, doc_v| flatten_doc["#{k}"+ delimiter + "#{doc_k}"] = doc_v end else flatten_doc[k.to_s] = v end end return flatten_doc end flat_hash(document, delimiter) end |
#is_boolean?(value) ⇒ Boolean
188 189 190 |
# File 'lib/agnostic_backend/utilities.rb', line 188 def is_boolean?(value) value == 'true' || value == 'false' end |
#is_date?(value) ⇒ Boolean
192 193 194 |
# File 'lib/agnostic_backend/utilities.rb', line 192 def is_date?(value) value.to_time.present? rescue false end |
#is_float?(value) ⇒ Boolean
184 185 186 |
# File 'lib/agnostic_backend/utilities.rb', line 184 def is_float?(value) (/^[-+]?(\d*[.])?\d+$/ =~ value.to_s).present? end |
#is_integer?(value) ⇒ Boolean
180 181 182 |
# File 'lib/agnostic_backend/utilities.rb', line 180 def is_integer?(value) (/^[-+]?\d+$/ =~ value.to_s).present? end |
#reject_blank_values_from(flat_document) ⇒ Object
121 122 123 |
# File 'lib/agnostic_backend/utilities.rb', line 121 def reject_blank_values_from(flat_document) flat_document.reject { |_, v| (v.is_a?(FalseClass) ? false : v.blank?) } end |
#transform_nested_values(hash, proc) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/agnostic_backend/utilities.rb', line 92 def transform_nested_values(hash, proc) hash.keys.each do |key| if hash[key].kind_of? Hash transform_nested_values(hash[key], proc) else hash[key] = proc.call(hash[key]) end end hash end |
#unflatten(document, delimiter = '__') ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/agnostic_backend/utilities.rb', line 68 def unflatten(document, delimiter='__') unflatten = {} def unflatten_hash(hash, delimiter) key, value = hash.keys.first, hash.values.first components = key.split(delimiter) new_key = components.shift if components.empty? return {new_key => value} else unflat_hash = {} unflat_hash[new_key] = unflatten_hash({components.join(delimiter) => value}, delimiter) end unflat_hash end document.each do |k, v| unflatten.deep_merge!(unflatten_hash({k => v}, delimiter)) end unflatten end |
#unflatten_hash(hash, delimiter) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/agnostic_backend/utilities.rb', line 71 def unflatten_hash(hash, delimiter) key, value = hash.keys.first, hash.values.first components = key.split(delimiter) new_key = components.shift if components.empty? return {new_key => value} else unflat_hash = {} unflat_hash[new_key] = unflatten_hash({components.join(delimiter) => value}, delimiter) end unflat_hash end |
#value_for_key(document, key) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/agnostic_backend/utilities.rb', line 103 def value_for_key(document, key) keys = key.split('.') key = keys.shift if document.is_a?(Hash) if document.has_key? key value_for_key(document[key], keys.join('.')) else return nil end else if document.present? && key.nil? return document else return nil end end end |
#with_exponential_backoff(error, &block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/agnostic_backend/utilities.rb', line 14 def with_exponential_backoff(error, &block) attempts = 0 begin block.call rescue error => e if attempts < exponential_backoff_max_attempts sleep(exponential_backoff_sleep_time(exponential_backoff_max_time, exponential_backoff_base, attempts)) attempts += 1 retry else raise e end end end |