Class: ExperianConsumerView::Transformers::ResultTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/experian_consumer_view/transformers/result_transformer.rb

Overview

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.

Constant Summary collapse

DEFAULT_ATTRIBUTE_TRANSFORMERS =

Helper code to get a default Transformer ###

[
  ExperianConsumerView::Transformers::Attributes::Match,
  ExperianConsumerView::Transformers::Attributes::PostcodeMosaicUk7Group,
  ExperianConsumerView::Transformers::Attributes::PostcodeMosaicUk7Type
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResultTransformer

Returns a new instance of ResultTransformer.



15
16
17
# File 'lib/experian_consumer_view/transformers/result_transformer.rb', line 15

def initialize
  @attribute_transformers = {}
end

Class Method Details

.defaultObject



58
59
60
61
62
63
64
65
66
# 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

Instance Method Details

#register_attribute_transformer(transformer) ⇒ Object

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 can

    transform.
    
  • transform_attribute - accepts a value for the given attribute as returned by the ConsumerView API, and

    transforms it in some way - usually into a richer data format.
    

Parameters:

  • transformer


28
29
30
# File 'lib/experian_consumer_view/transformers/result_transformer.rb', line 28

def register_attribute_transformer(transformer)
  @attribute_transformers[transformer.attribute_name] = transformer
end

#transform(result_hash) ⇒ Object

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.

Parameters:

  • 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.



40
41
42
43
44
# 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