Class: Pushpad::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/pushpad/notification.rb

Defined Under Namespace

Classes: CancelError, DeliveryError, FindError, ReadonlyError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Notification

Returns a new instance of Notification.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pushpad/notification.rb', line 21

def initialize(options)
  @id = options[:id]
  @created_at = options[:created_at] && Time.parse(options[:created_at])
  @scheduled_count = options[:scheduled_count]
  @successfully_sent_count = options[:successfully_sent_count]
  @opened_count = options[:opened_count]

  @body = options[:body]
  @title = options[:title]
  @target_url = options[:target_url]
  @icon_url = options[:icon_url]
  @badge_url = options[:badge_url]
  @image_url = options[:image_url]
  @ttl = options[:ttl]
  @require_interaction = options[:require_interaction]
  @silent = options[:silent]
  @urgent = options[:urgent]
  @custom_data = options[:custom_data]
  @custom_metrics = options[:custom_metrics]
  @actions = options[:actions]
  @starred = options[:starred]
  @send_at = options[:send_at]
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def actions
  @actions
end

#badge_urlObject

Returns the value of attribute badge_url.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def badge_url
  @badge_url
end

#bodyObject

Returns the value of attribute body.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def body
  @body
end

#created_atObject (readonly)

Returns the value of attribute created_at.



19
20
21
# File 'lib/pushpad/notification.rb', line 19

def created_at
  @created_at
end

#custom_dataObject

Returns the value of attribute custom_data.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def custom_data
  @custom_data
end

#custom_metricsObject

Returns the value of attribute custom_metrics.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def custom_metrics
  @custom_metrics
end

#icon_urlObject

Returns the value of attribute icon_url.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def icon_url
  @icon_url
end

#idObject (readonly)

Returns the value of attribute id.



19
20
21
# File 'lib/pushpad/notification.rb', line 19

def id
  @id
end

#image_urlObject

Returns the value of attribute image_url.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def image_url
  @image_url
end

#opened_countObject (readonly)

Returns the value of attribute opened_count.



19
20
21
# File 'lib/pushpad/notification.rb', line 19

def opened_count
  @opened_count
end

#require_interactionObject

Returns the value of attribute require_interaction.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def require_interaction
  @require_interaction
end

#scheduled_countObject (readonly)

Returns the value of attribute scheduled_count.



19
20
21
# File 'lib/pushpad/notification.rb', line 19

def scheduled_count
  @scheduled_count
end

#send_atObject

Returns the value of attribute send_at.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def send_at
  @send_at
end

#silentObject

Returns the value of attribute silent.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def silent
  @silent
end

#starredObject

Returns the value of attribute starred.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def starred
  @starred
end

#successfully_sent_countObject (readonly)

Returns the value of attribute successfully_sent_count.



19
20
21
# File 'lib/pushpad/notification.rb', line 19

def successfully_sent_count
  @successfully_sent_count
end

#target_urlObject

Returns the value of attribute target_url.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def target_url
  @target_url
end

#titleObject

Returns the value of attribute title.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def title
  @title
end

#ttlObject

Returns the value of attribute ttl.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def ttl
  @ttl
end

#urgentObject

Returns the value of attribute urgent.



18
19
20
# File 'lib/pushpad/notification.rb', line 18

def urgent
  @urgent
end

Class Method Details

.find(id) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/pushpad/notification.rb', line 45

def self.find(id)
  response = Request.get("https://pushpad.xyz/api/v1/notifications/#{id}")

  unless response.code == "200"
    raise FindError, "Response #{response.code} #{response.message}: #{response.body}"
  end

  new(JSON.parse(response.body, symbolize_names: true)).readonly!
end

.find_all(options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pushpad/notification.rb', line 55

def self.find_all(options = {})
  project_id = options[:project_id] || Pushpad.project_id
  raise "You must set project_id" unless project_id

  query_parameters = {}
  query_parameters[:page] = options[:page] if options.key?(:page)

  response = Request.get("https://pushpad.xyz/api/v1/projects/#{project_id}/notifications",
                         query_parameters: query_parameters)

  unless response.code == "200"
    raise FindError, "Response #{response.code} #{response.message}: #{response.body}"
  end

  JSON.parse(response.body, symbolize_names: true).map do |attributes|
    new(attributes).readonly!
  end
end

Instance Method Details

#broadcast(options = {}) ⇒ Object



79
80
81
# File 'lib/pushpad/notification.rb', line 79

def broadcast(options = {})
  deliver req_body(nil, options[:tags]), options
end

#cancelObject



94
95
96
97
98
99
100
# File 'lib/pushpad/notification.rb', line 94

def cancel
  response = Request.delete("https://pushpad.xyz/api/v1/notifications/#{id}/cancel")

  unless response.code == "204"
    raise CancelError, "Response #{response.code} #{response.message}: #{response.body}"
  end
end

#deliver_to(users, options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/pushpad/notification.rb', line 83

def deliver_to(users, options = {})
  uids = if users.respond_to?(:ids)
    users.ids
  elsif users.respond_to?(:collect)
    users.collect {|u| u.respond_to?(:id) ? u.id : u }
  else
    [users.respond_to?(:id) ? users.id : users]
  end
  deliver req_body(uids, options[:tags]), options
end

#readonly!Object



74
75
76
77
# File 'lib/pushpad/notification.rb', line 74

def readonly!
  @readonly = true
  self
end