Class: Pushpad::Subscription

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

Defined Under Namespace

Classes: CountError, CreateError, DeleteError, FindError, UpdateError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Subscription

Returns a new instance of Subscription.



20
21
22
23
24
25
26
27
28
29
# File 'lib/pushpad/subscription.rb', line 20

def initialize(options)
  @id = options[:id]
  @endpoint = options[:endpoint]
  @p256dh = options[:p256dh]
  @auth = options[:auth]
  @uid = options[:uid]
  @tags = options[:tags]
  @last_click_at = options[:last_click_at] && Time.parse(options[:last_click_at])
  @created_at = options[:created_at] && Time.parse(options[:created_at])
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



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

def auth
  @auth
end

#created_atObject (readonly)

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#last_click_atObject (readonly)

Returns the value of attribute last_click_at.



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

def last_click_at
  @last_click_at
end

#p256dhObject (readonly)

Returns the value of attribute p256dh.



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

def p256dh
  @p256dh
end

#tagsObject (readonly)

Returns the value of attribute tags.



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

def tags
  @tags
end

#uidObject (readonly)

Returns the value of attribute uid.



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

def uid
  @uid
end

Class Method Details

.count(options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pushpad/subscription.rb', line 45

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

  endpoint = "https://pushpad.xyz/api/v1/projects/#{project_id}/subscriptions"
  response = Request.head(endpoint, query_parameters: query_parameters(options))

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

  response["X-Total-Count"].to_i
end

.create(attributes, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pushpad/subscription.rb', line 31

def self.create(attributes, options = {})
  project_id = options[:project_id] || Pushpad.project_id
  raise "You must set project_id" unless project_id
  
  endpoint = "https://pushpad.xyz/api/v1/projects/#{project_id}/subscriptions"
  response = Request.post(endpoint, attributes.to_json)

  unless response.code == "201"
    raise CreateError, "Response #{response.code} #{response.message}: #{response.body}"
  end
  
  new(JSON.parse(response.body, symbolize_names: true))
end

.find(id, options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pushpad/subscription.rb', line 59

def self.find(id, options = {})
  project_id = options[:project_id] || Pushpad.project_id
  raise "You must set project_id" unless project_id
  
  response = Request.get("https://pushpad.xyz/api/v1/projects/#{project_id}/subscriptions/#{id}")

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

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

.find_all(options = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pushpad/subscription.rb', line 72

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

  query_parameters_with_pagination = query_parameters(options)
  query_parameters_with_pagination << ["page", options[:page]] if options.key?(:page)

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

  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)
  end
end

Instance Method Details

#delete(options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pushpad/subscription.rb', line 111

def delete(options = {})
  project_id = options[:project_id] || Pushpad.project_id
  raise "You must set project_id" unless project_id
  
  raise "You must set id" unless id
  
  response = Request.delete("https://pushpad.xyz/api/v1/projects/#{project_id}/subscriptions/#{id}")

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

#update(attributes, options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pushpad/subscription.rb', line 91

def update(attributes, options = {})
  project_id = options[:project_id] || Pushpad.project_id
  raise "You must set project_id" unless project_id
  
  raise "You must set id" unless id
  
  endpoint = "https://pushpad.xyz/api/v1/projects/#{project_id}/subscriptions/#{id}"
  response = Request.patch(endpoint, attributes.to_json)

  unless response.code == "200"
    raise UpdateError, "Response #{response.code} #{response.message}: #{response.body}"
  end
  
  attributes = JSON.parse(response.body, symbolize_names: true)
  @uid = attributes[:uid]
  @tags = attributes[:tags]
  
  self
end