Sha256: c7a5c417d054c536d1d2882fd9582df5fe887bded99d2811b2600ea34ce4cac5

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

module Mention
  class AlertCreator
    def initialize(account_resource, alert)
      @account_resource, @alert = account_resource, alert
    end

    def created_alert
      @created_alert ||= Alert.new(response['alert'])
    end

    def valid?
      validate_response!
      true
    end

    private
    attr_reader :account_resource, :alert

    def response
      @response ||= JSON.parse(response_str)
    end

    def response_str
      @response_str ||= account_resource['alerts'].post(
        JSON.generate(request_params),
        :content_type => 'application/json')
    end

    def request_params
      alert.attributes
        .reject{|k,v| k == :id}
        .reject{|k,v| k == :shares}
    end

    def validate_response!
      raise ValidationError.new(validation_errors.join(", ")) unless response['alert']
    end

    def validation_errors
      aggregate_errors(response)
    end

    def aggregate_errors(hash_node, list = [])
      if !hash_node.is_a?(Hash)
        []
      elsif hash_node['errors']
        hash_node['errors']
      else
        error_lists = hash_node.map do |key, node|
          aggregate_errors(node)
        end
        error_lists.inject([]) do |list, node_list|
          list + node_list
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mention-api-0.1.0 lib/mention/alert_creator.rb
mention-api-0.0.3 lib/mention/alert_creator.rb
mention-api-0.0.2 lib/mention/alert_creator.rb