Class: CakeMail::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/cakemail/session.rb

Defined Under Namespace

Classes: ApiRequestError, HttpRequestError

Constant Summary collapse

API_SERVER_BASE_URL =
"http://api.cakemail.com/"
API_VERSION =
"1.0"
API_LOCALE =
"en_US"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_id, api_key) ⇒ Session

Returns a new instance of Session.



12
13
14
15
16
# File 'lib/cakemail/session.rb', line 12

def initialize(api_id, api_key)
  @api_id = api_id
  @api_key = api_key
  @crypto = Crypt::Blowfish.new(@api_key)
end

Instance Attribute Details

#api_idObject (readonly)

Returns the value of attribute api_id.



10
11
12
# File 'lib/cakemail/session.rb', line 10

def api_id
  @api_id
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/cakemail/session.rb', line 10

def api_key
  @api_key
end

#user_keyObject (readonly)

Returns the value of attribute user_key.



10
11
12
# File 'lib/cakemail/session.rb', line 10

def user_key
  @user_key
end

Instance Method Details

#login(email, password) ⇒ Object

Returns a logged in user.



18
19
20
21
# File 'lib/cakemail/session.rb', line 18

def (email, password)
  user = CakeMail::User.(self, email, password)
  return user
end

#password_recovery(email) ⇒ Object

Recover password.



23
24
25
# File 'lib/cakemail/session.rb', line 23

def password_recovery(email)
  CakeMail::User.password_recovery(email)
end

#request(service_class, method, args) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cakemail/session.rb', line 27

def request(service_class, method, args)
  builder = Builder::XmlMarkup.new(:indent => 1)
  builder.instruct! :xml, :version => "1.0", :encoding => "utf-8"
  req = builder.body(:version => API_VERSION) { |body| Object.module_eval(service_class).method_xml(body, self, method, args) }
  res = Net::HTTP.post_form(URI.parse(API_SERVER_BASE_URL), {'alg' => 'blowfish', 'mode' => 'ecb', 'id' => @api_id, 'request' => encode(req)})
  raise HttpRequestError if res.nil? || res.body.nil? || (res.code.to_i != 200)
  r = XmlSimple.xml_in(decode(res.body))
  raise ApiRequestError if r.nil? || !r.has_key?('class') || !r['class'].is_a?(Array)
  r = r['class'].first
  raise ApiRequestError if (r['type'] != service_class.gsub(/.*::/, '')) ||
  !r['method'].is_a?(Array) || (r['method'].first['type'] != method)
  r = r['method'].first
  r = unescape_xmlsimple_result(r)
  if r.has_key?('error_code')
    error = "Error #{r['error_code'].first}"
    error += ": #{CGI.unescape(r['error_message'].first)}" if r.has_key?('error_message')
    error += " [#{service_class.to_s} :: #{method}]"
    raise ApiRequestError.new(error)
  end
  return r
end