module AutoRemote

Constants

VERSION

Public Class Methods

addDevice(name, input)

Add

Alias for: add_device
add_device(name, input) click to toggle source

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
Also aliased as: addDevice, saveDevice, save_device
deleteDevice(name)
Alias for: remove_device
delete_device(name)
Alias for: remove_device
getDevice(name)

Get

Alias for: get_device
get_device(name) click to toggle source

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
Also aliased as: getDevice
listDevices()

List

Alias for: list_devices
list_devices() click to toggle source

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
Also aliased as: listDevices
regOnDevice(device, remotehost)
Alias for: register_on_device
reg_on_device(device, remotehost)
Alias for: register_on_device
registerOnDevice(device, remotehost)

Register

Alias for: register_on_device
register_on_device(device, remotehost) click to toggle source

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
removeDevice(name)

Remove

Alias for: remove_device
remove_device(name) click to toggle source

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
saveDevice(name, input)
Alias for: add_device
save_device(name, input)
Alias for: add_device
sendMessage(device, message)

Message

Alias for: send_message
sendMsg(device, message)
Alias for: send_message
send_message(device, message) click to toggle source

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
Also aliased as: sendMessage, sendMsg, send_msg
send_msg(device, message)
Alias for: send_message

Private Class Methods

get_ip_address() click to toggle source

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
validate_device(input) click to toggle source

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