lib/keikokuc/notification.rb in keikokuc-0.0.3 vs lib/keikokuc/notification.rb in keikokuc-0.1

- old
+ new

@@ -6,24 +6,24 @@ # # notification = Keikokuc::Notification.new(message: 'hello', # severity: 'info', # target: 'sunny-skies-42' # producer_api_key: 'abcd') -# if notificaiton.publish +# if notification.publish # # persist notification # else # # handle error # end # class Keikokuc::Notification ATTRS = %w[message url severity target_name account_email - producer_api_key remote_id errors].freeze + producer_api_key remote_id errors read_at].freeze attr_accessor *ATTRS - # Public: class constructor + # Public: Initialize a notification # # opts - a hash of attributes to be set on constructed object # # Examples # @@ -34,10 +34,12 @@ ATTRS.each do |attribute| if opts.has_key?(attribute.to_sym) send("#{attribute}=", opts[attribute.to_sym]) end end + @read = false + @client = opts[:client] end # Public: publishes this notification to keikoku # # This method sets the `remote_id` attribute if it succeeds. @@ -53,20 +55,40 @@ self.errors = response[:errors] end error.nil? end + # Public: marks this notification as read on keikoku + # + # Marks the notification as read, after which it will + # no longer be displayed to any consumer for this user + # + # Returns a boolean set to true if marking as read succeeded + def read + response, error = client.read_notification(remote_id) + if error.nil? + self.read_at = response[:read_at] + end + error.nil? + end + + # Public: whether this notification is marked as read by this user + # + # Returns true if the user has marked this notification as read + def read? + !!@read_at + end + # 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 end -private def client # :nodoc: @client ||= Keikokuc::Client.new(producer_api_key: producer_api_key) end end