Sha256: c84a57edc383c4c349495138d79327bbd02a1a9cffed6d5aa3299a6f14188dd7

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

require "faraday"
require "json"
require_relative "../version"
require_relative "../errors/error_map"

module Evervault
  module Http
    class Request
      def initialize(api_key:, base_url:, cage_run_url:, timeout:)
        @api_key = api_key
        @timeout = timeout
        @base_url = base_url
        @cage_run_url = cage_run_url
      end

      def get(path, params = nil)
        execute(:get, build_url(path), params)
      end

      def put(path, params)
        execute(:put, build_url(path), params)
      end

      def delete(path, params)
        execute(:delete, build_url(path), params)
      end

      def post(path, params, cage_run: false)
        execute(:post, build_url(path, cage_run), params)
      end

      private def build_url(path, cage_run = false)
        return "#{@base_url}#{path}" unless cage_run
        "#{@cage_run_url}#{path}"
      end

      def execute(method, url, params)
        resp = Faraday.send(method, url) do |req|
            req.body = params.nil? || params.empty? ? nil : params.to_json
            req.headers = build_headers
        end
        return JSON.parse(resp.body) if resp.status >= 200 && resp.status <= 300
        Evervault::Errors::ErrorMap.raise_errors_on_failure(resp.status, resp.body)
      end

      private def build_headers
        {
          "AcceptEncoding": "gzip, deflate",
          "Accept": "application/json",
          "Content-Type": "application/json",
          "User-Agent": "evervault-ruby/#{VERSION}",
          "Api-Key": @api_key
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
evervault-0.1.2 lib/evervault/http/request.rb