Sha256: c7382ca49daafa10ffd484c5adddda56a14a30d367e0741b5f4b91f3a741ff12

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

require 'liquid'
require 'feedbook/liquid_extensions'
require 'feedbook/factories/notifiers_factory'
require 'feedbook/errors/template_syntax_error'
require 'feedbook/errors/invalid_variables_format_error'

module Feedbook
  class Notification
    
    attr_reader :type, :template, :variables

    # Initializes Notification instance
    # @param opts = {} [Hash] Hash with 
    # 
    # @return [type] [description]
    def initialize(opts = {})
      @type      = opts.fetch(:type, '')
      @variables = opts.fetch(:variables, {})
      @template  = parse_template(opts.fetch(:template, ''))
    end

    # Notifies selected gateway about new post
    # @param object [Object] objct that respond to :to_hash method
    # 
    # @return [NilClass] nil
    def notify(object)
      message = template.render(object.to_hash.merge(variables))
      
      notifier.notify(message)
    end

    # Validates if given parameters are valid
    # 
    # @return [NilClass] nil
    # @raise [Feedbook::Errors::InvalidVariablesFormatError] if variables parameter is not a Hash
    def valid?
      unless variables.is_a? Hash
        raise Errors::InvalidVariablesFormatError.new
      end
    end

    private

    # Parses template from string into a valid Liquid::Template
    # @param template [String] String with valid Liquid template
    # 
    # @return [Liquid::Template] compiled Liquid template
    # @raise [Feedbook::Errors::TemplateSyntaxError] if there is a SyntaxError inside template
    def parse_template(template)
      Liquid::Template.parse(template)
    rescue SyntaxError => e
      raise Errors::TemplateSyntaxError.new(e.message)
    end

    # Returms Notifier instance
    # 
    # @return [Notifier] Notifier instance for given type
    def notifier
      @notifier ||= Factories::NotifiersFactory.create(type)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
feedbook-1.1.0 lib/feedbook/notification.rb
feedbook-1.0.0 lib/feedbook/notification.rb