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::Campaign, AWeber::Resources::Click, AWeber::Resources::CustomField, AWeber::Resources::Followup, AWeber::Resources::Integration, AWeber::Resources::Link, AWeber::Resources::List, AWeber::Resources::Message, AWeber::Resources::Open, AWeber::Resources::Stat, AWeber::Resources::Subscriber, AWeber::Resources::TrackedEvent, AWeber::Resources::WebForm, AWeber::Resources::WebFormSplitTest, AWeber::Resources::WebFormSplitTestComponent
Class Attribute Summary collapse
-
.path ⇒ Object
Returns the value of attribute path.
-
.writable_attrs ⇒ Object
Returns the value of attribute writable_attrs.
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
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.
- .basepath(path) ⇒ Object
-
.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.
- #inspect ⇒ Object
- #path ⇒ Object
- #save ⇒ Object
- #uri ⇒ Object
- #writable_attrs ⇒ Object
Constructor Details
#initialize(client, data = {}) ⇒ Resource
Returns a new instance of Resource.
108 109 110 111 112 113 |
# File 'lib/aweber/resource.rb', line 108 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
.path ⇒ Object
Returns the value of attribute path.
19 20 21 |
# File 'lib/aweber/resource.rb', line 19 def path @path end |
.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 |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
106 107 108 |
# File 'lib/aweber/resource.rb', line 106 def parent @parent end |
Class Method Details
.alias_attribute(alias_, attribute) ⇒ Object
Works the same as alias_method
, but for attributes created via attr*
methods.
28 29 30 |
# File 'lib/aweber/resource.rb', line 28 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.
45 46 47 48 49 50 51 52 53 |
# File 'lib/aweber/resource.rb', line 45 def api_attr(attr_, opts={}) if opts[:writable] attr_accessor attr_ @writable_attrs ||= [] @writable_attrs << attr_ else attr_reader attr_ end end |
.basepath(path) ⇒ Object
21 22 23 |
# File 'lib/aweber/resource.rb', line 21 def basepath(path) @path = path end |
.has_many(name) ⇒ AWeber::Collection
Creates a lazy loaded method which will retrieve and create a collection of name
of objects.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/aweber/resource.rb', line 81 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) response = client.get(resource_link).merge(:parent => self) collection = Collection.new(client, klass, response) instance_variable_set("@#{name}", collection) end end |
.has_one(name) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/aweber/resource.rb', line 55 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
131 132 133 |
# File 'lib/aweber/resource.rb', line 131 def <=>(other) @id <=> other.id end |
#delete ⇒ Object
115 116 117 |
# File 'lib/aweber/resource.rb', line 115 def delete client.delete(@self_link) end |
#inspect ⇒ Object
143 144 145 |
# File 'lib/aweber/resource.rb', line 143 def inspect %(#<#{self.class} id="#{id}" />) end |
#path ⇒ Object
135 136 137 |
# File 'lib/aweber/resource.rb', line 135 def path parent and "#{parent.path}/#{id}" or id.to_s end |
#save ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/aweber/resource.rb', line 119 def save body = writable_attrs.inject({}) do |body, attr_| body[attr_] = send(attr_) body end client.put(self_link, body) end |
#uri ⇒ Object
139 140 141 |
# File 'lib/aweber/resource.rb', line 139 def uri self_link.gsub(AWeber.api_url, '') end |
#writable_attrs ⇒ Object
127 128 129 |
# File 'lib/aweber/resource.rb', line 127 def writable_attrs self.class.writable_attrs ||= {} end |