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

Class Method Details

.add_device(name, input) ⇒ Device? Also known as: addDevice, saveDevice, save_device

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

Returns:

  • (Device)

    the device that was was created

  • (nil)

    if the device already exists

Raises:



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

Returns:

  • (Device)

    if the device was found

  • (nil)

    if the device wasn’t found



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

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

.list_devicesDevice::ActiveRecord_Relation Also known as: listDevices

Returns a list with all devices

Returns:

  • (Device::ActiveRecord_Relation)


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

Parameters:

  • device (Device, String)

    A device object or the name of the device

  • remotehost (String)

    The public hostname or ip-address

Returns:

  • (true)

    if the registration was successful

  • (false)

    if the registration failed

Raises:

  • (ArgumentError)

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



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

Parameters:

  • name (String)

    The name of the device

Returns:

  • (true)

    if the device was deleted

  • (false)

    if the device wasn’t found



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

Parameters:

  • device (Device, String)

    A device object or the name of the device

  • message (String)

    The message to send

Returns:

  • (true)

    if the message was sent

  • (false)

    if the message wasn’t sent

Raises:

  • (ArgumentError)

    if message isn’t a string



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::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