Class: RUPNP::CP::Base

Inherits:
Object
  • Object
show all
Includes:
EM::Deferrable, LogMixin, Tools
Defined in:
lib/rupnp/cp/base.rb

Overview

Base class for devices and services

Author:

  • Sylvain Daubert

Direct Known Subclasses

RemoteDevice, RemoteService

Constant Summary collapse

HTTP_COMMON_CONFIG =

Common HTTP headers for description requests

{
  :head => {
    :user_agent => USER_AGENT,
    :host => "#{HOST_IP}:#{DISCOVERY_PORT}",
  },
}

Constants included from LogMixin

LogMixin::LOG_LEVEL

Instance Method Summary collapse

Methods included from LogMixin

#log

Methods included from Tools

#build_url, #snake_case, #urn_are_equivalent?, #usn2udn

Constructor Details

#initializeBase

Returns a new instance of Base.



25
26
27
# File 'lib/rupnp/cp/base.rb', line 25

def initialize
  @parser = Nori.new(:convert_tags_to => ->(tag){ tag.snakecase.to_sym })
end

Instance Method Details

#get_description(location, getter) ⇒ void

This method returns an undefined value.

Get description from location

Parameters:

  • location (String)
  • getter (EM::Defferable)

    deferrable to advice about failure or success. On fail, getter receive a message. On success, it receive a description (XML Nori hash)



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rupnp/cp/base.rb', line 35

def get_description(location, getter)
  log :info, "getting description for #{location}"
  http = EM::HttpRequest.new(location).get(HTTP_COMMON_CONFIG)

  http.errback do |error|
    getter.set_deferred_status :failed, 'Cannot get description'
  end

  callback = Proc.new do
    description = @parser.parse(http.response)
    log :debug, 'Description received'
    getter.succeed description
  end

  http.headers do |h|
    unless h['SERVER'] =~ /UPnP\/1\.\d/
      log :error, "Not a supported UPnP response : #{h['SERVER']}"
      http.cancel_callback callback
    end
  end

  http.callback &callback
end

#inspectObject

Returns String.

Returns:

  • String



60
61
62
# File 'lib/rupnp/cp/base.rb', line 60

def inspect
  "#<#{self.class}:#{object_id} type=#{type.inspect}>"
end