Class: AWeber::Resource
- Inherits:
-
Object
- Object
- AWeber::Resource
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/aweber/resource.rb
Overview
Base class for all entries. All resources have the following attributes:
-
id
-
http_etag
(aliased asetag
) -
self_link
(aliased aslink
) -
resource_type_link
(aliased asresource_type
)
Resources also have attributes based on their the type of resource it is. To see a full list of attributes per resource, visit labs.aweber.com/docs/reference/1.0/reference.
Direct Known Subclasses
Collection, AWeber::Resources::Account, AWeber::Resources::Broadcast, AWeber::Resources::Click, AWeber::Resources::Followup, AWeber::Resources::Integration, AWeber::Resources::Link, AWeber::Resources::List, AWeber::Resources::Message, AWeber::Resources::Open, AWeber::Resources::Subscriber, AWeber::Resources::TrackedEvent, AWeber::Resources::WebForm, AWeber::Resources::WebFormSplitTest, AWeber::Resources::WebFormSplitTestComponent
Class Attribute Summary collapse
-
.writable_attrs ⇒ Object
Returns the value of attribute writable_attrs.
Class Method Summary collapse
-
.alias_attribute(alias_, attribute) ⇒ Object
Works the same as
alias_method
, but for attributes created viaattr*
methods. -
.api_attr(attr_, opts = {}) ⇒ Object
Defines an attribute that it either read only or writable.
-
.has_many(name) ⇒ AWeber::Collection
Creates a lazy loaded method which will retrieve and create a collection of
name
of objects. - .has_one(name) ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #delete ⇒ Object
-
#initialize(client, data = {}) ⇒ Resource
constructor
A new instance of Resource.
- #save ⇒ Object
- #writable_attrs ⇒ Object
Constructor Details
#initialize(client, data = {}) ⇒ Resource
Returns a new instance of Resource.
97 98 99 100 101 102 |
# File 'lib/aweber/resource.rb', line 97 def initialize(client, data={}) @client = client data.each do |key, value| instance_variable_set("@#{key}", value) if respond_to? key end end |
Class Attribute Details
.writable_attrs ⇒ Object
Returns the value of attribute writable_attrs.
18 19 20 |
# File 'lib/aweber/resource.rb', line 18 def writable_attrs @writable_attrs end |
Class Method Details
.alias_attribute(alias_, attribute) ⇒ Object
Works the same as alias_method
, but for attributes created via attr*
methods.
22 23 24 |
# File 'lib/aweber/resource.rb', line 22 def alias_attribute(alias_, attribute) alias_method alias_, attribute end |
.api_attr(attr_, opts = {}) ⇒ Object
Defines an attribute that it either read only or writable.
Example:
# Read-only
api_attr :name
# Writable
api_attr :name, :writable => true
If an attribute is writable it will be sent with the request when save
is called.
39 40 41 42 43 44 45 46 47 |
# File 'lib/aweber/resource.rb', line 39 def api_attr(attr_, opts={}) if opts[:writable] attr_accessor attr_ @writable_attrs ||= [] @writable_attrs << attr_ else attr_reader attr_ end end |
.has_many(name) ⇒ AWeber::Collection
Creates a lazy loaded method which will retrieve and create a collection of name
of objects.
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/aweber/resource.rb', line 75 def has_many(name) define_method(name) do ivar = instance_variable_get("@#{name}") return ivar if ivar resource_link = instance_variable_get("@#{name}_collection_link") klass = AWeber.get_class(name) collection = Collection.new(client, klass, client.get(resource_link)) instance_variable_set("@#{name}", collection) end end |
.has_one(name) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/aweber/resource.rb', line 49 def has_one(name) define_method(name) do ivar = instance_variable_get("@#{name}") return ivar if ivar resource_link = instance_variable_get("@#{name}_link") klass = AWeber.get_class(:"#{name}s") collection = klass.new(client, client.get(resource_link)) instance_variable_set("@#{name}", collection) end end |
Instance Method Details
#<=>(other) ⇒ Object
120 121 122 |
# File 'lib/aweber/resource.rb', line 120 def <=>(other) @id <=> other.id end |
#delete ⇒ Object
104 105 106 |
# File 'lib/aweber/resource.rb', line 104 def delete client.delete(@self_link) end |
#save ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/aweber/resource.rb', line 108 def save body = writable_attrs.inject({}) do |body, attr_| body[attr_] = send(attr_) body end client.put(self_link, body) end |
#writable_attrs ⇒ Object
116 117 118 |
# File 'lib/aweber/resource.rb', line 116 def writable_attrs self.class.writable_attrs ||= {} end |