class Virtuaservices::Decorators::Gateway
Decorator for a service, providing methods to make requests on it. @author Vincent Courtois <courtois.vincent@outlook.com>
Attributes
@!attribute [rw] action
@return [String] the action of the route using this API.
Public Class Methods
# File lib/virtuaservices/decorators/gateway.rb, line 14 def initialize(action, _object) super(_object) @logger = Logger.new(STDOUT) @action = action end
Public Instance Methods
Shortcut to make a DELETE request on the API. @param session [Virtuaservices::Authentication::Session] the session of the user requesting the API. @param url [String] the URL you want to reach on the service. @param params [Hash] the additional parameters to pass in the JSON body.
# File lib/virtuaservices/decorators/gateway.rb, line 24 def delete(session:, url:, params:) return make_request_without_body(verb: 'delete', session: session, url: url, params: params) end
Shortcut to make a GET request on the API. @param session [Virtuaservices::Authentication::Session] the session of the user requesting the API. @param url [String] the URL you want to reach on the service. @param params [Hash] the additional parameters to pass in the JSON body.
# File lib/virtuaservices/decorators/gateway.rb, line 32 def get(session:, url:, params:) return make_request_without_body(verb: 'get', session: session, url: url, params: params) end
Shortcut to make a POST request on the API. @param session [Virtuaservices::Authentication::Session] the session of the user requesting the API. @param url [String] the URL you want to reach on the service. @param params [Hash] the additional parameters to pass in the JSON body.
# File lib/virtuaservices/decorators/gateway.rb, line 40 def post(session:, url:, params:) return make_request(verb: 'post', session: session, url: url, params: params) end
Shortcut to make a PUT request on the API. @param session [Virtuaservices::Authentication::Session] the session of the user requesting the API. @param url [String] the URL you want to reach on the service. @param params [Hash] the additional parameters to pass in the JSON body.
# File lib/virtuaservices/decorators/gateway.rb, line 48 def put(session:, url:, params:) return make_request(verb: 'put', session: session, url: url, params: params) end
Private Instance Methods
# File lib/virtuaservices/decorators/gateway.rb, line 91 def before_requests(session, params) if ENV['APP_KEY'].nil? raise Virtuaservices::Decorators::Errors::EnvVariableMissing.new(action: action) end params[:app_key] = ENV['APP_KEY'] params[:session_id] = session.token return params end
# File lib/virtuaservices/decorators/gateway.rb, line 100 def get_connection Faraday.new(object.url) do |faraday| faraday.request :url_encoded faraday.response :logger faraday.adapter Faraday.default_adapter end end
Makes a POST request to the given service with the following steps :
-
Gets an active and running instance of the service to make the request.
-
Creates a Faraday connection to use it as a pipeline for the request.
-
Makes the actual request and returns an object with the status and body of the response.
@param verb [String] the HTTP verb to use for this request. @param session [Virtuaservices::Authentication::Session] the session of the user requesting the API. @param url [String] the URL you want to reach on the service. @param params [Hash] the additional parameters to pass in the JSON body.
@return [Hash, Boolean] FALSE if no instance are found, or an object with :status and :body keys correspding
to the status and body of the response to the request
# File lib/virtuaservices/decorators/gateway.rb, line 66 def make_request(verb:, session:, url:, params:) params = before_requests(session, params) connection = get_connection response = connection.send(verb) do |req| req.url url req.headers['Content-Type'] = 'application/json' req.body = params.to_json end return { status: response.status, body: JSON.parse(response.body) } end
# File lib/virtuaservices/decorators/gateway.rb, line 82 def make_request_without_body(verb:, session:, url:, params:) params = before_requests(session, params) connection = get_connection response = connection.send(verb) do |req| req.url url, params req.headers['Content-Type'] = 'application/json' end end