Module: AutoRemote

Defined in:
lib/autoremote.rb,
lib/autoremote/version.rb,
lib/autoremote/exceptions.rb

Defined Under Namespace

Classes: AutoRemoteException, DeviceAlreadyExist, DeviceNotFound, InvalidKey

Constant Summary collapse

REGURL =
"http://autoremotejoaomgcd.appspot.com/registerpc?key=%YOUR_KEY%&name=%DISPLAY_NAME%&id=%UNIQUE_ID%&type=linux&publicip=%PUBLIC_HOST%&localip=%IP_ADDRESS%"
MSGURL =
"http://autoremotejoaomgcd.appspot.com/sendmessage?key=%YOUR_KEY%&message=%MESSAGE%&sender=%SENDER_ID%"
VALIDATIONURL =
"http://autoremotejoaomgcd.appspot.com/sendmessage?key=%YOUR_KEY%"
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.addDevice(name, input) ⇒ void Also known as: saveDevice

This method returns an undefined value.

Add a device

Parameters:

  • name (String)

    The name of the device

  • input (String)

    Can either be the ‘goo.gl’ url or the personal key of the device

Raises:



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
63
64
65
66
67
68
69
# File 'lib/autoremote.rb', line 38

def AutoRemote::addDevice( name, input )
    
    ## Validation if input is a 'goo.gl' url
    if input.match( /^(https?:\/{2})?(goo.gl\/[\S]*)$/i )
        result = self.urlRequest( 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 = self.urlRequest( VALIDATIONURL.sub( /%YOUR_KEY%/, input ) )
        
        ## Check result
        if result.body != "OK"
            raise self::InvalidKey
        end
    end
    
    ## Check if the device already exist
    if Device.find_by_name( name ) || Device.find_by_key( input )
        raise self::DeviceAlreadyExist
    end
    
    ## Save the device
    Device.create(:name => name, :key => input)
end

.getDevice(name) ⇒ Device

Returns one specific device

Returns:



93
94
95
# File 'lib/autoremote.rb', line 93

def AutoRemote::getDevice( name )
    return Device.find_by_name( name )
end

.listDevicesDevice::ActiveRecord_Relation

Returns a list with all devices

Returns:

  • (Device::ActiveRecord_Relation)


87
88
89
# File 'lib/autoremote.rb', line 87

def AutoRemote::listDevices
    return Device.order("name").all
end

.registerOnDevice(device, remotehost) ⇒ void Also known as: regOnDevice

This method returns an undefined value.

Register on the device

Parameters:

  • device (Device, String)

    A device object or the name of the device

  • remotehost (String)

    The public hostname or ip-address

Raises:

  • (AutoRemote::DeviceNotFound)

    if the device didn’t exits

  • (AutoRemote::UnsupportedAction)

    if running from windows

  • (TypeError)

    if message isn’t a string or less than 5 characters



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/autoremote.rb', line 128

def AutoRemote::registerOnDevice( device, remotehost )
    if ! device.kind_of?( Device ) && ! ( device = Device.find_by_name( device ) )
        raise self::DeviceNotFound
    elsif ! remotehost.kind_of?( String ) || remotehost.length < 5
        raise ArgumentError, "remotehost must be a string of 5 chars or more"
    end
    
    hostname = `hostname`.strip
    ipAddress = AutoRemote::getIpAddress.ip_address
    
    ## Perform the registration
    result = self.urlRequest( REGURL.sub( /%YOUR_KEY%/, device.key ).sub(/%DISPLAY_NAME%/, hostname ).sub(/%UNIQUE_ID%/, hostname ).sub(/%PUBLIC_HOST%/, remotehost ).sub(/%IP_ADDRESS%/, ipAddress ) )
    
    ## Check result
    if result.body != "OK"
        raise self::AutoRemoteException, "Something went wrong when registering on the device"
    end
end

.removeDevice(name) ⇒ void Also known as: deleteDevice

This method returns an undefined value.

Remove a specific device

Parameters:

  • name (String)

    The name of the device

Raises:



75
76
77
78
79
80
81
82
83
# File 'lib/autoremote.rb', line 75

def AutoRemote::removeDevice( name )
    if device = Device.find_by_name(name)
        
        ## Remove the device
        Device.delete(device.id)
    else
        raise self::DeviceNotFound
    end
end

.sendMessage(device, message) ⇒ void Also known as: sendMsg

This method returns an undefined value.

Sends a message to a device

Parameters:

  • device (Device, String)

    A device object or the name of the device

  • message (String)

    The message to send

Raises:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/autoremote.rb', line 103

def AutoRemote::sendMessage( device, message )
    if ! device.kind_of?( Device ) && ! ( device = Device.find_by_name( device ) )
        raise self::DeviceNotFound
    elsif ! message.kind_of?( String )
        raise TypeError, "Message must be a string"
    end
    
    hostname = `hostname`.strip
    
    ## Send the message
    result = self.urlRequest( MSGURL.sub( /%YOUR_KEY%/, device.key ).sub( /%MESSAGE%/, message ).sub( /%SENDER_ID%/, hostname ) )
    
    ## Check result
    if result.body != "OK"
        raise self::InvalidKey
    end
end