Sha256: fd5a0239db9c88f5cd4ed9e429260f8b1999b5d6000028f1fd61fb452fc7b6ce

Contents?: true

Size: 922 Bytes

Versions: 5

Compression:

Stored size: 922 Bytes

Contents

require 'active_support/core_ext/hash/indifferent_access'

class InputSanitizer::V2::PayloadTransform
  attr_reader :original_payload, :context

  def self.call(original_payload, context = {})
    new(original_payload, context).call
  end

  def initialize(original_payload, context = {})
    fail "#{self.class} is missing #transform method" unless respond_to?(:transform)
    @original_payload, @context = original_payload, context
  end

  def call
    transform
    payload
  end

  private
  def rename(from, to)
    if has?(from)
      data = payload.delete(from)
      payload[to] = data
    end
  end

  def merge_in(field, options = {})
    if source = payload.delete(field)
      source = options[:using].call(source) if options[:using]
      payload.merge!(source)
    end
  end

  def has?(key)
    payload.has_key?(key)
  end

  def payload
    @payload ||= original_payload.with_indifferent_access
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
input_sanitizer-0.6.0 lib/input_sanitizer/v2/payload_transform.rb
input_sanitizer-0.5.0 lib/input_sanitizer/v2/payload_transform.rb
input_sanitizer-0.4.1 lib/input_sanitizer/v2/payload_transform.rb
input_sanitizer-0.4.0 lib/input_sanitizer/v2/payload_transform.rb
input_sanitizer-0.3.33 lib/input_sanitizer/v2/payload_transform.rb