Class: Emailvision::Api

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/emailvision/api.rb

Constant Summary collapse

HTTP_VERBS =

HTTP verbs allowed to trigger a call-chain

[:get, :post].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) {|_self| ... } ⇒ Api

Returns a new instance of Api.

Yields:

  • (_self)

Yield Parameters:



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/emailvision/api.rb', line 17

def initialize(params = {})      
  yield(self) if block_given?      
  
  self.server_name ||= params[:server_name]  || self.class.server_name
  self.endpoint    ||= params[:endpoint]     || self.class.endpoint
  self.       ||= params[:login]        || self.class.
  self.password    ||= params[:password]     || self.class.password
  self.key         ||= params[:key]          || self.class.key
  self.token       ||= params[:token]        || self.class.token
  self.debug       ||= params[:debug]        || self.class.debug      
end

Class Attribute Details

.debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

.endpointObject

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

.keyObject

Returns the value of attribute key.



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

def key
  @key
end

.loginObject

Returns the value of attribute login.



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

def 
  @login
end

.passwordObject

Returns the value of attribute password.



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

def password
  @password
end

.server_nameObject

Returns the value of attribute server_name.



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

def server_name
  @server_name
end

.tokenObject

Returns the value of attribute token.



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

def token
  @token
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

#endpointObject

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#loginObject

Returns the value of attribute login.



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

def 
  @login
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#server_nameObject

Returns the value of attribute server_name.



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

def server_name
  @server_name
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#base_uriObject

Base uri



169
170
171
# File 'lib/emailvision/api.rb', line 169

def base_uri
  "http://#{server_name}/#{endpoint}/services/rest/"
end

#call(http_verb, method, parameters = {}) ⇒ Object

Perform the API call http_verb = (get, post, …) method = Method to call parameters = Parameters to send (optionnal)



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/emailvision/api.rb', line 71

def call(http_verb, method, parameters = {})
  params ||= {}      

  # == Check presence of these essential attributes ==
  unless server_name and endpoint
    raise Emailvision::Exception.new "Cannot make an API call without a server name and an endpoint !"
  end

  # == Sanitize parameters ==
  parameters = Emailvision::Tools.sanitize_parameters(parameters)

  retries = 2
  begin
    # == Build uri ==
    uri = base_uri + method
    if parameters[:uri]
      uri += token ? "/#{token}/" : '/'
      uri += (parameters[:uri].respond_to?(:join) ? parameters[:uri] : [parameters[:uri]]).compact.join '/'
      parameters.delete :uri
    elsif parameters[:body]
      uri += token ? "/#{token}/" : '/'
    else
      parameters[:token] = token
    end
    
    # == Build body ==
    # 1. Extract body from parameters
    body = parameters[:body] || {}
    parameters.delete :body
    # 2. Camelize all keys
    body = Emailvision::Tools.r_camelize body
    # 3. Convert to xml
    body_xml = Emailvision::Tools.to_xml_as_is body
  
    # == Send request ==      
    logger.send "#{uri} with query : #{parameters} and body : #{body}"
    response = self.class.send http_verb, uri, :query => parameters, :body => body_xml, :timeout => 30      
  
    # == Parse response ==
    http_code = response.header.code
    content = {}
    begin
      content = Crack::XML.parse response.body
    rescue MultiXml::ParseError => e
      logger.send "#{uri} Error when parsing response body (#{e.to_s})"
    end
    logger.receive content.inspect

    # Return response or raise an exception if request failed
    if (http_code == "200") and (content and content["response"])
      response = content["response"]["result"] || content["response"] 
    else        
      raise Emailvision::Exception.new "#{http_code} - #{content}"
    end

  return response

  rescue Emailvision::Exception => e
    if e.message =~ /Your session has expired/ or e.message =~ /The maximum number of connection allowed per session has been reached/
      self.close_connection          
      self.open_connection
      if((retries -= 1) >= 0)
        retry
      else
        raise e
      end
    else
      raise e          
    end
  rescue Errno::ECONNRESET => e
    if((retries -= 1) >= 0)
      retry
    else
      raise e
    end
  rescue Timeout::Error => e
    if((retries -= 1) >= 0)
      retry
    else
      raise e
    end
  end
end

#close_connectionObject

Logout from Emailvision API Return :

  • True if the connection has been destroyed

  • False if the connection cannot be destroyed or has already been destroyed



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/emailvision/api.rb', line 45

def close_connection
  if connected?
    get.connect.close.call
  else
    return false
  end
rescue Emailvision::Exception => e
ensure
  invalidate_token!
  not connected?
end

#connected?Boolean

Check whether the connection has been established or not

Returns:

  • (Boolean)


58
59
60
# File 'lib/emailvision/api.rb', line 58

def connected?
  !token.nil?
end

#invalidate_token!Object



62
63
64
# File 'lib/emailvision/api.rb', line 62

def invalidate_token!
  self.token = nil
end

#open_connectionObject

Login to Emailvision API Return :

  • True if the connection has been established

  • False if the connection cannot be established or has already been established



35
36
37
38
39
# File 'lib/emailvision/api.rb', line 35

def open_connection
  return false if connected?
  self.token = get.connect.open.call :login => @login, :password => @password, :key => @key
  connected?
end