Class: Knowtify::Contacts
- Inherits:
-
Object
- Object
- Knowtify::Contacts
- Includes:
- Helper
- Defined in:
- lib/knowtify/contacts.rb
Constant Summary
Constants included from Helper
Helper::AUTHENTICATION_ERROR_MESSAGE
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Array - can be JSON, array of hashes or array of Knowtify::Contact objects.
-
#contacts ⇒ Object
Array - can be JSON, array of hashes or array of Knowtify::Contact objects.
-
#http_request_options ⇒ Object
Array - can be JSON, array of hashes or array of Knowtify::Contact objects.
-
#invalid_contacts ⇒ Object
Array - can be JSON, array of hashes or array of Knowtify::Contact objects.
-
#response ⇒ Object
Array - can be JSON, array of hashes or array of Knowtify::Contact objects.
Attributes included from Helper
Instance Method Summary collapse
- #add(contact) ⇒ Object (also: #add_contact)
- #delete ⇒ Object
- #emails ⇒ Object
-
#initialize(contacts = [], opts = {}) ⇒ Contacts
constructor
A new instance of Contacts.
- #save ⇒ Object
- #to_hash ⇒ Object
- #to_json ⇒ Object
- #valid?(for_delete = false) ⇒ Boolean
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_key ⇒ Object
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 |
#contacts ⇒ Object
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_options ⇒ Object
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 end |
#invalid_contacts ⇒ Object
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 |
#response ⇒ Object
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 |
#delete ⇒ Object
70 71 72 73 |
# File 'lib/knowtify/contacts.rb', line 70 def delete @response = client.contacts_delete(emails,,api_key) if valid?(true) valid?(true) end |
#emails ⇒ Object
66 67 68 |
# File 'lib/knowtify/contacts.rb', line 66 def emails contacts.collect(&:email) end |
#save ⇒ Object
61 62 63 64 |
# File 'lib/knowtify/contacts.rb', line 61 def save @response = client.contacts_create_or_update(contacts.collect(&:to_hash),,api_key) if valid? valid? end |
#to_hash ⇒ Object
35 36 37 |
# File 'lib/knowtify/contacts.rb', line 35 def to_hash {'contacts' => contacts.collect(&:to_hash)} end |
#to_json ⇒ Object
39 40 41 |
# File 'lib/knowtify/contacts.rb', line 39 def to_json to_hash.to_json end |
#valid?(for_delete = false) ⇒ 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 |