class Knowtify::Contacts

Attributes

api_key[RW]
contacts[RW]
http_request_options[RW]
invalid_contacts[RW]
response[RW]

Public Class Methods

new(contacts = [],opts={}) click to toggle source
# File lib/knowtify/contacts.rb, line 11
def initialize(contacts = [],opts={})
  @contacts = []
  @invalid_contacts = []
  contacts = (contacts.is_a?(String) ? JSON.parse(contacts) : contacts)
  contacts.each do |contact|
    add(contact)
  end
  opts.each do |meth, val|
    send("#{meth}=",val)
  end
  @http_request_options ||= {}
end

Public Instance Methods

add(contact) click to toggle source
# File lib/knowtify/contacts.rb, line 24
def add(contact)
  contact = contact.is_a?(Knowtify::Contact) ? contact : Knowtify::Contact.new(contact)
  if contact.valid?
    @contacts << contact
  else
    @invalid_contacts << contact
  end
  contact
end
Also aliased as: add_contact
add_contact(contact)
Alias for: add
delete() click to toggle source
# File lib/knowtify/contacts.rb, line 70
def delete
  @response = client.contacts_delete(emails,http_request_options,api_key) if valid?(true)
  valid?(true)
end
emails() click to toggle source
# File lib/knowtify/contacts.rb, line 66
def emails
  contacts.collect(&:email)
end
save() click to toggle source
# File lib/knowtify/contacts.rb, line 61
def save
  @response = client.contacts_create_or_update(contacts.collect(&:to_hash),http_request_options,api_key) if valid?
  valid?
end
to_hash() click to toggle source
# File lib/knowtify/contacts.rb, line 35
def to_hash
  {'contacts' => contacts.collect(&:to_hash)}
end
to_json() click to toggle source
# File lib/knowtify/contacts.rb, line 39
def to_json
  to_hash.to_json
end
valid?(for_delete=false) click to toggle source
# File lib/knowtify/contacts.rb, line 43
def valid?(for_delete=false)
  @errors = []
  @errors << "There are invalid contacts." if !@invalid_contacts.empty? && !config.ingore_invalid_contacts?
  @errors << "There are no valid contacts" if @contacts.empty?
  if @response
    if @response.authentication_error?
      add_authenication_error
    else
      unless parsed_response['contacts_updated'] == @contacts.length
        action = (for_delete ? 'delete' : 'create/update')
        Knowtify.logger.error "Knowtify contacts #{action} operation failed: #{response.body}." if Knowtify.logger
        @errors << "#{@contacts.length - parsed_response['contacts_updated']} contacts failed to #{action}"
      end
    end
  end
  @errors.empty?
end