Class: EmailVision::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/email_vision/base_client.rb

Direct Known Subclasses

Campaign, Member, Message

Constant Summary collapse

SESSION_TIMEOUT =
10*60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ BaseClient

Returns a new instance of BaseClient.



7
8
9
10
11
# File 'lib/email_vision/base_client.rb', line 7

def initialize(options)
  self.options  = options
  @wsdl         = options.delete(:wsdl)
  @namespace    = options.delete(:namespace)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/email_vision/base_client.rb', line 5

def options
  @options
end

Instance Method Details

#api_namespaced(method) ⇒ Object



65
66
67
# File 'lib/email_vision/base_client.rb', line 65

def api_namespaced(method)
  "api:#{method.to_s.gsub(/_./){|x| x.slice(1,1).upcase }}"
end

#check_server_status!(e, status) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/email_vision/base_client.rb', line 69

def check_server_status!(e, status)
  fault = e.to_hash[:fault]
  if fault && fault[:faultcode] == "soap:Server" && status === fault[:detail].first.last[:status]
    block_given? ? yield(fault[:detail][:status]) : true
  else
    raise e
  end
end

#clientObject



18
19
20
# File 'lib/email_vision/base_client.rb', line 18

def client
  @client ||= Savon::Client.new(@wsdl)
end

#connect!Object



26
27
28
29
30
31
32
33
34
# File 'lib/email_vision/base_client.rb', line 26

def connect!
  @token = request_without_protection(:open_api_connection,
    :login => options[:login],
    :pwd => options[:password],
    :key => options[:key],
    :soap_client => client
  )
  @token_requested = Time.now.to_i
end

#connected?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/email_vision/base_client.rb', line 22

def connected?
  @token and @token_requested > (Time.now.to_i - SESSION_TIMEOUT)
end

#connectionObject



13
14
15
16
# File 'lib/email_vision/base_client.rb', line 13

def connection
  connect! unless connected?
  client
end

#execute(method, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/email_vision/base_client.rb', line 36

def execute(method, options={})
  request_without_protection(method, options.merge(:token => true))
rescue Object => e
  if e.respond_to?(:http) and e.http.respond_to?(:body)
    retries ||= -1
    retries += 1
    session_error = (e.http.body =~ /status>(SESSION_RETRIEVING_FAILED|CHECK_SESSION_FAILED)</)
    if session_error
      if retries < 1
        connect!
        retry
      else
        e.message << " -- retried #{retries}"
      end
    end
  end
  raise e
end

#request_without_protection(method, options) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/email_vision/base_client.rb', line 55

def request_without_protection(method, options)
  client = options.delete(:soap_client) || connection
  response = client.request(api_namespaced(method)) do |r|
    r.namespaces['xmlns:api'] = @namespace
    options[:token] = @token if options[:token] # token first is generated via connection method
    r.body = options
  end
  response.to_hash["#{method}_response".to_sym][:return]
end