class Mailchimp::Collection
Public Class Methods
new(client, parent_path = '', options = {})
click to toggle source
Calls superclass method
# File lib/mailchimp_api_v3/collection.rb, line 7 def initialize(client, parent_path = '', options = {}) @client = client @parent_path = parent_path parse_options(options) super page_children end
Public Instance Methods
count()
click to toggle source
# File lib/mailchimp_api_v3/collection.rb, line 15 def count page['total_items'] end
create(data)
click to toggle source
# File lib/mailchimp_api_v3/collection.rb, line 41 def create(data) response = @client.post path, data self.class::CHILD_CLASS.new @client, response, path end
create_or_update(data)
click to toggle source
# File lib/mailchimp_api_v3/collection.rb, line 52 def create_or_update(data) clean_data = data.deep_stringify_keys if clean_data.key? 'id' instance = self.class::CHILD_CLASS.get @client, path, clean_data.delete('id') instance ? instance.update(clean_data) : create(clean_data) else find_by(clean_data) || create(clean_data) end end
find_by(data)
click to toggle source
# File lib/mailchimp_api_v3/collection.rb, line 36 def find_by(data) instances = where(data, 1) instances ? instances.first : nil end
first_or_create(data)
click to toggle source
# File lib/mailchimp_api_v3/collection.rb, line 46 def first_or_create(data) instance = find_by(data) return instance if instance create(data) end
name_field()
click to toggle source
# File lib/mailchimp_api_v3/collection.rb, line 63 def name_field self.class.const_defined?(:NAME_FIELD) ? self.class::NAME_FIELD : 'name' end
path()
click to toggle source
# File lib/mailchimp_api_v3/collection.rb, line 19 def path @path ||= "#{@parent_path}/#{self.class::PATH_KEY}" end
where(data, limit = nil)
click to toggle source
# File lib/mailchimp_api_v3/collection.rb, line 23 def where(data, limit = nil) instances = [] find_each do |instance| if instance.matches? data instances << instance break if instances.count == limit end end instances end