Class: Mailchimp::Instance

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

Direct Known Subclasses

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Instance methods



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

def initialize(client, data, collection_path = '')
  @client = client
  @data = data
  @collection_path = collection_path
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



61
62
63
64
65
# File 'lib/mailchimp_api_v3/instance.rb', line 61

def method_missing(symbol, options = {})
  key = symbol.id2name
  return @data[key] if @data.key? key
  super
end

Class Method Details

.get(client, collection_path, id) ⇒ Object

Class methods



7
8
9
10
# File 'lib/mailchimp_api_v3/instance.rb', line 7

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

Instance Method Details

#deleteObject



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

def delete
  @client.delete(path)
end

#matches?(match_data) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/mailchimp_api_v3/instance.rb', line 33

def matches?(match_data)
  match_data.each do |k, v|
    break false unless same?(__send__(k), v)
    true
  end
end

#pathObject



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

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

#same?(thing, other_thing) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/mailchimp_api_v3/instance.rb', line 40

def same?(thing, other_thing)
  return thing.casecmp(other_thing.to_s).zero? if thing.is_a? String
  return thing.subhash?(other_thing.to_h)      if thing.is_a? Hash
  thing == other_thing
end

#subclass_from(collection_class, *args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/mailchimp_api_v3/instance.rb', line 46

def subclass_from(collection_class, *args)
  id, name, options = parse_args(*args)
  return subclass_instance_from(collection_class, id) if id

  paging_options = options.divide_on('start', 'page_size') if options
  collection = collection_class.new @client, path, paging_options

  return collection.find_by collection.name_field => name if name
  options.nil? || options.empty? ? collection : collection.where(options)
end

#update(new_data) ⇒ Object



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

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