Class: Sendgrid::API::Entities::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/sendgrid/api/entities/entity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Entity

Returns a new instance of Entity.



10
11
12
# File 'lib/sendgrid/api/entities/entity.rb', line 10

def initialize(attributes = {})
  @attributes = sanitize_attributes(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/sendgrid/api/entities/entity.rb', line 8

def attributes
  @attributes
end

Class Method Details

.attribute(*args) ⇒ Object

Add attributes to the entity



63
64
65
66
67
# File 'lib/sendgrid/api/entities/entity.rb', line 63

def attribute(*args)
  @attributes = attributes
  @attributes += args
  @attributes.uniq!
end

.attributesObject

Get the entity attributes



70
71
72
# File 'lib/sendgrid/api/entities/entity.rb', line 70

def attributes
  @attributes ||= []
end

.clear_attributesObject



74
75
76
# File 'lib/sendgrid/api/entities/entity.rb', line 74

def clear_attributes
  @attributes = []
end

.from_response(response) ⇒ Object

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



51
52
53
54
55
56
57
58
59
60
# 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

Instance Method Details

#as_jsonObject



18
19
20
# File 'lib/sendgrid/api/entities/entity.rb', line 18

def as_json
  attributes
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/sendgrid/api/entities/entity.rb', line 33

def respond_to?(method, include_private = false)
  super || has_attribute?(method)
end

#to_json(*a) ⇒ Object



14
15
16
# File 'lib/sendgrid/api/entities/entity.rb', line 14

def to_json(*a)
  as_json.to_json(*a)
end