Class: AWeber::Collection

Inherits:
Resource show all
Includes:
Enumerable
Defined in:
lib/aweber/collection.rb

Overview

Collection objects are groups of Resources. Collections imitate regular Hashes in most ways. You can access Resource by id via the [] method.

lists    #=> #<AWeber::Collection ...>
lists[1] #=> #<AWeber::Resources::List @id=1 ...>

Also like Hashes, you can iterate over all of it’s entries

lists.each { |id, list| puts list.name }

Collections support dynamic find_by_* methods, where * is an attribute and the first and only parameter is the value.

lists.find_by_name("testlist")
#=> #<AWeber::Resources::List @id=123, @name="testlist" ...>

find_by_* methods will also return a Hash of entries if more than one matches the criteria. The hash will be keyed by resource ID.

messages.find_by_total_opens(0)
#=> { "45697" => #<Message>, "12345" => #<Message> }

Collections are paginated in groups of 100.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#<=>, alias_attribute, api_attr, basepath, #delete, has_many, has_one, #save, #uri, #writable_attrs

Constructor Details

#initialize(client, klass, data = {}) ⇒ Collection

Returns a new instance of Collection.

Parameters:

  • client (AWeber::Base)

    Instance of AWeber::Base

  • klass (Class)

    Class to create entries of

  • data (Hash) (defaults to: {})

    JSON decoded response data Hash



44
45
46
47
48
49
50
51
# File 'lib/aweber/collection.rb', line 44

def initialize(client, klass, data={})
  super client, data
  @client  = client
  @klass   = klass
  @entries = {}
  create_entries(data["entries"]) if data.include?("entries")
  @_entries = @entries.to_a
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



148
149
150
151
152
153
# File 'lib/aweber/collection.rb', line 148

def method_missing(method, *args)
  method.to_s.scan /^find_by_(.+)/ do |_attr|
    return find_by(_attr.first, *args)
  end
  super
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



30
31
32
# File 'lib/aweber/collection.rb', line 30

def entries
  @entries
end

Returns the value of attribute next_collection_link.



31
32
33
# File 'lib/aweber/collection.rb', line 31

def next_collection_link
  @next_collection_link
end

#parentObject (readonly)

Returns the value of attribute parent.



35
36
37
# File 'lib/aweber/collection.rb', line 35

def parent
  @parent
end

Returns the value of attribute prev_collection_link.



32
33
34
# File 'lib/aweber/collection.rb', line 32

def prev_collection_link
  @prev_collection_link
end

Returns the value of attribute resource_type_link.



33
34
35
# File 'lib/aweber/collection.rb', line 33

def resource_type_link
  @resource_type_link
end

#total_sizeObject (readonly) Also known as: size, length

Returns the value of attribute total_size.



34
35
36
# File 'lib/aweber/collection.rb', line 34

def total_size
  @total_size
end

Instance Method Details

#[](id) ⇒ Object



92
93
94
# File 'lib/aweber/collection.rb', line 92

def [](id)
  @entries[id] ||= fetch_entry(id)
end

#[]=(key, resource) ⇒ Object



96
97
98
# File 'lib/aweber/collection.rb', line 96

def []=(key, resource)
  @entries[key] = resource
end

#create(attrs = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/aweber/collection.rb', line 67

def create(attrs={})
  params = attrs.merge("ws.op" => "create")

  if params.has_key?('custom_fields')
    if params['custom_fields'].is_a?(Hash)
        params['custom_fields'] = params['custom_fields'].to_json
    end
  end

  response = client.post(path, params)

  if response.is_a? Net::HTTPCreated
    resource = get(response["location"]).merge(:parent => self)
    resource = @klass.new(client, resource)

    self[resource.id] = resource
  else
    if response
        raise CreationError, JSON.parse(response.body)['error']['message'], caller
    else
        raise CreationError, "No response received", caller
    end
  end
end

#eachObject



100
101
102
# File 'lib/aweber/collection.rb', line 100

def each
  (1..@total_size).each { |n| yield get_entry(n) }
end

#inspectObject



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

def inspect
  "#<AW::Collection(#{@klass.to_s}) size=\"#{size}\">"
end

#pathObject



108
109
110
# File 'lib/aweber/collection.rb', line 108

def path
  parent and parent.path.to_s + @klass.path.to_s or @klass.path.to_s
end

#search(params = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/aweber/collection.rb', line 53

def search(params={})
  if params.has_key?('custom_fields')
    if params['custom_fields'].is_a?(Hash)
        params['custom_fields'] = params['custom_fields'].to_json
    end
  end
  params   = params.map { |k,v| "#{h(k)}=#{h(v)}" }.join("&")
  uri      = "#{path}?ws.op=find&#{params}"
  response = client.get(uri).merge(:parent => parent)
  response["total_size"] ||= response["entries"].size

  self.class.new(client, @klass, response)
end