Sha256: 5cc78b6ab6c805609495ba959d001545ee61d5575fb532885a3178dbe1a91650

Contents?: true

Size: 882 Bytes

Versions: 3

Compression:

Stored size: 882 Bytes

Contents

require 'httparty'

module CmQuiz
  class ProjectAPI
    REQUEST_TIMEOUT = 5

    class PerformFailed < StandardError
      attr_reader :response

      def initialize(message, response)
        @response = response
        super(message)
      end
    end

    def initialize(endpoint)
      @endpoint = endpoint
    end

    def request(verb, path, options = {})
      url = @endpoint + path

      query = options[:query]
      body = options[:body] ? options[:body].to_json : options[:body]
      headers = { 'Content-Type' => 'application/json' }.merge(options[:headers] || {})

      http_options = {
        query: query,
        body: body,
        headers: headers,
        timeout: REQUEST_TIMEOUT
      }
      res = HTTParty.send(verb, url, http_options)

      raise PerformFailed.new("[#{res.code}]: #{res.body}", res) unless res.success?

      res
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cm_quiz-0.0.8 lib/cm_quiz/project_api.rb
cm_quiz-0.0.7 lib/cm_quiz/project_api.rb
cm_quiz-0.0.6 lib/cm_quiz/project_api.rb