lib/keikokuc/notification.rb in keikokuc-0.6 vs lib/keikokuc/notification.rb in keikokuc-0.7

- old
+ new

@@ -13,43 +13,48 @@ # else # # handle error # end # class Keikokuc::Notification - ATTRS = %w[message url severity - target_name account_email - producer_api_key remote_id - errors read_at account_sequence].freeze + attr_accessor :message, :url, :severity, + :target_name, :account_email, + :producer_api_key, :remote_id, + :errors, :read_at, :account_sequence - attr_accessor *ATTRS - # Public: Initialize a notification # # opts - a hash of attributes to be set on constructed object # # Examples # # notification = Keikokuc::Notification.new(message: 'hello') # - # All keys on matching ATTRS will be set + # All keys on the attr_accessor list will be set def initialize(opts = {}) - ATTRS.each do |attribute| - if opts.has_key?(attribute.to_sym) - send("#{attribute}=", opts[attribute.to_sym]) - end - end - @client = opts[:client] + @message = opts[:message] + @url = opts[:url] + @severity = opts[:severity] + @target_name = opts[:target_name] + @account_email = opts[:account_email] + @producer_api_key = opts[:producer_api_key] + @remote_id = opts[:remote_id] + @errors = opts[:errors] + @read_at = opts[:read_at] + @account_sequence = opts[:account_sequence] + @client = opts[:client] end # Public: publishes this notification to keikoku # # This method sets the `remote_id` attribute if it succeeds. # If it fails, the `errors` hash will be populated. # # Returns a boolean set to true if publishing succeeded def publish - response, error = client.post_notification(to_hash) + hash = to_hash + hash.delete(:client) + response, error = client.post_notification(hash) if error.nil? self.remote_id = response[:id] self.errors = nil elsif error == Keikokuc::Client::InvalidNotification self.errors = response[:errors] @@ -80,13 +85,22 @@ # Internal: coerces this notification to a hash # # Returns this notification's attributes as a hash def to_hash - ATTRS.inject({}) do |h, attribute| - h[attribute.to_sym] = send(attribute) - h - end + { + :message => @message, + :url => @url, + :severity => @severity, + :target_name => @target_name, + :account_email => @account_email, + :producer_api_key => @producer_api_key, + :remote_id => @remote_id, + :errors => @errors, + :read_at => @read_at, + :account_sequence => @account_sequence, + :client => @client + } end def client # :nodoc: @client ||= Keikokuc::Client.new(:producer_api_key => producer_api_key) end