Class: Mailchimp::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/mailchimp_api_v3/instance.rb

Direct Known Subclasses

Account, List, List::InterestCategory, List::Member

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, data, collection_path = '') ⇒ Instance

Instance methods



12
13
14
15
16
17
# File 'lib/mailchimp_api_v3/instance.rb', line 12

def initialize(client, data, collection_path = '')
  @client = client
  @collection_path = collection_path
  @data = data
  #- puts @data # debug
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, options = {}) ⇒ Object (private)



50
51
52
53
54
# File 'lib/mailchimp_api_v3/instance.rb', line 50

def method_missing(symbol, options = {})
  key = symbol.id2name
  fail_unless_exists key, options
  @data[key]
end

Class Method Details

.get(client, collection_path, id) ⇒ Object

Class methods



5
6
7
8
# File 'lib/mailchimp_api_v3/instance.rb', line 5

def self.get(client, collection_path, id)
  data = client.get "#{collection_path}/#{id}"
  new client, data, collection_path
end

Instance Method Details

#matches?(match_data) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
# File 'lib/mailchimp_api_v3/instance.rb', line 28

def matches?(match_data)
  match_data.each do |k, v|
    break false unless __send__(k).casecmp(v).zero? # case-insensitive comparison
    true
  end
end

#pathObject



24
25
26
# File 'lib/mailchimp_api_v3/instance.rb', line 24

def path
  @path ||= "#{@collection_path}/#{@data['id']}"
end

#subclass_from(collection_class, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/mailchimp_api_v3/instance.rb', line 35

def subclass_from(collection_class, options = {})
  if options.is_a? String
    # Use it as an id for an instance
    child_path = "#{path}/#{collection_class::PATH_KEY}"
    collection_class::CHILD_CLASS.get @client, child_path, options
  else
    # Get the collection
    collection_class.new @client, path, options
  end
end

#update(new_data) ⇒ Object



19
20
21
22
# File 'lib/mailchimp_api_v3/instance.rb', line 19

def update(new_data)
  @data = @client.patch(new_data, path)
  self
end