Module: AdaptivePayments::JsonModel::Parsing
- Included in:
- AdaptivePayments::JsonModel
- Defined in:
- lib/pp-adaptive/support/json_model.rb
Overview
Methods used for building a JsonModel given a JSON string.
Instance Method Summary collapse
-
#from_json(json) ⇒ JsonModel
Process JSON input and return a JsonModel for the data.
-
#to_attributes(json) ⇒ Hash
Given a ruby Hash representation of raw JSON from the API (or a subset thereof), map all JSON parameter keys to attribute names.
Instance Method Details
#from_json(json) ⇒ JsonModel
Process JSON input and return a JsonModel for the data.
This method must be invoked on a subclass of the correct type. Invoking it on JsonModel itself will do nothing. All parameters in the JSON are recursively mapped to their attribute names, based on :param on the attribute. Unknown parameters are ignored.
58 59 60 |
# File 'lib/pp-adaptive/support/json_model.rb', line 58 def from_json(json) new(to_attributes(JSON.parse(json))) end |
#to_attributes(json) ⇒ Hash
Given a ruby Hash representation of raw JSON from the API (or a subset thereof), map all JSON parameter keys to attribute names
69 70 71 72 |
# File 'lib/pp-adaptive/support/json_model.rb', line 69 def to_attributes(json) json = {} unless json.kind_of?(Hash) json.inject({}) { |hash, (key, value)| (attr_value = attribute_value(key, value)) ? hash.merge(attribute_key(key) => attr_value) : hash } end |