class NeverBounce::API::Response::Container

A container with values, by default supports JSON parsing. @see API::Feature::Eigencache @see API::Feature::Oattrs @see API::Feature::RequireAttr

Private Class Methods

oattr(name, type, *args) click to toggle source

Declare & register an oattr.

oattr :first_name, :scalar    # => :first_name
oattr ...                     # Handled by `Feature::Oattrs`.

@return [Symbol] name. @see Feature::Oattrs::ClassMethods#oattr

Calls superclass method
# File lib/never_bounce/api/response/container.rb, line 54
def oattr(name, type, *args)
  if type == :scalar
    scalar_oattr(name, *args)
    name.tap { oattrs << name }
  else
    # Handle other types in parent class.
    super
  end
end
scalar_oattr(name, options = {}) click to toggle source

Build a generic scalar oattr.

scalar_oattr :first_name
scalar_oattr :page, type: :integer
scalar_oattr :total_valid, type: :integer, allow_nil: true

@return [Symbol]

# File lib/never_bounce/api/response/container.rb, line 71
def scalar_oattr(name, options = {})
  o, options = {}, options.dup
  o[k = :allow_nil] = options.include?(k) ? options.delete(k) : false
  o[k = :key] = options.delete(k) || name
  o[k = :type] = options.delete(k) || :any
  raise ArgumentError, "Unknown options: #{options.inspect}" if not options.empty?

  code = []
  code << %{attr_writer :#{name}}

  code << case o[:type]
    when :any
      %{def #{name}; @#{name} ||= body_hash.fetch("#{o[:key]}"); end}
    when :float
      if o[:allow_nil]
        %{
          def #{name}
            @#{name} ||= unless (v = body_hash.fetch("#{o[:key]}")).nil?
              Float(v)
            end
          end
        }
      else
        %{def #{name}; @#{name} ||= Float(body_hash.fetch("#{o[:key]}")); end}
      end
    when :integer
      if o[:allow_nil]
        %{
          def #{name}
            @#{name} ||= unless (v = body_hash.fetch("#{o[:key]}")).nil?
              Integer(v)
            end
          end
        }
      else
        %{def #{name}; @#{name} ||= Integer(body_hash.fetch("#{o[:key]}")); end}
      end
    else
      raise ArgumentError, "Unknown type: #{o[:type].inspect}"
    end

  class_eval code.join(";")

  name
end

Public Instance Methods

body_hash() click to toggle source

Container data. Default is JSON-parsed {#raw}. @!attribute body_hash @return [Hash]

# File lib/never_bounce/api/response/container.rb, line 23
def body_hash
  _cache[:body_hash] ||= JSON.parse(require_attr(:raw))
end
body_hash=(value) click to toggle source
# File lib/never_bounce/api/response/container.rb, line 27
def body_hash=(value)
  _cache[:body_hash] = value
end
raw() click to toggle source

Raw response body. @!attribute raw @return [String]

# File lib/never_bounce/api/response/container.rb, line 34
def raw
  _cache[:raw]
end
raw=(value) click to toggle source
# File lib/never_bounce/api/response/container.rb, line 38
def raw=(value)
  _cache[:raw] ||= value
end