Class: Pushpad::Subscription

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

Defined Under Namespace

Classes: CountError, FindError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Subscription

Returns a new instance of Subscription.



11
12
13
14
15
16
17
18
19
20
# File 'lib/pushpad/subscription.rb', line 11

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.



9
10
11
# File 'lib/pushpad/subscription.rb', line 9

def auth
  @auth
end

#created_atObject (readonly)

Returns the value of attribute created_at.



9
10
11
# File 'lib/pushpad/subscription.rb', line 9

def created_at
  @created_at
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



9
10
11
# File 'lib/pushpad/subscription.rb', line 9

def endpoint
  @endpoint
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/pushpad/subscription.rb', line 9

def id
  @id
end

#last_click_atObject (readonly)

Returns the value of attribute last_click_at.



9
10
11
# File 'lib/pushpad/subscription.rb', line 9

def last_click_at
  @last_click_at
end

#p256dhObject (readonly)

Returns the value of attribute p256dh.



9
10
11
# File 'lib/pushpad/subscription.rb', line 9

def p256dh
  @p256dh
end

#tagsObject (readonly)

Returns the value of attribute tags.



9
10
11
# File 'lib/pushpad/subscription.rb', line 9

def tags
  @tags
end

#uidObject (readonly)

Returns the value of attribute uid.



9
10
11
# File 'lib/pushpad/subscription.rb', line 9

def uid
  @uid
end

Class Method Details

.count(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pushpad/subscription.rb', line 22

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

.find_all(options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pushpad/subscription.rb', line 36

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