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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

.pathObject

Returns the value of attribute path.



19
20
21
# File 'lib/aweber/resource.rb', line 19

def path
  @path
end

.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

Instance Attribute Details

#parentObject (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.

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:



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

#deleteObject



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

def delete
  client.delete(@self_link)
end

#inspectObject



143
144
145
# File 'lib/aweber/resource.rb', line 143

def inspect
  %(#<#{self.class} id="#{id}" />)
end

#pathObject



135
136
137
# File 'lib/aweber/resource.rb', line 135

def path
  parent and "#{parent.path}/#{id}" or id.to_s
end

#saveObject



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

#uriObject



139
140
141
# File 'lib/aweber/resource.rb', line 139

def uri
  self_link.gsub(AWeber.api_url, '')
end

#writable_attrsObject



127
128
129
# File 'lib/aweber/resource.rb', line 127

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