Class: Togglehq::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/togglehq/device.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Device

Returns a new instance of Device.



5
6
7
8
9
10
11
# File 'lib/togglehq/device.rb', line 5

def initialize(params = {})
  @os = params[:os]
  @uuid = params[:uuid]
  @os_version = params[:os_version]
  @manufacturer = params[:manufacturer]
  @model = params[:model]
end

Instance Attribute Details

#manufacturerObject

Returns the value of attribute manufacturer.



3
4
5
# File 'lib/togglehq/device.rb', line 3

def manufacturer
  @manufacturer
end

#modelObject

Returns the value of attribute model.



3
4
5
# File 'lib/togglehq/device.rb', line 3

def model
  @model
end

#osObject

Returns the value of attribute os.



3
4
5
# File 'lib/togglehq/device.rb', line 3

def os
  @os
end

#os_versionObject

Returns the value of attribute os_version.



3
4
5
# File 'lib/togglehq/device.rb', line 3

def os_version
  @os_version
end

#uuidObject

Returns the value of attribute uuid.



3
4
5
# File 'lib/togglehq/device.rb', line 3

def uuid
  @uuid
end

Instance Method Details

#assign!(user_identifier) ⇒ Object

Assigns a device to a specific user

Parameters:

  • user_identifier (String)

    the identifier of the user

Raises:

  • (RuntimeError)

    raised if either the device or user is invalid or not found



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/togglehq/device.rb', line 31

def assign!(user_identifier)
  response = Togglehq::Request.new("/devices/assign",{"device" => {"uuid" => self.uuid}, "user" => {"identifier" => user_identifier}}).patch!
  if response.status == 200
    return true
  elsif response.status == 404
    json = JSON.parse(response.body)
    raise json["message"]
  else
    raise "unexpected error assigning the device"
  end
end

#enable!(token) ⇒ Object

Enables a device to receive notifications

Parameters:

  • token (String)

    the token of the device

Raises:

  • (RuntimeError)

    raised if the device is invalid or not found



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/togglehq/device.rb', line 16

def enable!(token)
  response = Togglehq::Request.new("/devices/enable",{"device" => {"uuid" => self.uuid, "token" => token}}).patch!
  if response.status == 200
    return true
  elsif response.status == 404
    json = JSON.parse(response.body)
    raise json["message"]
  else
    raise "unexpected error enabling the device"
  end
end

#persisted!Object



71
72
73
# File 'lib/togglehq/device.rb', line 71

def persisted!
  @persisted = true
end

#saveObject

Saves a new device



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/togglehq/device.rb', line 58

def save
  response = Togglehq::Request.new("/devices", {"device" => {"os" => self.os, "os_version" => self.os_version, "manufacturer" => self.manufacturer, "model" => self.model}}).post!
  if response.status == 200
    self.persisted!
    json = JSON.parse(response.body)
    self.uuid = json["device"]["uuid"]
    return true
  else
    raise "unexpected error saving the device"
  end
end

#unassign!Object

Unassigns a device from all users

Raises:

  • (RuntimeError)

    raised if either the device or user is invalid or not found



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/togglehq/device.rb', line 45

def unassign!
  response = Togglehq::Request.new("/devices/unassign",{"device" => {"uuid" => self.uuid}}).patch!
  if response.status == 200
    return true
  elsif response.status == 404
    json = JSON.parse(response.body)
    raise json["message"]
  else
    raise "unexpected error unassigning the device"
  end
end