Class: Icontact::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/icontact/api.rb

Constant Summary collapse

VERSION =
"0.3"
API_VERSION =
"2.2"
URL =
'https://app.icontact.com/icp'
API_KEY =
"API_KEY"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username = nil, password = nil) ⇒ Api

Returns a new instance of Api.



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

def initialize(username=nil, password=nil)
  self.username = username
  self.password = password
  self.url = URL
  self.app_id = API_KEY
  self.api_version = API_VERSION
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



15
16
17
# File 'lib/icontact/api.rb', line 15

def api_version
  @api_version
end

#app_idObject

Returns the value of attribute app_id.



13
14
15
# File 'lib/icontact/api.rb', line 13

def app_id
  @app_id
end

#passwordObject

Returns the value of attribute password.



12
13
14
# File 'lib/icontact/api.rb', line 12

def password
  @password
end

#urlObject

Returns the value of attribute url.



14
15
16
# File 'lib/icontact/api.rb', line 14

def url
  @url
end

#usernameObject

Returns the value of attribute username.



11
12
13
# File 'lib/icontact/api.rb', line 11

def username
  @username
end

Class Method Details

.body_encoder(data) ⇒ Object

override this to use a different encoding method for sending data (like xml)



43
44
45
# File 'lib/icontact/api.rb', line 43

def self.body_encoder(data)
  data.to_json
end

.package_query_params(params = {}) ⇒ Object

Package up any options into a query string and format it properly for the server arrays become comma separated lists and Times are expressed as iso8601



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/icontact/api.rb', line 27

def self.package_query_params(params={})
  return nil if params.nil? || params.empty?
  massaged_params = params.map do |key, value|
    case value
      when Array
        "#{key}=#{value.join(',')}"
      when Time
        "#{key}=#{value.strftime("%Y-%m-%dT%H:%M:%S")}#{"%+0.2d:00" % (value.gmtoff / 3600)}"
      else
        "#{key}=#{value}"
    end
  end
  "?#{massaged_params.join('&')}"
end

.parse_response(code, response) ⇒ Object

override this method to use a different response parser (like REXML for an xml response) when the response is not actually a JSON object an exception is thrown and the raw response is returned in the body instead.



49
50
51
# File 'lib/icontact/api.rb', line 49

def self.parse_response(code, response)
  {'code'=>code, 'body' => (JSON.parse(response) rescue response)}
end

Instance Method Details

#apply_headers(req) ⇒ Object

populate headers required by the icontact server on each request for authentication Accept and Content-Type are set to application/json to use JSON objects for the data exchange. Also accepts text/xml for either, but then you have to deal with XML encoding and decoding manually



57
58
59
60
61
62
63
64
65
# File 'lib/icontact/api.rb', line 57

def apply_headers(req)
  req.add_field('API-Version', self.api_version)
  req.add_field('accept','application/json')
  req.add_field('Content-Type','application/json')
  req.add_field('API-Appid', self.app_id)
  req.add_field('API-Username', self.username)
  req.add_field('API-Password', self.password)
  return req
end

#request(kind, url, data, options) ⇒ Object

Actually make the get, put, post, or delete request



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/icontact/api.rb', line 82

def request(kind, url, data, options)
  # options passed as optional parameter show up as an array
  options = options.first if options.kind_of? Array
  query_options = self.class.package_query_params(options)
  full_url = URI.parse("#{self.url}#{url}#{query_options}")

  # create an object of the class required to process this method
  klass = Object.module_eval("::Net::HTTP::#{kind.to_s.capitalize}", __FILE__, __LINE__)
  request = klass.new([full_url.path, full_url.query].compact.join('?'))
  request = apply_headers(request)
  # take passed data and encode it
  request.body = self.class.body_encoder(data) if data

  http = Net::HTTP.new(full_url.host, full_url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  response = http.start do |web|
    web.request(request)
  end
  return self.class.parse_response(response.code, response.body)
end