module AutoRemote
Constants
- VERSION
Public Class Methods
Add a device @param name [String] The name of the device @param input [String] Can either be the ‘goo.gl’ url or the personal key of the device @raise [AutoRemote::InvalidKey] if the key or url is invalid @return [Device] the device that was was created @return [nil] if the device already exists
# 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
Returns one specific device @return [Device] if the device was found @return [nil] if the device wasn’t found
# File lib/autoremote.rb, line 87 def AutoRemote::get_device(name) return Device.find_by_name(name) end
Returns a list with all devices @return [Device::ActiveRecord_Relation]
# File lib/autoremote.rb, line 80 def AutoRemote::list_devices return Device.order('name').all end
Register on the device @param device [Device, String] A device object or the name of the device @param remotehost [String] The public hostname or ip-address @raise [ArgumentError] if message isn’t a string or less than 5 characters @return [true] if the registration was successful @return [false] if the registration failed
# 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 a specific device @param name [String] The name of the device @return [true] if the device was deleted @return [false] if the device wasn’t found
# 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
Sends a message to a device @param device [Device, String] A device object or the name of the device @param message [String] The message to send @raise [ArgumentError] if message isn’t a string @return [true] if the message was sent @return [false] if the message wasn’t sent
# File lib/autoremote.rb, line 97 def AutoRemote::send_message(device, message) device = self.validate_device(device) if !device return false elsif ! message.is_a?(String) raise ArgumentError, 'Message must be a string' end ## Send the message result = AutoRemoteRequest.message(device.key, `hostname`.strip, CGI.escape(message)) ## Check result if result.body == 'OK' return true else return false end end
Private Class Methods
Gets the ip address of the system @return [String]
# File lib/autoremote.rb, line 190 def AutoRemote::get_ip_address return Socket.ip_address_list.detect { |ipInfo| ipInfo.ipv4_private? } end
Validates device @param input the input to validate @return [Device] if the input is valid @return [nil] if the input is not valid
# File lib/autoremote.rb, line 175 def AutoRemote::validate_device(input) if input.is_a?(Device) return input else device = Device.find_by_name(input) if device.kind_of?(Device) return device else return nil end end end