Sha256: d7c05d460ebbc7b88ea5e5180809a0bd35c8ee1c8ad6e81b1c3587a1a26d4f3d
Contents?: true
Size: 1.49 KB
Versions: 4
Compression:
Stored size: 1.49 KB
Contents
# frozen_string_literal: true require File.join(File.dirname(__FILE__), 'service') module Charrington # Inserts and modifies a Postgres database class TransformPostgres include Service attr_accessor :event attr_reader :top_level_keys Error = Class.new(StandardError) EventNil = Class.new(Error) TableNameNil = Class.new(Error) ColumnBlacklist = Class.new(Error) KEY_FILTER_BLACKLIST = %w[host path jwt sequence].freeze KEY_RAISE_BLACKLIST = %w[id inserted_at].freeze def initialize(event) raise EventNil, 'Event is nil' if event.nil? event = event.to_hash @event = drop_keys(event) @top_level_keys = @event.keys check_blacklist end def call flattened = flatten_hash(event) top_level_keys.each { |k| event.delete(k) } flattened.each_pair { |key, val| event[key] = val } event end private def check_blacklist arr = [] KEY_RAISE_BLACKLIST.each { |k| arr << k if event.keys.include?(k) } raise ColumnBlacklist, "Event contains these blacklisted keys: #{arr.join(',')}" unless arr.empty? end def drop_keys(event) event.delete_if { |k, _v| k.start_with?('@') || KEY_FILTER_BLACKLIST.include?(k) } end def flatten_hash(hash) hash.each_with_object({}) do |(k, v), acc| if v.is_a? Hash flatten_hash(v).map do |h_k, h_v| acc["#{k}_#{h_k}"] = h_v end else acc[k] = v end end end end end
Version data entries
4 entries across 4 versions & 1 rubygems