class Airplay::Connection

Public: The class that handles all the outgoing basic HTTP connections

Public: The class that handles all the outgoing basic HTTP connections

Constants

Authentication
PasswordRequired
Response
WrongPassword

Public Class Methods

new(device, options = {}) click to toggle source
# File lib/airplay/connection.rb, line 15
def initialize(device, options = {})
  @device = device
  @options = options
  @logger = Airplay::Logger.new("airplay::connection")
end

Public Instance Methods

close() click to toggle source

Public: Closes the opened connection

Returns nothing

# File lib/airplay/connection.rb, line 34
def close
  persistent.close
  @_persistent = nil
end
get(resource, headers = {}) click to toggle source

Public: Executes a GET to a resource

resource - The resource on the currently active Device headers - Optional headers

Returns a response object

# File lib/airplay/connection.rb, line 70
def get(resource, headers = {})
  prepare_request(:get, resource, nil, headers)
end
persistent() click to toggle source

Public: Establishes a persistent connection to the device

Returns the persistent connection

# File lib/airplay/connection.rb, line 25
def persistent
  address = @options[:address] || "http://#{@device.address}"
  @_persistent ||= Airplay::Connection::Persistent.new(address, @options)
end
post(resource, body = "", headers = {}) click to toggle source

Public: Executes a POST to a resource

resource - The resource on the currently active Device body - The body of the action headers - Optional headers

Returns a response object

# File lib/airplay/connection.rb, line 47
def post(resource, body = "", headers = {})
  prepare_request(:post, resource, body, headers)
end
put(resource, body = "", headers = {}) click to toggle source

Public: Executes a PUT to a resource

resource - The resource on the currently active Device body - The body of the action headers - Optional headers

Returns a response object

# File lib/airplay/connection.rb, line 59
def put(resource, body = "", headers = {})
  prepare_request(:put, resource, body, headers)
end

Private Instance Methods

default_headers() click to toggle source

Private: The defaults connection headers

Returns the default headers

# File lib/airplay/connection.rb, line 103
def default_headers
  {
    "User-Agent"         => "MediaControl/1.0",
    "X-Apple-Session-ID" => persistent.session,
    "X-Apple-Device-ID"  => persistent.mac_address
  }
end
prepare_request(verb, resource, body, headers) click to toggle source

Private: Prepares HTTP requests for :get, :post and :put

verb - The http method/verb to use for the request resource - The resource on the currently active Device body - The body of the action headers - The headers of the request

Returns a response object

# File lib/airplay/connection.rb, line 85
def prepare_request(verb, resource, body, headers)
  msg = "#{verb.upcase} #{resource}"

  request = Net::HTTP.const_get(verb.capitalize).new(resource)

  unless verb.eql?(:get)
    request.body = body
    msg.concat(" with #{body.bytesize} bytes")
  end

  @logger.info(msg)
  send_request(request, headers)
end
send_request(request, headers) click to toggle source

Private: Sends a request to the Device

request - The Request object headers - The headers of the request

Returns a response object

# File lib/airplay/connection.rb, line 118
def send_request(request, headers)
  request.initialize_http_header(default_headers.merge(headers))

  if @device.password?
    authentication = Airplay::Connection::Authentication.new(@device, persistent)
    request = authentication.sign(request)
  end

  @logger.info("Sending request to #{@device.address}")
  response = persistent.request(request)

  verify_response(Airplay::Connection::Response.new(persistent, response))
end
verify_response(response) click to toggle source

Private: Verifies response

response - The Response object

Returns a response object or exception

# File lib/airplay/connection.rb, line 138
def verify_response(response)
  if response.response.status == 401
    return PasswordRequired.new if !@device.password?
    return WrongPassword.new    if @device.password?
  end

  response
end