Class: AWeber::Resource

Inherits:
Object
  • Object
show all
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 as etag)

  • self_link (aliased as link)

  • resource_type_link (aliased as resource_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.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_attrsObject

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.

Examples:

class Account
  attr_reader :lists_collection_link
  collection  :lists
end

#=> Account.new(...).lists #=> <AWeber::Collection>

Parameters:

  • name (Symbol)

    name of the collection method

Returns:



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

#deleteObject



104
105
106
# File 'lib/aweber/resource.rb', line 104

def delete
  client.delete(@self_link)
end

#saveObject



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_attrsObject



116
117
118
# File 'lib/aweber/resource.rb', line 116

def writable_attrs
  self.class.writable_attrs ||= {}
end