Class: Knowtify::Contacts

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/knowtify/contacts.rb

Constant Summary

Constants included from Helper

Helper::AUTHENTICATION_ERROR_MESSAGE

Instance Attribute Summary collapse

Attributes included from Helper

#errors

Instance Method Summary collapse

Methods included from Helper

#add_authenication_error, #blank_string?, #client, #config, #parsed_response

Constructor Details

#initialize(contacts = [], opts = {}) ⇒ Contacts

Returns a new instance of Contacts.



11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#api_keyObject

Array - can be JSON, array of hashes or array of Knowtify::Contact objects



5
6
7
# File 'lib/knowtify/contacts.rb', line 5

def api_key
  @api_key
end

#contactsObject

Array - can be JSON, array of hashes or array of Knowtify::Contact objects



5
6
7
# File 'lib/knowtify/contacts.rb', line 5

def contacts
  @contacts
end

#http_request_optionsObject

Array - can be JSON, array of hashes or array of Knowtify::Contact objects



5
6
7
# File 'lib/knowtify/contacts.rb', line 5

def http_request_options
  @http_request_options
end

#invalid_contactsObject

Array - can be JSON, array of hashes or array of Knowtify::Contact objects



5
6
7
# File 'lib/knowtify/contacts.rb', line 5

def invalid_contacts
  @invalid_contacts
end

#responseObject

Array - can be JSON, array of hashes or array of Knowtify::Contact objects



5
6
7
# File 'lib/knowtify/contacts.rb', line 5

def response
  @response
end

Instance Method Details

#add(contact) ⇒ Object Also known as: add_contact



24
25
26
27
28
29
30
31
32
# 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

#deleteObject



70
71
72
73
# 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

#emailsObject



66
67
68
# File 'lib/knowtify/contacts.rb', line 66

def emails
  contacts.collect(&:email)
end

#saveObject



61
62
63
64
# 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_hashObject



35
36
37
# File 'lib/knowtify/contacts.rb', line 35

def to_hash
  {'contacts' => contacts.collect(&:to_hash)}
end

#to_jsonObject



39
40
41
# File 'lib/knowtify/contacts.rb', line 39

def to_json
  to_hash.to_json
end

#valid?(for_delete = false) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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