Class: Pushpad::Project

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

Defined Under Namespace

Classes: CreateError, DeleteError, FindError, UpdateError

Constant Summary collapse

ATTRIBUTES =
:id, :sender_id, :name, :website, :icon_url, :badge_url, :notifications_ttl, :notifications_require_interaction, :notifications_silent, :created_at

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Project

Returns a new instance of Project.



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

def initialize(options)
  @id = options[:id]
  @sender_id = options[:sender_id]
  @name = options[:name]
  @website = options[:website]
  @icon_url = options[:icon_url]
  @badge_url = options[:badge_url]
  @notifications_ttl = options[:notifications_ttl]
  @notifications_require_interaction = options[:notifications_require_interaction] 
  @notifications_silent = options[:notifications_silent]
  @created_at = options[:created_at] && Time.parse(options[:created_at])
end

Class Method Details

.create(attributes) ⇒ Object



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

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



43
44
45
46
47
48
49
50
51
# File 'lib/pushpad/project.rb', line 43

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



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pushpad/project.rb', line 53

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



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

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

#update(attributes) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pushpad/project.rb', line 65

def update(attributes)      
  raise "You must set id" unless id
  
  endpoint = "https://pushpad.xyz/api/v1/projects/#{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