class Airplay::Device

Public: Represents an Airplay Node

Public: Represents an Airplay Node

Public: Represents an Airplay Device

Constants

MissingAttributes

Attributes

address[R]
name[R]
password[R]
type[R]

Public Class Methods

new(attributes = {}) click to toggle source
# File lib/airplay/device.rb, line 18
def initialize(attributes = {})
  validate_attributes(attributes)

  @name     = attributes[:name]
  @address  = attributes[:address]
  @type     = attributes[:type]
  @password = attributes[:password]

  @it_has_password = false

  Airplay.configuration.load
end

Public Instance Methods

address=(address) click to toggle source

Public: Set the addess of the device

address - The address string of the device

Returns nothing

# File lib/airplay/device.rb, line 77
def address=(address)
  @address = address
end
connection() click to toggle source

Public: Establishes a conection to the device

Returns the Connection

# File lib/airplay/device.rb, line 109
def connection
  @_connection ||= Airplay::Connection.new(self)
end
features() click to toggle source

Public: Access the Features of the device

Returns the Featurs of the device

# File lib/airplay/device.rb, line 85
def features
  @_features ||= Features.new(self)
end
id() click to toggle source

Public: The unique id of the device (mac address)

Returns the mac address based on basic_info or server_info

# File lib/airplay/device.rb, line 125
def id
  @_id ||= begin
    basic_info.fetch("macAddress", server_info["macAddress"])
  end
end
info() click to toggle source

Public: Access the Info of the device

Returns the Info of the device

# File lib/airplay/device.rb, line 93
def info
  @_info ||= Info.new(self)
end
ip() click to toggle source

Public: Access the ip of the device

Returns the memoized ip address

# File lib/airplay/device.rb, line 35
def ip
  @_ip ||= address.split(":").first
end
password=(passwd) click to toggle source

Public: Sets the password for the device

passwd - The password string

Returns nothing

# File lib/airplay/device.rb, line 58
def password=(passwd)
  @password = passwd
end
password?() click to toggle source

Public: Checks if the devices has a password

Returns boolean for the presence of a password

# File lib/airplay/device.rb, line 66
def password?
  return @it_has_password if @it_has_password
  !!password && !password.empty?
end
refresh_connection() click to toggle source

Public: Forces the refresh of the connection

Returns nothing

# File lib/airplay/device.rb, line 117
def refresh_connection
  @_connection = nil
end
server_info() click to toggle source

Public: Access the full information of the device

Returns a hash with all the information

# File lib/airplay/device.rb, line 101
def server_info
  @_server_info ||= basic_info.merge(extra_info)
end
text_records=(record) click to toggle source

Public: Sets server information based on text records

Returns text records hash.

# File lib/airplay/device.rb, line 43
def text_records=(record)
  @text_records = {
    "model"      => record["model"],
    "features"   => record["features"],
    "macAddress" => record["deviceid"],
    "srcvers"    => record["srcvers"]
  }
end

Private Instance Methods

basic_info() click to toggle source

Private: Access the basic info of the device

Returns a hash with the basic information

# File lib/airplay/device.rb, line 141
def basic_info
  @_basic_info ||= begin
    return @text_records if @text_records

    response = connection.get("/server-info").response
    plist = CFPropertyList::List.new(data: response.body)
    CFPropertyList.native_types(plist.value)
  end
end
extra_info() click to toggle source

Private: Access extra info of the device

Returns a hash with extra information

# File lib/airplay/device.rb, line 155
def extra_info
  @_extra_info ||=
    begin
      new_device = clone
      new_device.refresh_connection
      new_device.address = "#{ip}:7100"

      result = new_device.connection.get("/stream.xml")
      raise result if !result.is_a?(Airplay::Connection::Response)

      response = result.response
      return {} if response.status != 200

      plist = CFPropertyList::List.new(data: response.body)
      CFPropertyList.native_types(plist.value)
    rescue Airplay::Connection::PasswordRequired
      it_has_password!

      return {}
    end
end
it_has_password!() click to toggle source
# File lib/airplay/device.rb, line 133
def it_has_password!
  @it_has_password = true
end
validate_attributes(attributes) click to toggle source

Private: Validates the mandatory attributes for a device

attributes - The attributes hash to be validated

Returns nothing or raises a MissingAttributes if some key is missing

# File lib/airplay/device.rb, line 183
def validate_attributes(attributes)
  if !([:name, :address] - attributes.keys).empty?
    raise MissingAttributes.new("A :name and an :address are mandatory")
  end
end