Class: Knowtify::Contact
- Inherits:
-
Object
- Object
- Knowtify::Contact
- Includes:
- Helper
- Defined in:
- lib/knowtify/contact.rb
Constant Summary
Constants included from Helper
Helper::AUTHENTICATION_ERROR_MESSAGE
Instance Attribute Summary collapse
-
#api_key ⇒ Object
String.
-
#data ⇒ Object
String.
-
#email ⇒ Object
String.
-
#http_request_options ⇒ Object
String.
-
#name ⇒ Object
String.
-
#response ⇒ Object
String.
Attributes included from Helper
Instance Method Summary collapse
- #data? ⇒ Boolean
- #delete ⇒ Object
- #email? ⇒ Boolean
-
#initialize(args = {}) ⇒ Contact
constructor
A new instance of Contact.
- #name? ⇒ Boolean
- #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(args = {}) ⇒ Contact
Returns a new instance of Contact.
11 12 13 14 15 16 17 |
# File 'lib/knowtify/contact.rb', line 11 def initialize(args={}) args = JSON.parse(args) if args.is_a?(String) args.each do |meth,val| send("#{meth}=",val) end @http_request_options ||= {} end |
Instance Attribute Details
#api_key ⇒ Object
String
4 5 6 |
# File 'lib/knowtify/contact.rb', line 4 def api_key @api_key end |
#data ⇒ Object
String
4 5 6 |
# File 'lib/knowtify/contact.rb', line 4 def data @data end |
#email ⇒ Object
String
4 5 6 |
# File 'lib/knowtify/contact.rb', line 4 def email @email end |
#http_request_options ⇒ Object
String
4 5 6 |
# File 'lib/knowtify/contact.rb', line 4 def @http_request_options end |
#name ⇒ Object
String
4 5 6 |
# File 'lib/knowtify/contact.rb', line 4 def name @name end |
#response ⇒ Object
String
4 5 6 |
# File 'lib/knowtify/contact.rb', line 4 def response @response end |
Instance Method Details
#data? ⇒ Boolean
39 40 41 |
# File 'lib/knowtify/contact.rb', line 39 def data? @data.empty? end |
#delete ⇒ Object
78 79 80 81 |
# File 'lib/knowtify/contact.rb', line 78 def delete @response = client.contacts_delete([email],,api_key) if valid?(true) valid?(true) end |
#email? ⇒ Boolean
31 32 33 |
# File 'lib/knowtify/contact.rb', line 31 def email? !@email.nil? end |
#name? ⇒ Boolean
23 24 25 |
# File 'lib/knowtify/contact.rb', line 23 def name? !@name.nil? end |
#save ⇒ Object
71 72 73 74 75 76 |
# File 'lib/knowtify/contact.rb', line 71 def save if valid? @response = client.contacts_create_or_update([to_hash],,api_key) end valid? end |
#to_hash ⇒ Object
43 44 45 46 47 48 |
# File 'lib/knowtify/contact.rb', line 43 def to_hash hash = { 'name' => name, 'email' => email } hash['data'] = data unless data.empty? hash end |
#to_json ⇒ Object
50 51 52 |
# File 'lib/knowtify/contact.rb', line 50 def to_json to_hash.to_json end |
#valid?(for_delete = false) ⇒ Boolean
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/knowtify/contact.rb', line 54 def valid?(for_delete=false) @errors = [] # reset @errors << "Email is blank." unless email? if @response if @response.authentication_error? add_authenication_error else if parsed_response['contacts_errored'] > 0 action = (for_delete ? 'delete' : 'create/update') Knowtify.logger.error "Knowtify contacts #{action} operation failed: #{response.body}." if Knowtify.logger @errors << "Contact failed to #{action}" end end end @errors.empty? end |