Class: ImperiusGem::Device

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Device

Returns a new instance of Device.



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

def initialize(url)
  @url = url
end

Class Method Details

.http(method, url, params, body = "", headers = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/device.rb', line 45

def self.http(method,url,params,body="",headers={})
  headers.merge!({ :Accept => "application/json" })
  request = Typhoeus::Request.new( url,
    :method => method,
    :body => body,
    :headers => headers,
    :params => params #:verbose => Context.verbose
  )
  res = request.run
end

Instance Method Details

#cleanObject



30
31
32
33
# File 'lib/device.rb', line 30

def clean
  res = Device.http(:get,"#{@url}/clean",{})
  raise "Error #{res.response_code} (url was #{res.effective_url}).\n#{res.response_body}" if (res.response_code != 200)
end

#find(name) ⇒ Object



20
21
22
# File 'lib/device.rb', line 20

def find(name)
  ImperiusGem::Thing.new(self,name)
end

#run(on, method, args = []) ⇒ Object



13
14
15
16
17
18
# File 'lib/device.rb', line 13

def run(on,method,args=[])
  res = Device.http(:get,"#{@url}/execute",{:on => on, :method => method, :args => "#{[*args].to_json}"})
  raise "Error #{res.response_code} (url was #{res.effective_url}).\n#{res.response_body}" if (res.response_code != 200)
  res = JSON.parse("[#{res.response_body}]").first
  return (res.to_s.start_with?("hash:")) ? ImperiusGem::Thing.new(self,res) : res
end

#setPackages(packages = {}) ⇒ Object



40
41
42
43
# File 'lib/device.rb', line 40

def setPackages(packages={})
  res = Device.http(:post,"#{@url}/packages",{:packages => packages.to_json})
  raise "Error #{res.response_code} (url was #{res.effective_url}).\n#{res.response_body}" if (res.response_code != 200)
end

#terminateObject



35
36
37
38
# File 'lib/device.rb', line 35

def terminate
  res = Device.http(:get,"#{@url}/terminate",{})
  raise "Error #{res.response_code} (url was #{res.effective_url}).\n#{res.response_body}" if (res.response_code != 200)
end

#versionObject



24
25
26
27
28
# File 'lib/device.rb', line 24

def version
  res = Device.http(:get,"#{@url}/version",{})
  raise "Error #{res.response_code} (url was #{res.effective_url}).\n#{res.response_body}" if (res.response_code != 200)
  res.response_body
end