class Togglehq::Device

Attributes

manufacturer[RW]
model[RW]
os[RW]
os_version[RW]
uuid[RW]

Public Class Methods

new(params = {}) click to toggle source
# 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

Public Instance Methods

assign!(user_identifier) click to toggle source

Assigns a device to a specific user @param user_identifier [String] the identifier of the user @raise [RuntimeError] raised if either the device or user is invalid or not found

# 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) click to toggle source

Enables a device to receive notifications @param token [String] the token of the device @raise [RuntimeError] raised if the device is invalid or not found

# 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!() click to toggle source

@private

# File lib/togglehq/device.rb, line 71
def persisted!
  @persisted = true
end
save() click to toggle source

Saves a new device

# 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!() click to toggle source

Unassigns a device from all users @raise [RuntimeError] raised if either the device or user is invalid or not found

# 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