Sha256: 14d476593d2942a039c6215203f57e69e86222b7da3ed7475b3595d782960ebe

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module Notifications
  class Client
    class Notification
      FIELDS = [
        :id,
        :api_key,
        :billable_units,
        :to,
        :subject,
        :body,
        :job,
        :notification_type,
        :status,
        :service,
        :sent_at,
        :sent_by,
        :template,
        :template_version,
        :reference,
        :created_at,
        :updated_at
      ].freeze

      attr_reader(*FIELDS)

      def initialize(notification)
        notification = normalize(notification)

        FIELDS.each do |field|
            instance_variable_set(:"@#{field}", notification.fetch(field.to_s, nil)
            )
        end
      end

      [
        :sent_at,
        :created_at,
        :updated_at
      ].each do |field|
        define_method field do
          begin
            value = instance_variable_get(:"@#{field}")
            Time.parse value
          rescue
            value
          end
        end
      end

      private

      def normalize(notification)
        notification.key?('data') ? notification['data']['notification'] : notification
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
notifications-ruby-client-1.1.2 lib/notifications/client/notification.rb
notifications-ruby-client-1.1.1 lib/notifications/client/notification.rb
notifications-ruby-client-1.0.0 lib/notifications/client/notification.rb