Class: BigBlueButton::BigBlueButtonFormatter

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

Overview

Helper class to format the response hash received when the BigBlueButtonApi makes API calls

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ BigBlueButtonFormatter

Returns a new instance of BigBlueButtonFormatter.



7
8
9
# File 'lib/bigbluebutton_formatter.rb', line 7

def initialize(hash)
  @hash = hash || {}
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



5
6
7
# File 'lib/bigbluebutton_formatter.rb', line 5

def hash
  @hash
end

Class Method Details

.format_attendee(attendee) ⇒ Object

Default formatting for an attendee hash



110
111
112
113
114
115
# File 'lib/bigbluebutton_formatter.rb', line 110

def self.format_attendee(attendee)
  f = BigBlueButtonFormatter.new(attendee)
  f.to_string(:userID)
  f.to_sym(:role)
  attendee
end

.format_meeting(meeting) ⇒ Object

Default formatting for a meeting hash



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/bigbluebutton_formatter.rb', line 92

def self.format_meeting(meeting)
  f = BigBlueButtonFormatter.new(meeting)
  f.to_string(:meetingID)
  f.to_string(:meetingName)
  f.to_string(:moderatorPW)
  f.to_string(:attendeePW)
  f.to_boolean(:hasBeenForciblyEnded)
  f.to_boolean(:running)
  f.to_int(:createTime) if meeting.has_key?(:createTime)
  f.to_string(:dialNumber)
  f.to_int(:voiceBridge)
  f.to_int(:participantCount)
  f.to_int(:listenerCount)
  f.to_int(:videoCount)
  meeting
end

.format_recording(rec) ⇒ Object

Default formatting for a recording hash



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/bigbluebutton_formatter.rb', line 118

def self.format_recording(rec)
  f = BigBlueButtonFormatter.new(rec)
  f.to_string(:recordID)
  f.to_string(:meetingID)
  f.to_string(:name)
  f.to_boolean(:published)
  f.to_datetime(:startTime)
  f.to_datetime(:endTime)
  if rec[:playback] and rec[:playback][:format]
    if rec[:playback][:format].is_a?(Hash)
      f2 = BigBlueButtonFormatter.new(rec[:playback][:format])
      f2.to_int(:length)
    elsif rec[:playback][:format].is_a?(Array)
      rec[:playback][:format].each do |format|
        f2 = BigBlueButtonFormatter.new(format)
        f2.to_int(:length)
      end
    end
  end
  if rec[:metadata]
    rec[:metadata].each do |key, value|
      if value.nil? or value.empty? or value.split.empty?
        # removes any no {}s, []s, or " "s, should always be empty string
        rec[:metadata][key] = ""
      end
    end
  end
  rec
end

Instance Method Details

#default_formattingObject

Default formatting for all responses given by a BBB server



80
81
82
83
84
85
86
87
88
89
# File 'lib/bigbluebutton_formatter.rb', line 80

def default_formatting
  response = @hash

  # Adjust some values. There will always be a returncode, a message and a messageKey in the hash.
  response[:returncode] = response[:returncode].downcase == "success"                              # true instead of "SUCCESS"
  response[:messageKey] = "" if !response.has_key?(:messageKey) or response[:messageKey].empty?    # "" instead of {}
  response[:message] = "" if !response.has_key?(:message) or response[:message].empty?             # "" instead of {}

  @hash = response
end

#flatten_objects(first, second) ⇒ Object

Simplifies the XML-styled hash node ‘first’. Its value will then always be an Array.

For example, if the current hash is:

{ :name => "Test", :attendees => { :attendee => [ { :name => "attendee1" }, { :name => "attendee2" } ] } }

Calling:

flatten_objects(:attendees, :attendee)

The hash will become:

{ :name => "Test", :attendees => [ { :name => "attendee1" }, { :name => "attendee2" } ] }

Other examples:

# Hash:
{ :name => "Test", :attendees => {} }
# Result:
{ :name => "Test", :attendees => [] }

# Hash:
{ :name => "Test", :attendees => { :attendee => { :name => "attendee1" } } }
# Result:
{ :name => "Test", :attendees => [ { :name => "attendee1" } ] }


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/bigbluebutton_formatter.rb', line 171

def flatten_objects(first, second)
  if !@hash[first] or @hash[first].empty?
    collection = []
  else
    node = @hash[first][second]
    if node.kind_of?(Array)
      collection = node
    else
      collection = []
      collection << node
    end
  end
  @hash[first] = collection
  @hash
end

#to_boolean(key) ⇒ Object

converts a value in the @hash to boolean



12
13
14
15
16
17
18
# File 'lib/bigbluebutton_formatter.rb', line 12

def to_boolean(key)
  unless @hash.has_key?(key)
    false
  else
    @hash[key] = @hash[key].downcase == "true"
  end
end

#to_datetime(key) ⇒ Object

converts a value in the @hash to DateTime



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
# File 'lib/bigbluebutton_formatter.rb', line 35

def to_datetime(key)
  unless @hash.has_key?(key) and @hash[key]
    nil
  else
    # BBB >= 0.8 uses the unix epoch for all time related values
    # older versions use strings

    # a number but in a String class
    if (@hash[key].class == String && @hash[key].to_i.to_s == @hash[key])
      value = @hash[key].to_i
    else
      value = @hash[key]
    end

    if value.is_a?(Numeric)
      result = value == 0 ? nil : DateTime.parse(Time.at(value/1000.0).to_s)
    else
      if value.downcase == "null"
        result = nil
      else
        # note: just in case the value comes as a string in the format: "Thu Sep 01 17:51:42 UTC 2011"
        result = DateTime.parse(value)
      end
    end

    @hash[key] = result
  end
end

#to_int(key) ⇒ Object

converts a value in the @hash to int



21
22
23
24
25
26
27
# File 'lib/bigbluebutton_formatter.rb', line 21

def to_int(key)
  unless @hash.has_key?(key)
    0
  else
    @hash[key] = @hash[key].to_i rescue 0
  end
end

#to_string(key) ⇒ Object

converts a value in the @hash to string



30
31
32
# File 'lib/bigbluebutton_formatter.rb', line 30

def to_string(key)
  @hash[key] = @hash[key].to_s
end

#to_sym(key) ⇒ Object

converts a value in the @hash to a symbol



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bigbluebutton_formatter.rb', line 65

def to_sym(key)
  unless @hash.has_key?(key)
    ""
  else
    if @hash[key].instance_of?(Symbol)
      @hash[key]
    elsif @hash[key].empty?
      ""
    else
      @hash[key] = @hash[key].downcase.to_sym
    end
  end
end