Module: RUPNP::Tools

Included in:
CP::Base, ControlPoint
Defined in:
lib/rupnp/tools.rb

Overview

Helper module

Author:

  • Sylvain Daubert

Instance Method Summary collapse

Instance Method Details

#build_url(base, rest) ⇒ String

Build an url from a base and a relative url

Parameters:

  • base (String)
  • rest (String)

Returns:

  • (String)


11
12
13
14
# File 'lib/rupnp/tools.rb', line 11

def build_url(base, rest)
  url = base + (base.end_with?('/') ? '' : '/')
  url + (rest.start_with?('/') ? rest[1..-1] : rest)
end

#snake_case(str) ⇒ String

Convert a camel cased string to a snake cased one

snake_case("iconList")     # => "icon_list"
snake_case("eventSubURL")  # => "event_sub_url"

Parameters:

  • str (String)

Returns:

  • (String)


21
22
23
24
# File 'lib/rupnp/tools.rb', line 21

def snake_case(str)
  g = str.gsub(/([^A-Z_])([A-Z])/,'\1_\2')
  g.downcase || str.downcase
end

#urn_are_equivalent?(urn1, urn2) ⇒ Boolean

Check if two URN are equivalent. They are equivalent if they have the same name and the same major version.

Parameters:

  • urn1 (String)
  • urn2 (String)

Returns:

  • (Boolean)


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
# File 'lib/rupnp/tools.rb', line 31

def urn_are_equivalent?(urn1, urn2)
  u1 = urn1
  if urn1[0..3] == 'urn:'
    u1 = urn1[4..-1]
  end
  u2 = urn2
  if urn2[0..3] == 'urn:'
    u2 = urn2[4..-1]
  end

  m1 = u1.match(/(\w+):(\w+):(\w+):([\d-]+)/)
  m2 = u2.match(/(\w+):(\w+):(\w+):([\d-]+)/)
  if m1[1] == m2[1]
    if m1[2] == m2[2]
      if m1[3] == m2[3]
        v1_major = m1[4].split(/-/).first
        v2_major = m2[4].split(/-/).first
        if v1_major == v2_major
          return true
        end
      end
    end
  end
  false
end

#usn2udn(usn) ⇒ String

Retrieve UDN from USN

Parameters:

  • usn (String)

Returns:

  • (String)


60
61
62
63
# File 'lib/rupnp/tools.rb', line 60

def usn2udn(usn)
  rex = '([A-Fa-f0-9]{8,8}(-[A-Fa-f0-9]{4,4}){3,3}-[A-Fa-f0-9]{12,12})'
  usn.match(/#{rex}/)[1]
end