Class: Sendgrid::API::Entities::Entity
- Inherits:
-
Object
- Object
- Sendgrid::API::Entities::Entity
- Defined in:
- lib/sendgrid/api/entities/entity.rb
Direct Known Subclasses
Category, Email, List, MarketingEmail, Profile, Response, ResponseInsert, ResponseRemove, Schedule, SenderAddress, Stats
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
-
.attribute(*args) ⇒ Object
Add attributes to the entity.
-
.attributes ⇒ Object
Get the entity attributes.
- .clear_attributes ⇒ Object
-
.from_response(response) ⇒ Object
Instantiate the entity from API response body.
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(attributes = {}) ⇒ Entity
constructor
A new instance of Entity.
- #method_missing(method, *args, &block) ⇒ Object
- #respond_to?(method, include_private = false) ⇒ Boolean
- #to_json(*a) ⇒ Object
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
#attributes ⇒ Object (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 |
.attributes ⇒ Object
Get the entity attributes
70 71 72 |
# File 'lib/sendgrid/api/entities/entity.rb', line 70 def attributes @attributes ||= [] end |
.clear_attributes ⇒ Object
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_json ⇒ Object
18 19 20 |
# File 'lib/sendgrid/api/entities/entity.rb', line 18 def as_json attributes end |
#respond_to?(method, include_private = false) ⇒ 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 |