Class: ConstantContact::Base

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

Direct Known Subclasses

Activity, Campaign, CampaignEvent::CampaignEventBase, Contact, ContactEvent::ContactEventBase, EmailAddress, Event, List, Member

Constant Summary

DATE_FORMAT =
"%Y-%m-%dT%H:%M:%SZ"

Class Method Summary (collapse)

Instance Method Summary (collapse)

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method_symbol, *arguments)

Slightly tweaked ARes::Base's implementation so all the attribute names are looked up using camelcase since that's how the CC API returns them.



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/constant_contact/base.rb', line 81

def method_missing(method_symbol, *arguments) #:nodoc:
  method_name = method_symbol.to_s

  case method_name[-1,1]
    when "="
      attributes[method_name.chop.camelize] = arguments.first
    when "?"
      attributes[method_name.chop.camelize]
    else
      attributes.has_key?(method_name.camelize) ? attributes[method_name.camelize] : super
  end
end

Class Method Details

+ (Object) api_key



17
18
19
20
21
22
23
# File 'lib/constant_contact/base.rb', line 17

def api_key
  if defined?(@api_key)
    @api_key
  elsif superclass != Object && superclass.api_key
    superclass.api_key.dup.freeze
  end
end

+ (Object) api_key=(api_key)



25
26
27
28
# File 'lib/constant_contact/base.rb', line 25

def api_key=(api_key)
  @connection = nil
  @api_key = api_key
end

+ (Object) collection_path(prefix_options = {}, query_options = nil)



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

def collection_path(prefix_options = {}, query_options = nil)
  prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  "/ws/customers/#{self.user}#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
end

+ (Object) connection(refresh = false)



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/constant_contact/base.rb', line 30

def connection(refresh = false)
  if defined?(@connection) || superclass == Object
    @connection = ActiveResource::Connection.new(site, format) if refresh || @connection.nil?
    @connection.user = "#{api_key}%#{user}" if user
    @connection.password = password if password
    @connection.timeout = timeout if timeout
    @connection
  else
    superclass.connection
  end
end

+ (Object) element_path(id, prefix_options = {}, query_options = nil)



47
48
49
50
51
52
# File 'lib/constant_contact/base.rb', line 47

def element_path(id, prefix_options = {}, query_options = nil)
  prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  integer_id = parse_id(id)
  id_val = integer_id.zero? ? nil : "/#{integer_id}"
  "#{collection_path}#{id_val}#{query_string(query_options)}"
end

+ (Object) find_every(options)

Slight modification to AR::Base.find_every to handle instances where a single element is returned. This enables calling <tt>find(:first, => {:email => 'sample@example.com'})



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/constant_contact/base.rb', line 57

def find_every(options)
  case from = options[:from]
  when Symbol
    instantiate_collection(get(from, options[:params]))
  when String
    path = "#{from}#{query_string(options[:params])}"
    instantiate_collection(connection.get(path, headers) || [])
  else
    prefix_options, query_options = split_options(options[:params])
    path = collection_path(prefix_options, query_options)
    result = connection.get(path, headers)
    case result
    when Hash
      instantiate_collection( [ result ], prefix_options )
    else
      instantiate_collection( (result || []), prefix_options )
    end
  end
end

+ (Object) parse_id(url)

Returns an integer which can be used in #find calls. Assumes url structure with the id at the end, e.g.:

http://api.constantcontact.com/ws/customers/yourname/contacts/29


13
14
15
# File 'lib/constant_contact/base.rb', line 13

def parse_id(url)
  url.to_s.split('/').last.to_i
end

Instance Method Details

- (Object) after_save



119
120
# File 'lib/constant_contact/base.rb', line 119

def after_save
end

- (Object) before_save



116
117
# File 'lib/constant_contact/base.rb', line 116

def before_save
end

- (Object) encode



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/constant_contact/base.rb', line 132

def encode
  "<entry xmlns=\"http://www.w3.org/2005/Atom\">
    <title type=\"text\"> </title>
    <updated>#{Time.now.strftime(DATE_FORMAT)}</updated>
    <author></author>
    <id>#{id.blank? ? 'data:,none' : id}</id>
    <summary type=\"text\">#{self.class.name.split('::').last}</summary>
    <content type=\"application/vnd.ctct+xml\">
    #{self.to_xml}
    </content>
  </entry>"
end

- (Object) html_encode(txt)

TODO: Move this out to a lib



146
147
148
149
# File 'lib/constant_contact/base.rb', line 146

def html_encode(txt)
  mapping = { '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;' }
  txt.to_s.gsub(/[&"><]/) { |special| mapping[special] }
end

- (Object) int_id

Caching accessor for the the id integer



95
96
97
# File 'lib/constant_contact/base.rb', line 95

def int_id
  @id ||= self.class.parse_id(self.attributes['id'])
end

- (Object) save

Mimic ActiveRecord (snagged from HyperactiveResource).



108
109
110
111
112
113
114
# File 'lib/constant_contact/base.rb', line 108

def save
  return false unless valid?
  before_save    
  successful = super
  after_save if successful
  successful
end

- (Object) update_attributes(atts = {})

Mimics ActiveRecord's version



100
101
102
103
104
105
# File 'lib/constant_contact/base.rb', line 100

def update_attributes(atts={})
  camelcased_hash = {}
  atts.each{|key, val| camelcased_hash[key.to_s.camelize] = val}
  self.attributes.update(camelcased_hash)
  save
end

- (Boolean) valid?

So client-side validations run



126
127
128
129
130
# File 'lib/constant_contact/base.rb', line 126

def valid? 
  errors.clear
  validate 
  super 
end

- (Object) validate



122
123
# File 'lib/constant_contact/base.rb', line 122

def validate      
end