Class: Guardian::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/guardian/notifier.rb

Class Method Summary collapse

Class Method Details

.notify(system_id, attributes) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/guardian/notifier.rb', line 8

def self.notify system_id, attributes
  assert_valid_keys! attributes, :online, :description, :data

  unless attributes.has_key? :online
    raise ArgumentError, ":online missing! Must specify :online as a boolean."
  end

  if !attributes.has_key?(:description) && attributes[:online] == false
    raise ArgumentError, ":description missing! Must specify description when offline"
  end

  uri = URI.parse "#{Guardian::API_URL}/systems/#{system_id}/updates"
  params = {:update => attributes, :account_id => Guardian.}

  request = Net::HTTP::Post.new(uri.path)
  request.content_type = 'application/json'
  request.body = params.to_json

  response = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(request) }

  if response.is_a?(Net::HTTPUnauthorized)
    raise RuntimeError, "Request unauthorized! You must specify correct account and system tokens"
  end

  unless response.is_a?(Net::HTTPCreated)
    raise RuntimeError, "Guardian did something unexpected!\n #{response.body}"
  end

  true
end

.offline(system_id, description, data = nil) ⇒ Object



43
44
45
# File 'lib/guardian/notifier.rb', line 43

def self.offline(system_id, description, data = nil)
  notify system_id, :online => false, :description => description, :data => data
end

.online(system_id, description = nil, data = nil) ⇒ Object



39
40
41
# File 'lib/guardian/notifier.rb', line 39

def self.online(system_id, description = nil, data = nil)
  notify system_id, :online => true, :description => description, :data => data
end