Class: Judopay::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/judopay/serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serialized_object) ⇒ Serializer

Returns a new instance of Serializer.



6
7
8
# File 'lib/judopay/serializer.rb', line 6

def initialize(serialized_object)
  @serialized_object = serialized_object
end

Instance Attribute Details

#serialized_objectObject (readonly)

Returns the value of attribute serialized_object.



4
5
6
# File 'lib/judopay/serializer.rb', line 4

def serialized_object
  @serialized_object
end

Instance Method Details

#as_jsonObject



10
11
12
13
# File 'lib/judopay/serializer.rb', line 10

def as_json
  output_hash = hashify(@serialized_object).camel_case_keys!
  JSON.generate(output_hash)
end

#hashify(source) ⇒ Object

Turn a hash with nested Virtus objects into a simple nested hash



16
17
18
19
20
21
22
23
24
25
# File 'lib/judopay/serializer.rb', line 16

def hashify(source)
  output_hash = {}
  source.to_hash.each do |key, value|
    next if value.nil?

    # Is this a Virtus model object that we need to hashify?
    output_hash[key] = value.class.included_modules.include?(Virtus::Model::Core) ? hashify(value) : value
  end
  output_hash
end