Class: AdaptivePayments::JsonModel

Inherits:
Object
  • Object
show all
Extended by:
Aliases, Parsing
Includes:
Writing
Defined in:
lib/pp-adaptive/support/json_model.rb

Overview

A client-side, nestable, model based on Virtus, mapping JSON parameters to method names and vice-versa

All types defined in the Adaptive Payments API documentation (both request and response) are built from this class.

class CurrencyConversion < JsonModel
  attribute :from,          Node[CurrencyType]
  attribute :to,            Node[CurrencyType]
  attribute :exchange_rate, Decimal, :param => "exchangeRate"

  alias_params :from, {
    :from_amount        => :amount,
    :from_currency_code => :code
  }

  alias_params :to, {
    :to_amount        => :amount,
    :to_currency_code => :code
  }
end

Virtus will allow varying input types and will cast them accordingly, so assigning ‘0.8767’ to #exchange_rate in the above example will actually store BigDecimal(‘0.8767’) in the model.

The optional :param option defines the name of the property in the JSON representation, if it is different from the name of the attribute itself.

In the above example, #alias_params has been used to create shortcuts to the #amount and #code attributes of the two [CurrencyType] attributes, #from and #to. So:

conversion.from_amount == conversion.from.amount
conversion.from_currency_code == conversion.from.code

It’s possible to repeatedly create aliases like this, by chaining them together through multiple levels of the object graph. The Adaptive Payments API is rather deeply nested and verbose by default, so to be a little easier to use and feel more ruby-esque, pp-adaptive define aliases where it seems logical to do so. The fully qualified paths will always work, however.

Defined Under Namespace

Modules: Parsing, Writing

Method Summary

Methods included from Aliases

alias_param, alias_params

Methods included from Parsing

from_json, to_attributes

Methods included from Writing

#to_hash, #to_json