Class: Pushpad::Sender

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

Defined Under Namespace

Classes: CreateError, DeleteError, FindError, UpdateError

Constant Summary collapse

ATTRIBUTES =
:id, :name, :vapid_private_key, :vapid_public_key, :created_at

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Sender

Returns a new instance of Sender.



19
20
21
22
23
24
25
# File 'lib/pushpad/sender.rb', line 19

def initialize(options)
  @id = options[:id]
  @name = options[:name]
  @vapid_private_key = options[:vapid_private_key]
  @vapid_public_key = options[:vapid_public_key]
  @created_at = options[:created_at] && Time.parse(options[:created_at])
end

Class Method Details

.create(attributes) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/pushpad/sender.rb', line 27

def self.create(attributes)      
  endpoint = "https://pushpad.xyz/api/v1/senders"
  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) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/pushpad/sender.rb', line 38

def self.find(id)  
  response = Request.get("https://pushpad.xyz/api/v1/senders/#{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_allObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pushpad/sender.rb', line 48

def self.find_all
  response = Request.get("https://pushpad.xyz/api/v1/senders")
  
  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

#deleteObject



79
80
81
82
83
84
85
86
87
# File 'lib/pushpad/sender.rb', line 79

def delete      
  raise "You must set id" unless id
  
  response = Request.delete("https://pushpad.xyz/api/v1/senders/#{id}")
  
  unless response.code == "204"
    raise DeleteError, "Response #{response.code} #{response.message}: #{response.body}"
  end
end

#update(attributes) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pushpad/sender.rb', line 60

def update(attributes)      
  raise "You must set id" unless id
  
  endpoint = "https://pushpad.xyz/api/v1/senders/#{id}"
  response = Request.patch(endpoint, attributes.to_json)
  
  unless response.code == "200"
    raise UpdateError, "Response #{response.code} #{response.message}: #{response.body}"
  end
  
  updated = self.class.new(JSON.parse(response.body, symbolize_names: true))
  
  ATTRIBUTES.each do |attr|
    self.instance_variable_set("@#{attr}", updated.instance_variable_get("@#{attr}"))
  end
  
  self
end