Class: Mailinabox::Api::Client
- Inherits:
-
Object
- Object
- Mailinabox::Api::Client
- Includes:
- Endpoints::Users
- Defined in:
- lib/mailinabox/api/client.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#connection ⇒ Object
Returns the value of attribute connection.
Instance Method Summary collapse
- #evaluate_response(method, response) ⇒ Object
- #get(path, params: {}, headers: {}, options: {}) ⇒ Object
- #init_connection(headers: {}, options: {}) ⇒ Object
-
#initialize(configuration: ::Mailinabox::Api.configuration) ⇒ Client
constructor
A new instance of Client.
- #post(path, params: {}, data: {}, headers: {}, options: {}) ⇒ Object
- #request(path, method: :get, params: {}, data: {}, headers: {}, options: {}, retries: 3) ⇒ Object
Methods included from Endpoints::Users
#add_user, #add_user_privilege, #remove_user, #remove_user_privilege, #users
Constructor Details
#initialize(configuration: ::Mailinabox::Api.configuration) ⇒ Client
Returns a new instance of Client.
7 8 9 10 |
# File 'lib/mailinabox/api/client.rb', line 7 def initialize(configuration: ::Mailinabox::Api.configuration) self.configuration = configuration self.init_connection end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
5 6 7 |
# File 'lib/mailinabox/api/client.rb', line 5 def configuration @configuration end |
#connection ⇒ Object
Returns the value of attribute connection.
5 6 7 |
# File 'lib/mailinabox/api/client.rb', line 5 def connection @connection end |
Instance Method Details
#evaluate_response(method, response) ⇒ Object
62 63 64 |
# File 'lib/mailinabox/api/client.rb', line 62 def evaluate_response(method, response) response&.to_s&.strip&.downcase&.eql?(Mailinabox::Api::Constants::RESPONSES[method]) end |
#get(path, params: {}, headers: {}, options: {}) ⇒ Object
29 30 31 |
# File 'lib/mailinabox/api/client.rb', line 29 def get(path, params: {}, headers: {}, options: {}) request path, method: :get, params: params, headers: headers, options: end |
#init_connection(headers: {}, options: {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mailinabox/api/client.rb', line 14 def init_connection(headers: {}, options: {}) headers["User-Agent"] = .fetch(:user_agent, self.configuration.faraday.fetch(:user_agent, nil)) self.connection ||= ::Faraday.new(url: self.configuration.host) do |builder| builder.headers = headers builder.basic_auth(self.configuration.user, self.configuration.password) builder.request :url_encoded builder.response :logger if self.configuration.verbose_faraday? builder.adapter self.configuration.faraday.fetch(:adapter, :net_http) end end |
#post(path, params: {}, data: {}, headers: {}, options: {}) ⇒ Object
33 34 35 |
# File 'lib/mailinabox/api/client.rb', line 33 def post(path, params: {}, data: {}, headers: {}, options: {}) request path, method: :post, params: params, data: data, headers: headers, options: end |
#request(path, method: :get, params: {}, data: {}, headers: {}, options: {}, retries: 3) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mailinabox/api/client.rb', line 37 def request(path, method: :get, params: {}, data: {}, headers: {}, options: {}, retries: 3) response = nil begin response = case method when :get, :head, :options self.connection.send(method) do |request| request.url path request.params = params if params && !params.empty? end when :post, :put, :patch, :delete self.connection.send(method) do |request| request.url path request.body = data if data && !data.empty? request.params = params if params && !params.empty? end end rescue Faraday::TimeoutError, Faraday::ConnectionFailed => e retries -= 1 retry if retries > 0 end return response&.body end |