Module: Notificon

Defined in:
lib/notificon.rb,
lib/notificon/cache.rb,
lib/notificon/version.rb,
lib/notificon/controller.rb,
lib/notificon/user_state.rb,
lib/notificon/mongo_store.rb,
lib/notificon/notification.rb,
lib/notificon/user_state_store.rb,
lib/notificon/notification_store.rb

Overview

Private: Datamapper for Notification object

Defined Under Namespace

Modules: Cache, Controller, MongoStore Classes: Notification, NotificationStore, UserState, UserStateStore

Constant Summary collapse

VERSION =
"0.0.4".freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cacheObject

Public: cache to be used



35
36
37
# File 'lib/notificon.rb', line 35

def cache
  @cache
end

Class Method Details

.add_notification(username, item_url, item_text, actor, action, occured_at, item_id = nil) ⇒ Object

Public: Add a notification to the users lists

username - The String identifying the user being notified item_url - A String url of the item notification relates to item_text - A String that describes the item actor - The String identifying the user that performed the action action - A Symbol representing the action that the actor performed occured_at - The Time the action was performed item_id - A String uniquely identifying the item the notification is for

Returns the String id of the Notification generated

Raises:

  • (ArgumentError)


132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/notificon.rb', line 132

def add_notification(username, item_url, item_text, actor, action, occured_at, item_id=nil)
  raise ArgumentError.new("username must be a String") unless username.is_a? String
  raise ArgumentError.new("item_url must be a String") unless item_url.is_a? String
  raise ArgumentError.new("item_text must be a String") unless item_text.is_a? String
  raise ArgumentError.new("actor must be a String") unless actor.is_a? String
  raise ArgumentError.new("action must be a Symbol") unless action.is_a? Symbol
  raise ArgumentError.new("occured_at must be a Time") unless occured_at .is_a? Time

  id = notification_store.add(Notification.new(:username => username, :item_url => item_url,
    :item_text => item_text, :actor => actor, :action => action, :occured_at => occured_at, :item_id => item_id))
  update_user_unread_counts(username)
  id
end

.clear_notifications(username) ⇒ Object

Public: Clear all unread notifications for the user

username - The String identifying the user

Returns nothing

Raises:

  • (ArgumentError)


184
185
186
187
188
189
# File 'lib/notificon.rb', line 184

def clear_notifications(username)
  raise ArgumentError.new("username must be a String") unless username.is_a? String

  notification_store.mark_all_read_for_user(username, Time.now)
  user_state_store.clear_notifications(username)
end

.connection_profileObject

Public: Returns the connection profile for the datastore. Currently only mongo

supported. Defaults to mongodb://localhost/notificon_default if not set


62
63
64
# File 'lib/notificon.rb', line 62

def connection_profile
  @_connection_profile ||= "mongodb://localhost/notificon_default"
end

.connection_profile=(value) ⇒ Object

Public: Sets the String for the connection_profile

Raises:

  • (ArgumentError)


67
68
69
70
71
# File 'lib/notificon.rb', line 67

def connection_profile=(value)
  raise ArgumentError.new("value must a string or nil") unless value.nil? || value.is_a?(String)

  @_connection_profile = value
end

.get_notification(id) ⇒ Object

Public : Retrieve a specific notification

id - String id of the notification

Returns Notification



151
152
153
# File 'lib/notificon.rb', line 151

def get_notification(id)
  notification_store.get(id)
end

.get_notifications(username, limit = 100) ⇒ Object

Public: Retrieve the most recent Notifications for user

username - The String identifying the user limit - The Fixnum maximum number of items to return (default: 100)

Returns Enumerable list of Notification elements

Raises:

  • (ArgumentError)


161
162
163
164
165
166
# File 'lib/notificon.rb', line 161

def get_notifications(username, limit=100)
  raise ArgumentError.new("username must be a String") unless username.is_a? String
  raise ArgumentError.new("limit must be a Fixnum") unless limit.is_a? Fixnum

  notification_store.get_for_user(username, limit)
end

.get_user_state(username) ⇒ Object

Public: Retrieve the known state of the user, ie notifcation count

username - The String identifying the user

Returns UserState object describing the state

Raises:

  • (ArgumentError)


173
174
175
176
177
# File 'lib/notificon.rb', line 173

def get_user_state(username)
  raise ArgumentError.new("username must be a String") unless username.is_a? String

  user_state_store.get(username)
end

.loggerObject

Public: Returns the Logger currently enabled for the gem

If none specified, will create one pointing at STDOUT

Returns the Logger currently configured



77
78
79
# File 'lib/notificon.rb', line 77

def logger
  @_logger ||= build_logger
end

.logger=(value) ⇒ Object

Public: Setter for the Logger for the gem to use. Allows host system

loggers to be inject, eg Rails.logger

value - The Logger to use

Returns the assigned Logger



87
88
89
# File 'lib/notificon.rb', line 87

def logger=(value)
  @_logger = value
end

.mark_all_read_for_item(username, item_id, read_at) ⇒ Object

Public: Mark a notification as read

username - The String identifying the user item_id - The String identifying the notification item read_at - The Time the notitication was read

Returns nothing

Raises:

  • (ArgumentError)


112
113
114
115
116
117
118
119
# File 'lib/notificon.rb', line 112

def mark_all_read_for_item(username, item_id, read_at)
  raise ArgumentError.new("username must be a String") unless username.is_a? String
  raise ArgumentError.new("item_id must be a String") unless item_id.is_a? String
  raise ArgumentError.new("read_at must be a Time") unless read_at.is_a? Time

  notification_store.mark_all_read_for_item(username, item_id, read_at)
  update_user_unread_counts(username)
end

.mark_notification_read(id, read_at) ⇒ Object

Public: Mark a notification as read

id - The String identifying the notification read_at - The Time the notitication was read

Returns nothing

Raises:

  • (ArgumentError)


97
98
99
100
101
102
103
# File 'lib/notificon.rb', line 97

def mark_notification_read(id, read_at)
  raise ArgumentError.new("id must be a String") unless id.is_a? String
  raise ArgumentError.new("read_at must be a Time") unless read_at.is_a? Time

  notification = notification_store.mark_as_read(id, read_at)
  update_user_unread_counts(notification.username)
end

.notification_id_paramObject

Public: Returns the query string parameter used to identify the notification.

defaults to '_notificon_id' if not set


39
40
41
# File 'lib/notificon.rb', line 39

def notification_id_param
  @_notification_id_param ||= '_notificon_id'
end

.notification_id_param=(value) ⇒ Object

Public: Sets the String to use for the notification_id_param

Raises:

  • (ArgumentError)


54
55
56
57
58
# File 'lib/notificon.rb', line 54

def notification_id_param=(value)
  raise ArgumentError.new("value must a string or nil") unless value.nil? || value.is_a?(String)

  @_notification_id_param = value
end

.notification_item_id_paramObject

Public: Returns the query string parameter used to identify the notification item.



44
45
46
# File 'lib/notificon.rb', line 44

def notification_item_id_param
  "#{notification_id_param}_item"
end

.notification_username_paramObject

Public: Returns the query string parameter used to identify the user.



49
50
51
# File 'lib/notificon.rb', line 49

def notification_username_param
  "#{notification_id_param}_user"
end

.setup {|_self| ... } ⇒ Object

Public: accessor method used to config the gem

Yields the Notificon module

Examples

Notificon.setup do |config|
  config.connection_profile = 'mongo://myserver/notifications'
end

Returns nothing

Yields:

  • (_self)

Yield Parameters:

  • _self (Notificon)

    the object that the method was called on



30
31
32
# File 'lib/notificon.rb', line 30

def setup
  yield self
end

.update_user_unread_counts(username) ⇒ Object



191
192
193
# File 'lib/notificon.rb', line 191

def update_user_unread_counts(username)
  user_state_store.set_notifications(username, notification_store.unread_count_for_user(username))
end