Sha256: 8e359be6692cb3f7053f71a084b42246e758034ac873722ead87e59ac877d6b9

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require "httparty"

module Velocity
  module Api
    class Base
      class NotFoundError < StandardError; end
      class UnauthorizedError < StandardError; end
      class InternalServerError < StandardError; end
      class BadRequestError < StandardError; end

      include HTTParty

      base_uri "https://api.velocity.codeclimate.com/v1"

      attr_accessor :args

      def initialize(args)
        @args = args
      end

      def options
        {
          headers: {
            "Authorization" => "Bearer #{Velocity.configuration.api_token}",
            "Content-Type" => "application/vnd.api+json",
          }
        }
      end

      def parse_response(response)
        case response.code
          when 200..201
            JSON.parse(response.body)["data"]
          when 400
            errors = JSON.parse(response.body)["errors"]
            raise BadRequestError.new(errors.first["title"] + ": " + errors.first["detail"])
          when 401
            raise UnauthorizedError.new("401 Unauthorized");
          when 404
            raise NotFoundError.new("404 not found error");
          else
            raise InternalServerError.new("#{response.code} something went wrong: #{response.body}")
        end
      end

      def build_query
        args.map do |key, value|
          "filter[#{key.to_s}][contains]=#{value}"
        end.join("&")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
velocity_client_ruby-0.2.1 lib/velocity/api/base.rb