class Sendgrid::API::Entities::Entity

Attributes

attributes[R]

Public Class Methods

new(attributes = {}) click to toggle source
# File lib/sendgrid/api/entities/entity.rb, line 10
def initialize(attributes = {})
  @attributes = sanitize_attributes(attributes)
end

Private Class Methods

attribute(*args) click to toggle source

Add attributes to the entity

# File lib/sendgrid/api/entities/entity.rb, line 63
def attribute(*args)
  @attributes = attributes
  @attributes += args
  @attributes.uniq!
end
attributes() click to toggle source

Get the entity attributes

# File lib/sendgrid/api/entities/entity.rb, line 70
def attributes
  @attributes ||= []
end
clear_attributes() click to toggle source
# File lib/sendgrid/api/entities/entity.rb, line 74
def clear_attributes
  @attributes = []
end
from_response(response) click to toggle source

Instantiate the entity from API response body. Can generate multiple entities if response is an Array.

# File lib/sendgrid/api/entities/entity.rb, line 51
def from_response(response)
  body = response.body
  if body.is_a?(Array)
    body.map { |item| new(item) }
  elsif body.is_a?(Hash)
    new(body)
  else
    nil
  end
end

Public Instance Methods

as_json() click to toggle source
# File lib/sendgrid/api/entities/entity.rb, line 18
def as_json
  attributes
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/sendgrid/api/entities/entity.rb, line 22
def method_missing(method, *args, &block)
  setter = method.to_s.gsub(/=$/, '').to_sym
  if has_attribute?(method)
    attributes[method]
  elsif has_attribute?(setter)
    attributes[setter] = args.first
  else
    super
  end
end
respond_to?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/sendgrid/api/entities/entity.rb, line 33
def respond_to?(method, include_private = false)
  super || has_attribute?(method)
end
to_json(*a) click to toggle source
# File lib/sendgrid/api/entities/entity.rb, line 14
def to_json(*a)
  as_json.to_json(*a)
end

Private Instance Methods

has_attribute?(attribute) click to toggle source
# File lib/sendgrid/api/entities/entity.rb, line 39
def has_attribute?(attribute)
  self.class.attributes.include?(attribute)
end
sanitize_attributes(attributes) click to toggle source
# File lib/sendgrid/api/entities/entity.rb, line 43
def sanitize_attributes(attributes)
  attributes.reject { |key, value| !has_attribute?(key) }
end