class ExperianConsumerView::Transformers::ResultTransformer
Default implementation of a class to transform the raw result returned by the ConsumerView API into a richer format. It does this by registering one or more attribute transformers, and iterating over the key/value pairs in the result hash, applying the attribute transformers to the appropriate values.
You may provide your own custom implementations which transform the result hash in any way you wish. The only requirement is implementing the transform
method.
Constants
- DEFAULT_ATTRIBUTE_TRANSFORMERS
Helper code to get a default Transformer ###
Public Class Methods
# File lib/experian_consumer_view/transformers/result_transformer.rb, line 58 def self.default unless @default_transformer @default_transformer = ResultTransformer.new DEFAULT_ATTRIBUTE_TRANSFORMERS.each { |t| @default_transformer.register_attribute_transformer(t) } end @default_transformer end
# File lib/experian_consumer_view/transformers/result_transformer.rb, line 15 def initialize @attribute_transformers = {} end
Public Instance Methods
Registers an attribute transformer on this ResultTransformer
.
An attribute transformer must implement:
-
attribute_name
- returns the name of an attribute, as returned by the ConsumerView API, which it cantransform.
-
transform_attribute
- accepts a value for the given attribute as returned by the ConsumerView API, andtransforms it in some way - usually into a richer data format.
@param transformer
# File lib/experian_consumer_view/transformers/result_transformer.rb, line 28 def register_attribute_transformer(transformer) @attribute_transformers[transformer.attribute_name] = transformer end
Transforms all values in the given result_hash
using the registered attribute transformers. If there is no attribute transformer for a particular key in the result_hash
then the associated value will not be transformed.
@param result_hash [Hash] the raw result hash from the ConsumerView API for a single item which was looked up -
eg. a single individual, household, or postcode.
@returns [Hash] the transformed hash of result data
# File lib/experian_consumer_view/transformers/result_transformer.rb, line 40 def transform(result_hash) result_hash.each do |k, v| result_hash[k] = @attribute_transformers[k].transform_attribute(v) if @attribute_transformers[k] end end