Class: ConstantContact::Contact

Inherits:
Base
  • Object
show all
Defined in:
lib/constant_contact/contact.rb

Constant Summary

Constants inherited from Base

Base::DATE_FORMAT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#after_save, api_key, api_key=, #before_save, collection_path, connection, element_path, #encode, find_every, #html_encode, #int_id, #method_missing, parse_id, #save, #update_attributes, #valid?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ConstantContact::Base

Instance Attribute Details

#opt_in_sourceObject

Returns the value of attribute opt_in_source


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

def opt_in_source
  @opt_in_source
end

Class Method Details

.find_all_by_emails(emails) ⇒ Object

Needed a lot of custom work here because of the way ActiveResource turns arrays into query strings.

ActiveResource would do: email[]=<email 1>&email[]=<email 2>
But we need: email=<email 1>&email=<email 2>

Raises:

  • (ArgumentError)

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/constant_contact/contact.rb', line 79

def self.find_all_by_emails(emails)
  raise ArgumentError, "Expected an array of emails, got a(n) #{emails.class.name}" unless emails.is_a? Array
  return [] if emails.empty?

  query_string = emails.map{|e| "email=#{CGI.escape(e.to_s.downcase)}"}.join('&')

  path = "/ws/customers/#{self.user}/#{collection_name}?#{query_string}"
  result = connection.get(path, headers)

  case result
  when Hash
    instantiate_collection( [ result ], {} )
  else
    instantiate_collection( (result || []), {} )
  end
end

.find_by_email(email_address) ⇒ Object


71
72
73
# File 'lib/constant_contact/contact.rb', line 71

def self.find_by_email(email_address)
  find :first, {:params => {:email => email_address.downcase}}
end

Instance Method Details

#active?Boolean

Can we email them?


43
44
45
# File 'lib/constant_contact/contact.rb', line 43

def active?
  status.downcase == 'active'
end

#contact_listsObject


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/constant_contact/contact.rb', line 47

def contact_lists
  return @contact_lists if defined?(@contact_lists)
  # otherwise, attempt to assign it
  @contact_lists = if self.attributes.keys.include?('ContactLists')
    if self.ContactLists
      if self.ContactLists.ContactList.is_a?(Array)
        self.ContactLists.ContactList.collect { |list|
          ConstantContact::Base.parse_id(list.id)
        }
      else
        [ ConstantContact::Base.parse_id(self.ContactLists.ContactList.id) ]
      end
    else
      [] # => Contact is not a member of any lists (legitimatly empty!)
    end
  else
    nil
  end
end

#contact_lists=(val) ⇒ Object


67
68
69
# File 'lib/constant_contact/contact.rb', line 67

def contact_lists=(val)
  @contact_lists = val.kind_of?(Array) ? val : [val]
end

#list_url(id = nil) ⇒ Object


37
38
39
40
# File 'lib/constant_contact/contact.rb', line 37

def list_url(id=nil)
  id ||= defined?(self.list_id) ? self.list_id : 1
  "http://api.constantcontact.com/ws/customers/#{self.class.user}/lists/#{id}"
end

#to_xmlObject

NOTE: All column names are available only when finding a contact by id. @@column_names = [ :addr1, :addr2, :addr3, :city, :company_name, :country_code, :country_name,

:custom_field1, :custom_field10, :custom_field11, :custom_field12, :custom_field13, 
:custom_field14, :custom_field15, :custom_field2, :custom_field3, :custom_field4, :custom_field5, 
:custom_field6, :custom_field7, :custom_field8, :custom_field9, :email_address, :email_type, 
:first_name, :home_phone, :insert_time, :job_title, :last_name, :last_update_time, :name, :note, 
:postal_code, :state_code, :state_name, :status, :sub_postal_code, :work_phone ]

14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/constant_contact/contact.rb', line 14

def to_xml
  xml = Builder::XmlMarkup.new
  xml.tag!("Contact", :xmlns => "http://ws.constantcontact.com/ns/1.0/") do
    self.attributes.reject {|k,v| k == 'ContactLists'}.each{|k, v| xml.tag!( k.to_s.camelize, v )}
    xml.tag!("OptInSource", self.opt_in_source)
    xml.tag!("ContactLists") do
      @contact_lists = [1] if @contact_lists.nil? && self.new?
      self.contact_lists.sort.each do |list_id|
        xml.tag!("ContactList", :id=> self.list_url(list_id))
      end
    end
  end
end