Class: Knowtify::Contact

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/knowtify/contact.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(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_keyObject

String



4
5
6
# File 'lib/knowtify/contact.rb', line 4

def api_key
  @api_key
end

#dataObject

String



4
5
6
# File 'lib/knowtify/contact.rb', line 4

def data
  @data
end

#emailObject

String



4
5
6
# File 'lib/knowtify/contact.rb', line 4

def email
  @email
end

#http_request_optionsObject

String



4
5
6
# File 'lib/knowtify/contact.rb', line 4

def http_request_options
  @http_request_options
end

#nameObject

String



4
5
6
# File 'lib/knowtify/contact.rb', line 4

def name
  @name
end

#responseObject

String



4
5
6
# File 'lib/knowtify/contact.rb', line 4

def response
  @response
end

Instance Method Details

#data?Boolean

Returns:

  • (Boolean)


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

def data?
  @data.empty?
end

#deleteObject



78
79
80
81
# 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?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/knowtify/contact.rb', line 31

def email?
  !@email.nil?
end

#name?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/knowtify/contact.rb', line 23

def name?
  !@name.nil?
end

#saveObject



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],http_request_options,api_key)
  end
  valid?
end

#to_hashObject



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_jsonObject



50
51
52
# File 'lib/knowtify/contact.rb', line 50

def to_json
  to_hash.to_json
end

#valid?(for_delete = false) ⇒ Boolean

Returns:

  • (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