Class: Druzy::Upnp::UpnpService

Inherits:
Object
  • Object
show all
Defined in:
lib/druzy/upnp/upnp_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ UpnpService

Returns a new instance of UpnpService.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/druzy/upnp/upnp_service.rb', line 11

def initialize(args)
            
  @service_type = args[:service_type]
  @service_id = args[:service_id]
  @location = args[:location]
  @control_url = args[:control_url]
  @event_sub_url = args[:event_sub_url]
  
  uri = URI(@location)
  xml=Net::HTTP.get(uri)
  xml_nok = Nokogiri::XML(xml)
  xml_nok.remove_namespaces!
  xml_nok.xpath('scpd/actionList/action').to_a.each do |el|
    
    action_name = el.xpath('name').text
    define_singleton_method(action_name.to_sym) do |arguments={}|
      message = <<-MESSAGE
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
  <u:#{action_name} xmlns:u="#{@service_type}">
      MESSAGE
      
      arguments.each do |cle, valeur|
        message.concat <<-MESSAGE
    <#{cle.to_s}>#{valeur.to_s}</#{cle.to_s}>
        MESSAGE
      end
      
      message.concat <<-MESSAGE
  </u:#{action_name}>
</s:Body>
</s:Envelope>            
      MESSAGE
      
      header = {
        "HOST" => uri.host.to_s+':'+uri.port.to_s,
        "CONTENT-LENGTH" => message.size.to_s,
        "CONTENT-TYPE" => 'text/xml; charset="utf-8"',
        "SOAPACTION" => @service_type.to_s+"#"+action_name.to_s
      }            
      
      http = Net::HTTP.new(uri.host,uri.port)
      request = Net::HTTP::Post.new(uri.request_uri,header)
      request.body = message
      response = http.request(request)
      xml = Nokogiri.XML(response.body)
      xml.remove_namespaces!

      result = {}
      xml.xpath("Envelope/Body/"+action_name.to_s+"Response").children.each do |child|
        result[child.name.to_s] = child.text
      end
      
      return result
    end

  end
  
end

Instance Attribute Details

#control_urlObject (readonly)

Returns the value of attribute control_url.



9
10
11
# File 'lib/druzy/upnp/upnp_service.rb', line 9

def control_url
  @control_url
end

#event_sub_urlObject (readonly)

Returns the value of attribute event_sub_url.



9
10
11
# File 'lib/druzy/upnp/upnp_service.rb', line 9

def event_sub_url
  @event_sub_url
end

#locationObject (readonly)

Returns the value of attribute location.



9
10
11
# File 'lib/druzy/upnp/upnp_service.rb', line 9

def location
  @location
end

#service_idObject (readonly)

Returns the value of attribute service_id.



9
10
11
# File 'lib/druzy/upnp/upnp_service.rb', line 9

def service_id
  @service_id
end

#service_typeObject (readonly)

Returns the value of attribute service_type.



9
10
11
# File 'lib/druzy/upnp/upnp_service.rb', line 9

def service_type
  @service_type
end