class Mailchimp::Instance

Attributes

data[R]

Public Class Methods

get(client, collection_path, id) click to toggle source

Class methods

# 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
new(client, data, collection_path = '') click to toggle source

Instance methods

# File lib/mailchimp_api_v3/instance.rb, line 14
def initialize(client, data, collection_path = '')
  @client = client
  @data = data
  @collection_path = collection_path
end

Public Instance Methods

delete() click to toggle source
# File lib/mailchimp_api_v3/instance.rb, line 25
def delete
  @client.delete(path)
end
matches?(match_data) click to toggle source
# 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
path() click to toggle source
# File lib/mailchimp_api_v3/instance.rb, line 29
def path
  @path ||= "#{@collection_path}/#{@data['id']}"
end
same?(thing, other_thing) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/mailchimp_api_v3/instance.rb, line 20
def update(new_data)
  @data = @client.patch(path, new_data)
  self
end

Private Instance Methods

id_and_name_from(string) click to toggle source

If string is a hex string assume it's an id, otherwise it's a name

# File lib/mailchimp_api_v3/instance.rb, line 89
def id_and_name_from(string)
  string =~ /[^0-9a-f]/i ? [nil, string] : [string, nil]
end
method_missing(symbol, options = {}) click to toggle source
Calls superclass method
# 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
parse_args(*args) click to toggle source
# File lib/mailchimp_api_v3/instance.rb, line 77
def parse_args(*args)
  first_arg = args.shift
  second_arg = args.shift

  if first_arg.is_a? String
    id_and_name_from(first_arg) << second_arg
  else
    [nil, nil, first_arg]
  end
end
respond_to_missing?(symbol, *_) click to toggle source
# File lib/mailchimp_api_v3/instance.rb, line 67
def respond_to_missing?(symbol, *_)
  key = symbol.id2name
  @data.key? key
end
subclass_instance_from(collection_class, id) click to toggle source
# File lib/mailchimp_api_v3/instance.rb, line 72
def subclass_instance_from(collection_class, id)
  child_path = "#{path}/#{collection_class::PATH_KEY}"
  collection_class::CHILD_CLASS.get @client, child_path, id
end