Class: ConstantContact::Base

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

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

#method_missing(method_symbol, *arguments, &block) ⇒ Object (protected)

support underscore accessors to CamelCase the attributes hash



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/constantcontact/base.rb', line 147

def method_missing(method_symbol, *arguments, &block) #:nodoc:
  method_name = method_symbol.to_s
  if method_name =~ /(=|\?)$/
    case $1
    when "="
      attributes[search_attributes($`)] = arguments.first
    when "?"
      attributes[search_attributes($`)]
    end
  else
    key = search_attributes(method_name)
    return attributes[key] if attributes.has_key?(key)
    super
  end
end

Class Method Details

.api_keyObject

Gets the api_key for REST HTTP authentication.



32
33
34
35
36
37
38
39
# File 'lib/constantcontact/base.rb', line 32

def api_key
  # Not using superclass_delegating_reader. See +site+ for explanation
  if defined?(@api_key)
    @api_key
  elsif superclass != Object && superclass.api_key
    superclass.api_key.dup.freeze
  end
end

.api_key=(api_key) ⇒ Object

Sets the api_key for REST HTTP authentication.



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

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

.collection_path(prefix_options = {}, query_options = nil) ⇒ Object



65
66
67
68
69
# File 'lib/constantcontact/base.rb', line 65

def collection_path(prefix_options = {}, query_options = nil)
  check_prefix_options(prefix_options)
  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

.connection(refresh = false) ⇒ Object

An instance of ActiveResource::Connection that is the base connection to the remote service. The refresh parameter toggles whether or not the connection is refreshed at every request or not (defaults to false).



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/constantcontact/base.rb', line 50

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

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



78
79
80
81
82
83
84
# File 'lib/constantcontact/base.rb', line 78

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)}"
  "/ws/customers/#{self.user}#{prefix(prefix_options)}#{collection_name}/#{URI.parser.escape id.to_s}#{query_string(query_options)}"
end

.parse_id(url) ⇒ Object

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


74
75
76
# File 'lib/constantcontact/base.rb', line 74

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

Instance Method Details

#to_atom(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/constantcontact/base.rb', line 6

def to_atom(options={})
  "<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