Class: SRT::StreamIDComponents

Inherits:
Object
  • Object
show all
Defined in:
lib/rbsrt/streamid_components.rb

Constant Summary collapse

Types =
["stream", "file", "auth"]
Modes =
["request", "publish", "bidirectional"]
Header =
"#!::"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ StreamIDComponents

Returns a new instance of StreamIDComponents.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rbsrt/streamid_components.rb', line 21

def initialize(*args)

  @mode = :request
  @type = :stream
  @extra = {}

  # no args
  return unless args.first

  # hash args
  if args.first.class == ::Hash
    merge_hash!(args.first)
  elsif streamid = args.first.to_s
    if streamid.index(Header) == 0
      merge_str!(streamid)
    elsif streamid =~ URI::regexp
      merge_uri!(URI(streamid))
    else
      @resource_name = streamid
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/rbsrt/streamid_components.rb', line 78

def method_missing(name, *args)
  return extra[name.to_s] if extra.key? name.to_s
  match = name.to_s.match(/^(.*?)(=?)$/)
  case match[2]
  when "="
    @extra[match[1]] = args.first
  else
    return nil
  end
end

Instance Attribute Details

#extraObject (readonly)

Returns the value of attribute extra.



19
20
21
# File 'lib/rbsrt/streamid_components.rb', line 19

def extra
  @extra
end

#host_nameObject

Returns the value of attribute host_name.



16
17
18
# File 'lib/rbsrt/streamid_components.rb', line 16

def host_name
  @host_name
end

#modeObject

Returns the value of attribute mode.



15
16
17
# File 'lib/rbsrt/streamid_components.rb', line 15

def mode
  @mode
end

#resource_nameObject

Returns the value of attribute resource_name.



13
14
15
# File 'lib/rbsrt/streamid_components.rb', line 13

def resource_name
  @resource_name
end

#sessionidObject

Returns the value of attribute sessionid.



18
19
20
# File 'lib/rbsrt/streamid_components.rb', line 18

def sessionid
  @sessionid
end

#typeObject

Returns the value of attribute type.



17
18
19
# File 'lib/rbsrt/streamid_components.rb', line 17

def type
  @type
end

#user_nameObject

Returns the value of attribute user_name.



14
15
16
# File 'lib/rbsrt/streamid_components.rb', line 14

def user_name
  @user_name
end

Instance Method Details

#merge_hash!(hash) ⇒ Object



124
125
126
127
128
# File 'lib/rbsrt/streamid_components.rb', line 124

def merge_hash!(hash)
  hash.each do |key, val|
    set(key, val)
  end
end

#merge_str!(streamid) ⇒ Object



118
119
120
121
122
# File 'lib/rbsrt/streamid_components.rb', line 118

def merge_str!(streamid)
  streamid.scan(/([a-z0-9_-]+?)=(.+?)(,|$)/i).each do |key,val,_|
    set(key, val)
  end
end

#merge_uri!(streamid) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/rbsrt/streamid_components.rb', line 107

def merge_uri!(streamid)
  uri = URI(streamid)

  @host_name = uri.hostname
  @resource_name = uri.path[1..-1] if uri.path

  if uri.query && form = URI.decode_www_form(uri.query)
    merge_hash!(Hash[form])
  end
end

#set(key, val) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rbsrt/streamid_components.rb', line 89

def set(key, val)
  case key.to_s
  when 'resource_name', 'r' then @resource_name = val
  when 'user_name', 'u' then @user_name = val
  when 'mode', 'm' then @mode = val.to_sym if Modes.include?(val.to_s)
  when 'host_name', 'h' 
    if sep = val.index("/")
      @host_name = val[0..(sep - 1)]
      (@resource_name = val[(sep + 1)..-1] unless @resource_name)
    else
      @host_name = val
    end
  when 'type', 't' then (@type = val.to_sym if Types.include?(val.to_s))
  when 'sessionid', 's' then @sessionid = val
  else @extra[key] = val
  end
end

#to_hObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rbsrt/streamid_components.rb', line 61

def to_h
  h = {}

  @extra.each do |key, value|
    h[key] = value
  end

  h[:resource_name] = resource_name if resource_name
  h[:user_name] = user_name if user_name
  h[:mode] = mode if mode
  h[:host_name] = host_name if host_name
  h[:type] = type if type
  h[:sessionid] = sessionid if sessionid
  
  return h
end

#to_sObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rbsrt/streamid_components.rb', line 44

def to_s
  streamid = Header.dup

  vars = []

  vars << "r=#{resource_name}" if resource_name
  vars << "u=#{user_name}" if user_name
  vars << "m=#{mode}" if mode
  vars << "h=#{host_name}" if host_name
  vars << "t=#{type}" if type
  vars << "s=#{sessionid}" if sessionid
  
  extra.each { |key,val| vars << "#{key}=#{val}" }

  streamid + vars.join(",")
end