class WowzaRest::Data::Base

Public Class Methods

new(attrs = {}) click to toggle source
# File lib/wowza_rest/data/base.rb, line 4
def initialize(attrs = {})
  setup_attributes(attrs || {})
end

Public Instance Methods

define_key_getter(key) click to toggle source
# 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) click to toggle source
# File lib/wowza_rest/data/base.rb, line 64
def include?(attr_name)
  to_h.include?(attr_name.to_s)
end
keys_reader(*args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() { |var, instance_variable_get(var)| ... } click to toggle source

rubocop:disable Metrics/MethodLength

# 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
Also aliased as: to_hash
to_hash()

rubocop:enable Metrics/MethodLength

Alias for: to_h
to_json() click to toggle source
# File lib/wowza_rest/data/base.rb, line 60
def to_json
  to_h.to_json
end
wrap_array_objects(arr, klass) click to toggle source
# 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