Module: Transbank::Utils::JSONUtils
- Included in:
- Onepay::Item, Onepay::Request, Onepay::Response, Onepay::ShoppingCart
- Defined in:
- lib/transbank/sdk/utils/json_utils.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#jsonify ⇒ String
Get all instance variables of an instance of a class, then for all of these variables, if the instance of the class’ respond_to? returns true, #send the variable name (so, you’ll get the value of the instance variable’s getter), then save that to a Hash where [key] is the instance variable’s name and [value] is its value.
-
#transform_hash_keys(hash) ⇒ Object
Receive a Hash and return a new hash same as the one we received, but all keys that were strings or camelCase’d are snake_case’d and turned into symbols.
-
#underscore(camel_cased_word) ⇒ Object
FROM stackoverflow.com/a/1509957 Transforms camelCaseWords to snake_case_words.
Class Method Details
.included(mod) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/transbank/sdk/utils/json_utils.rb', line 4 def self.included(mod) # Implement #to_h if the class that includes this module doesn't have it # implemented. Used in several model classes to make them easier to # transform to hashes so they can be transformed to JSON afterwards unless mod.respond_to? :to_h mod.send(:define_method, :to_h) do JSON.parse(self.jsonify) end end end |
Instance Method Details
#jsonify ⇒ String
Get all instance variables of an instance of a class, then for all of these variables, if the instance of the class’ respond_to? returns true, #send the variable name (so, you’ll get the value of the instance variable’s getter), then save that to a Hash where [key] is the instance variable’s name and [value] is its value
Finally, generate a JSON string from this hash above operation
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/transbank/sdk/utils/json_utils.rb', line 24 def jsonify instance_vars = instance_variables.map! { |var| var.to_s.gsub!(/^@/, '') } instance_as_hash = instance_vars.reduce({}) do |resulting_hash, instance_variable| if respond_to? instance_variable value = send(instance_variable) # Safe navigation operator is Ruby 2.3+ value = value.to_h if value && value.respond_to?(:to_h) unless value.is_a? Array value = value.to_a if value && value.respond_to?(:to_a) unless value.is_a? Hash if value.is_a? Array value = value.map {|x| x.respond_to?(:jsonify) ? JSON.parse(x.jsonify) : x } end value = value.jsonify if value.respond_to? :jsonify resulting_hash[instance_variable] = value end resulting_hash end # Some values can't be null and must be removed if they are instance_as_hash.reject! do |key, value| %w[commerce_logo_url width_height].include?(key) && value.nil? end JSON.generate instance_as_hash end |
#transform_hash_keys(hash) ⇒ Object
Receive a Hash and return a new hash same as the one we received, but all keys that were strings or camelCase’d are snake_case’d and turned into symbols. Example: “somevalue” Would return: “somevalue”
54 55 56 57 58 59 60 |
# File 'lib/transbank/sdk/utils/json_utils.rb', line 54 def transform_hash_keys(hash) hash.reduce({}) do |new_hsh, (key, val)| new_key = underscore(key).to_sym new_hsh[new_key] = val new_hsh end end |
#underscore(camel_cased_word) ⇒ Object
FROM stackoverflow.com/a/1509957 Transforms camelCaseWords to snake_case_words
64 65 66 67 68 69 70 |
# File 'lib/transbank/sdk/utils/json_utils.rb', line 64 def underscore(camel_cased_word) camel_cased_word.to_s.gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') .gsub(/([a-z\d])([A-Z])/,'\1_\2') .tr("-", "_") .downcase end |