Class: PassaporteWeb::Notification
- Inherits:
-
Object
- Object
- PassaporteWeb::Notification
- Includes:
- Attributable
- Defined in:
- lib/passaporte_web/notification.rb
Overview
Represents the Notifications of PassaporteWeb.
Constant Summary collapse
- ATTRIBUTES =
[:body, :target_url, :uuid, :absolute_url, :scheduled_to, :sender_data, :read_at, :notification_type, :destination]
- CREATABLE_ATTRIBUTES =
[:body, :target_url, :scheduled_to, :destination]
- UPDATEABLE_ATTRIBUTES =
[]
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
-
.count(show_read = false, since = nil) ⇒ Object
Returns the number of notifications available for the authenticated user.
-
.find_all(page = 1, limit = 20, since = nil, show_read = false, ordering = "oldest-first") ⇒ Object
Finds all Notifications destined to the authenticated user, paginated.
Instance Method Summary collapse
-
#attributes ⇒ Object
Returns a hash with all attribures of the Notification.
-
#destroy ⇒ Object
Excludes a newly created notification.
- #destroyed? ⇒ Boolean
-
#initialize(attributes = {}) ⇒ Notification
constructor
Instanciates a new Notification with the supplied attributes.
- #persisted? ⇒ Boolean
-
#read! ⇒ Object
Marks a notification as read.
-
#save(auth_type = 'user') ⇒ Object
Creates the notification on PassaporteWeb.
Constructor Details
#initialize(attributes = {}) ⇒ Notification
Instanciates a new Notification with the supplied attributes.
Example:
notification = PassaporteWeb::Notification.new(
destination: "ac3540c7-5453-424d-bdfd-8ef2d9ff78df",
body: "Feliz ano novo!",
target_url: "https://app.passaporteweb.com.br",
scheduled_to: "2012-01-01 00:00:00"
)
79 80 81 82 |
# File 'lib/passaporte_web/notification.rb', line 79 def initialize(attributes={}) set_attributes(attributes) @errors = {} end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
14 15 16 |
# File 'lib/passaporte_web/notification.rb', line 14 def errors @errors end |
Class Method Details
.count(show_read = false, since = nil) ⇒ Object
Returns the number of notifications available for the authenticated user. All parameters are optional.
API method: GET /notifications/api/count/
API documentation: app.passaporteweb.com.br/static/docs/notificacao.html#get-notifications-api-count
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/passaporte_web/notification.rb', line 57 def self.count(show_read=false, since=nil) since = since.strftime('%Y-%m-%d') if since && since.respond_to?(:strftime) params = { since: since, show_read: show_read }.reject { |k,v| v.nil? || v.to_s.empty? } response = Http.get("/notifications/api/count/", params, 'user') data = MultiJson.decode(response.body) Integer(data['count']) rescue *[RestClient::BadRequest] => e MultiJson.decode(e.response.body) end |
.find_all(page = 1, limit = 20, since = nil, show_read = false, ordering = "oldest-first") ⇒ Object
Finds all Notifications destined to the authenticated user, paginated. By default returns 20 notifications per request, starting at “page” 1. Returns an OpenStruct object with two attributes notifications
and meta
. notifications
is an array of Notification instances or an empty array if no Notifications are found. meta
is an OpenStruct object with information about limit and available pagination values, to use in subsequent calls to .find_all
. Raises a RestClient::ResourceNotFound
exception if the requested page does not exist. All method parameters are optional.
API method: GET /notifications/api/
API documentation: app.passaporteweb.com.br/static/docs/notificacao.html#get-notifications-api
Example:
data = PassaporteWeb::Notification.find_all
data.notifications # => [notification1, notification2, ...]
data. # => #<OpenStruct limit=20, next_page=2, prev_page=nil, first_page=1, last_page=123>
data..limit # => 20
data..next_page # => 2
data..prev_page # => nil
data..first_page # => 1
data..last_page # => 123
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/passaporte_web/notification.rb', line 36 def self.find_all(page=1, limit=20, since=nil, show_read=false, ordering="oldest-first") since = since.strftime('%Y-%m-%d') if since && since.respond_to?(:strftime) params = { page: page, limit: limit, since: since, show_read: show_read, ordering: ordering }.reject { |k,v| v.nil? || v.to_s.empty? } response = Http.get("/notifications/api/", params, 'user') raw_data = MultiJson.decode(response.body) result_hash = {} result_hash[:notifications] = raw_data.map { |hash| load_notification(hash) } result_hash[:meta] = PassaporteWeb::Helpers.(response.headers[:link]) PassaporteWeb::Helpers.convert_to_ostruct_recursive(result_hash) rescue *[RestClient::BadRequest] => e MultiJson.decode(e.response.body) end |
Instance Method Details
#attributes ⇒ Object
Returns a hash with all attribures of the Notification.
147 148 149 150 151 152 |
# File 'lib/passaporte_web/notification.rb', line 147 def attributes ATTRIBUTES.inject({}) do |hash, attribute| hash[attribute] = self.send(attribute) hash end end |
#destroy ⇒ Object
Excludes a newly created notification. Only notifications scheduled for the future can be deleted.
API method: DELETE /notifications/api/:uuid/
API documentation: app.passaporteweb.com.br/static/docs/notificacao.html#delete-notifications-api-uuid
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/passaporte_web/notification.rb', line 128 def destroy unless self.persisted? @errors = {message: 'notification not persisted yet'} return false end response = Http.delete("/notifications/api/#{self.uuid}/", {}, 'application') raise "unexpected response: #{response.code} - #{response.body}" unless response.code == 204 @errors = {} @persisted = false @destroyed = true true rescue *[RestClient::Forbidden] => e @persisted = true @destroyed = false @errors = MultiJson.decode(e.response.body) false end |
#destroyed? ⇒ Boolean
158 159 160 |
# File 'lib/passaporte_web/notification.rb', line 158 def destroyed? @destroyed == true end |
#persisted? ⇒ Boolean
154 155 156 |
# File 'lib/passaporte_web/notification.rb', line 154 def persisted? @persisted == true && !self.uuid.nil? end |
#read! ⇒ Object
Marks a notification as read.
API method: PUT /notifications/api/:uuid/
API documentation: app.passaporteweb.com.br/static/docs/notificacao.html#put-notifications-api-uuid
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/passaporte_web/notification.rb', line 106 def read! raise 'notification not persisted' unless self.persisted? unless self.read_at.nil? @errors = {message: 'notification already read'} return false end response = Http.put("/notifications/api/#{self.uuid}/", {}, {}, 'user') raise "unexpected response: #{response.code} - #{response.body}" unless response.code == 200 attributes_hash = MultiJson.decode(response.body) set_attributes(attributes_hash) @errors = {} true rescue *[RestClient::BadRequest] => e @errors = MultiJson.decode(e.response.body) false end |
#save(auth_type = 'user') ⇒ Object
Creates the notification on PassaporteWeb. The body
and destination
attributes are required. The auth_type
parameter defaults to ‘user’, meaning the notification is sent by the authenticated Identity. If auth_type
is ‘application’, than the notification is sent by the authenticated application.
API method: POST /notifications/api/
API documentation: app.passaporteweb.com.br/static/docs/notificacao.html#post-notifications-api
92 93 94 95 96 97 98 99 |
# File 'lib/passaporte_web/notification.rb', line 92 def save(auth_type='user') if self.persisted? @errors = {message: 'notification already persisted, call #read! to mark it as read'} false else create(auth_type) end end |