Class: Emailvision::Api
- Inherits:
-
Object
- Object
- Emailvision::Api
- Includes:
- HTTParty
- Defined in:
- lib/emailvision/api.rb
Overview
This is where the communication with the API is made.
Constant Summary collapse
- HTTP_VERBS =
HTTP verbs allowed to trigger a call-chain
[:get, :post].freeze
- ATTRIBUTES =
[:token, :server_name, :endpoint, :login, :password, :key, :debug].freeze
Instance Method Summary collapse
-
#base_uri ⇒ Object
Base uri.
-
#call(http_verb, method, parameters = {}) ⇒ Object
Perform an API call.
-
#close_connection ⇒ Boolean
Logout from Emailvision API.
-
#connected? ⇒ Boolean
Check whether the connection has been established or not.
-
#endpoint=(value) ⇒ Object
Change API endpoint.
-
#initialize(params = {}) { ... } ⇒ Api
constructor
Initialize.
-
#invalidate_token! ⇒ Object
When a token is no longer valid, this method can be called.
-
#open_connection ⇒ Boolean
Login to Emailvision API.
-
#token=(value) ⇒ Object
Set a new token.
Constructor Details
#initialize(params = {}) { ... } ⇒ Api
Initialize
26 27 28 29 |
# File 'lib/emailvision/api.rb', line 26 def initialize(params = {}) yield(self) if block_given? assign_attributes(params) end |
Instance Method Details
#base_uri ⇒ Object
Base uri
143 144 145 |
# File 'lib/emailvision/api.rb', line 143 def base_uri "http://#{server_name}/#{endpoint}/services/rest/" end |
#call(http_verb, method, parameters = {}) ⇒ Object
Perform an API call
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 |
# File 'lib/emailvision/api.rb', line 81 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 uri = prepare_uri(method, parameters) body = prepare_body(parameters) logger.send "#{uri} with query : #{parameters} and body : #{body}" response = perform_request(http_verb, uri, parameters, body) extract_response(response) rescue Emailvision::Exception => e if e. =~ /Your session has expired/ or e. =~ /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, Timeout::Error => e if((retries -= 1) >= 0) retry else raise e end end end |
#close_connection ⇒ Boolean
Logout from Emailvision API
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/emailvision/api.rb', line 47 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
63 64 65 |
# File 'lib/emailvision/api.rb', line 63 def connected? !token.nil? end |
#endpoint=(value) ⇒ Object
Change API endpoint. This will close the connection to the current endpoint
136 137 138 139 |
# File 'lib/emailvision/api.rb', line 136 def endpoint=(value) close_connection @endpoint = value end |
#invalidate_token! ⇒ Object
When a token is no longer valid, this method can be called. The #connected? method will return false
70 71 72 |
# File 'lib/emailvision/api.rb', line 70 def invalidate_token! self.token = nil end |
#open_connection ⇒ Boolean
Login to Emailvision API
37 38 39 40 41 |
# File 'lib/emailvision/api.rb', line 37 def open_connection return false if connected? self.token = get.connect.open.call :login => @login, :password => @password, :key => @key connected? end |
#token=(value) ⇒ Object
Set a new token
127 128 129 |
# File 'lib/emailvision/api.rb', line 127 def token=(value) @token = value end |