class Knowtify::Contact

Attributes

api_key[RW]
data[RW]
email[RW]
http_request_options[RW]
name[RW]
response[RW]

Public Class Methods

new(args={}) click to toggle source
# 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

Public Instance Methods

data=(data) click to toggle source
# File lib/knowtify/contact.rb, line 35
def data=data
  @data = data unless blank_string?(data.to_s)
end
data?() click to toggle source
# File lib/knowtify/contact.rb, line 39
def data?
  @data.empty?
end
delete() click to toggle source
# File lib/knowtify/contact.rb, line 78
def delete
  @response = client.contacts_delete([email],http_request_options,api_key) if valid?(true)
  valid?(true)
end
email=(email) click to toggle source
# File lib/knowtify/contact.rb, line 27
def email=email
  @email=email unless blank_string?(email)
end
email?() click to toggle source
# File lib/knowtify/contact.rb, line 31
def email?
  !@email.nil?
end
name=(name) click to toggle source
# File lib/knowtify/contact.rb, line 19
def name=name
  @name=name unless blank_string?(name)
end
name?() click to toggle source
# File lib/knowtify/contact.rb, line 23
def name?
  !@name.nil?
end
save() click to toggle source
# File lib/knowtify/contact.rb, line 71
def save
  if valid?
    @response = client.contacts_create_or_update([to_hash],http_request_options,api_key)
  end
  valid?
end
to_hash() click to toggle source
# 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() click to toggle source
# File lib/knowtify/contact.rb, line 50
def to_json
  to_hash.to_json
end
valid?(for_delete=false) click to toggle source
# 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