Method: EasyUpnp::DeviceControlPoint.from_service_definition

Defined in:
lib/easy_upnp/control_point/device_control_point.rb

.from_service_definition(definition, options, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/easy_upnp/control_point/device_control_point.rb', line 102

def self.from_service_definition(definition, options, &block)
  urn = definition[:st]
  root_uri = definition[:location]

  xml = Nokogiri::XML(open(root_uri))
  xml.remove_namespaces!

  service = xml.xpath("//device/serviceList/service[serviceType=\"#{urn}\"]").first

  if service.nil?
    raise RuntimeError, "Couldn't find service with urn: #{urn}"
  else
    service = Nokogiri::XML(service.to_xml)
    service_definition_uri = URI.join(root_uri, service.xpath('service/SCPDURL').text).to_s
    service_definition = open(service_definition_uri) { |f| f.read }

    endpoint_url = ->(xpath) do
      URI.join(root_uri, service.xpath(xpath).text).to_s
    end

    DeviceControlPoint.new(
        urn,
        endpoint_url.call('service/controlURL'),
        endpoint_url.call('service/eventSubURL'),
        service_definition,
        options,
        &block
    )
  end
end