Module: AdaptivePayments::JsonModel::Writing

Included in:
AdaptivePayments::JsonModel
Defined in:
lib/pp-adaptive/support/json_model.rb

Overview

Methods for converting a JsonModel into a JSON string for transport to the API

Instance Method Summary collapse

Instance Method Details

#to_hashHash

Recursively read all attributes in a ruby Hash, mapping attribute names to JSON parameters, according to the :param option

Returns:

  • (Hash)

    the JSON representation in ruby Hash form



100
101
102
103
104
# File 'lib/pp-adaptive/support/json_model.rb', line 100

def to_hash
  Hash[attribute_set.map{ |a| [a.name, self[a.name]] }].
    inject({}) { |hash, (key, value)| value.nil? ? hash : hash.merge(json_key(key) => json_value(value)) }.
    reject { |key, value| value.kind_of?(Enumerable) && value.none? }
end

#to_jsonString

Convert this JsonModel into a JSON string for transport to the PayPal API

Returns:

  • (String)

    the JSON string, containing all children, if any



110
111
112
# File 'lib/pp-adaptive/support/json_model.rb', line 110

def to_json(*)
  to_hash.to_json
end