Sha256: 19931dc0ac92220c583a97eecbc1ed11baa311dcd6656223623418cae31cfb09

Contents?: true

Size: 848 Bytes

Versions: 5

Compression:

Stored size: 848 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].to_json
      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

5 entries across 5 versions & 1 rubygems

Version Path
cm_quiz-0.0.5 lib/cm_quiz/project_api.rb
cm_quiz-0.0.4 lib/cm_quiz/project_api.rb
cm_quiz-0.0.3 lib/cm_quiz/project_api.rb
cm_quiz-0.0.2 lib/cm_quiz/project_api.rb
cm_quiz-0.0.1 lib/cm_quiz/project_api.rb