Module: AutoRemote
- Defined in:
- lib/autoremote.rb,
lib/autoremote/version.rb,
lib/autoremote/exceptions.rb
Defined Under Namespace
Classes: AutoRemoteException, InvalidKey
Constant Summary collapse
- VERSION =
'0.3.1'
Class Method Summary collapse
-
.add_device(name, input) ⇒ Device?
(also: addDevice, saveDevice, save_device)
Add a device.
-
.get_device(name) ⇒ Device?
(also: getDevice)
Returns one specific device.
-
.list_devices ⇒ Device::ActiveRecord_Relation
(also: listDevices)
Returns a list with all devices.
-
.register_on_device(device, remotehost) ⇒ true, false
(also: registerOnDevice, regOnDevice, reg_on_device)
Register on the device.
-
.remove_device(name) ⇒ true, false
(also: removeDevice, deleteDevice, delete_device)
Remove a specific device.
-
.send_message(device, message) ⇒ true, false
(also: sendMessage, sendMsg, send_msg)
Sends a message to a device.
Class Method Details
.add_device(name, input) ⇒ Device? Also known as: addDevice, saveDevice, save_device
Add a device
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/autoremote.rb', line 33 def AutoRemote::add_device(name, input) ## Validation if input is a 'goo.gl' url if input.match(/^(https?:\/{2})?(goo.gl\/[\S]*)$/i) result = AutoRemoteRequest.validate_url(input) ## Get the key from the resulting url begin input = CGI.parse(result.request.last_uri.query)['key'][0] rescue raise self::InvalidKey end ## If not a 'goo.gl' url, check if it is a valid key else ## Validate key result = AutoRemoteRequest.validate_key(input) ## Check result raise self::InvalidKey if result.body != 'OK' end ## Check if the device already exist if Device.find_by_name(name) return nil else ## Save the device return Device.create(:name => name, :key => input) end end |
.get_device(name) ⇒ Device? Also known as: getDevice
Returns one specific device
87 88 89 |
# File 'lib/autoremote.rb', line 87 def AutoRemote::get_device(name) return Device.find_by_name(name) end |
.list_devices ⇒ Device::ActiveRecord_Relation Also known as: listDevices
Returns a list with all devices
80 81 82 |
# File 'lib/autoremote.rb', line 80 def AutoRemote::list_devices return Device.order('name').all end |
.register_on_device(device, remotehost) ⇒ true, false Also known as: registerOnDevice, regOnDevice, reg_on_device
Register on the device
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/autoremote.rb', line 123 def AutoRemote::register_on_device(device, remotehost) device = self.validate_device(device) if !device return false elsif ! remotehost.is_a?(String) || remotehost.length < 5 raise ArgumentError, 'remotehost must be a string of 5 chars or more' end hostname = `hostname`.strip ipAddress = AutoRemote::get_ip_address.ip_address ## Perform the registration result = AutoRemoteRequest.register(device.key, hostname, hostname, remotehost, ipAddress) ## Check result if result.body == 'OK' return true else return false end end |
.remove_device(name) ⇒ true, false Also known as: removeDevice, deleteDevice, delete_device
Remove a specific device
68 69 70 71 72 73 74 75 76 |
# File 'lib/autoremote.rb', line 68 def AutoRemote::remove_device(name) if device = Device.find_by_name(name) ## Remove the device Device.delete(device.id) return true else return false end end |
.send_message(device, message) ⇒ true, false Also known as: sendMessage, sendMsg, send_msg
Sends a message to a device
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/autoremote.rb', line 97 def AutoRemote::(device, ) device = self.validate_device(device) if !device return false elsif ! .is_a?(String) raise ArgumentError, 'Message must be a string' end ## Send the message result = AutoRemoteRequest.(device.key, `hostname`.strip, CGI.escape()) ## Check result if result.body == 'OK' return true else return false end end |