Class: WowzaRest::Data::Base
- Inherits:
-
Object
- Object
- WowzaRest::Data::Base
show all
- Defined in:
- lib/wowza_rest/data/base.rb
Direct Known Subclasses
Application, Application::DRMConfig, Application::DVRConfig, Application::Module, Application::SecurityConfig, Application::StreamConfig, Application::TranscoderConfig, Application::TranscoderConfig::Template, ApplicationShort, IncomingStreamStats, IncomingStreamStats::ConnectionCount, Instance, Instance::IncomingStream, Instance::OutgoingStream, Instance::Recorder, Instance::StreamGroup, ServerStatus
Instance Method Summary
collapse
Constructor Details
#initialize(attrs = {}) ⇒ Base
Returns a new instance of Base.
4
5
6
|
# File 'lib/wowza_rest/data/base.rb', line 4
def initialize(attrs = {})
setup_attributes(attrs || {})
end
|
Instance Method Details
#define_key_getter(key) ⇒ Object
16
17
18
19
20
|
# File 'lib/wowza_rest/data/base.rb', line 16
def define_key_getter(key)
define_singleton_method("#{key.underscore}_key") do
key.to_s
end
end
|
#include?(attr_name) ⇒ Boolean
64
65
66
|
# File 'lib/wowza_rest/data/base.rb', line 64
def include?(attr_name)
to_h.include?(attr_name.to_s)
end
|
#keys_reader(*args) ⇒ Object
22
23
24
25
26
|
# File 'lib/wowza_rest/data/base.rb', line 22
def keys_reader(*args)
args.each do |arg|
define_key_getter(arg.to_s)
end
end
|
#objects_array_to_hash_array(arr) ⇒ Object
32
33
34
35
36
37
|
# File 'lib/wowza_rest/data/base.rb', line 32
def objects_array_to_hash_array(arr)
return if arr.nil?
arr.map do |obj|
obj.is_a?(WowzaRest::Data::Base) ? obj.to_h : obj
end
end
|
#setup_attributes(attrs) ⇒ Object
8
9
10
11
12
13
14
|
# File 'lib/wowza_rest/data/base.rb', line 8
def setup_attributes(attrs)
attrs.each do |k, v|
singleton_class.send(:attr_accessor, k.to_s.underscore)
define_key_getter(k.to_s)
instance_variable_set("@#{k.to_s.underscore}", v)
end
end
|
#to_h ⇒ Object
Also known as:
to_hash
rubocop:disable Metrics/MethodLength
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/wowza_rest/data/base.rb', line 40
def to_h
instance_variables.each_with_object({}) do |var, hash|
value = instance_variable_get(var)
hash[send("#{var.to_s.delete('@')}_key")] =
if value.is_a?(WowzaRest::Data::Base)
instance_variable_get(var).to_h
elsif value.is_a?(Array)
if block_given?
yield(var, instance_variable_get(var))
else
objects_array_to_hash_array(instance_variable_get(var))
end
else
instance_variable_get(var)
end
end
end
|
#to_json ⇒ Object
60
61
62
|
# File 'lib/wowza_rest/data/base.rb', line 60
def to_json
to_h.to_json
end
|
#wrap_array_objects(arr, klass) ⇒ Object
28
29
30
|
# File 'lib/wowza_rest/data/base.rb', line 28
def wrap_array_objects(arr, klass)
arr.map { |obj| klass.new(obj) } unless arr.nil?
end
|