Sha256: 89b3aef0d53eb4f9744e61ca6b3c2087ac896b62ed8eef27ed19c4374e3505c8

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'httparty'

module Velocity
  class Api
    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"

    def initialize
      @options = {
        headers: {
          authorization: "Bearer #{Velocity.configuration.api_token}"
        }
      }
    end

    def fetch_contributors(args)
      parse_response(self.class.get("/people", @options.merge({
        query: build_query_attributes(args)
      })))
    end

    def parse_response(response)
      case response.code
        when 200
          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_attributes(args)
      args.map do |key, value|
        "filter[#{key.to_s}][eq]=#{value}"
      end.join("&")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
velocity_client_ruby-0.1.1 lib/velocity/api.rb