Class: CmQuiz::ProjectAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/cm_quiz/project_api.rb

Defined Under Namespace

Classes: PerformFailed

Constant Summary collapse

REQUEST_TIMEOUT =
5

Instance Method Summary collapse

Constructor Details

#initialize(endpoint) ⇒ ProjectAPI

Returns a new instance of ProjectAPI.



16
17
18
# File 'lib/cm_quiz/project_api.rb', line 16

def initialize(endpoint)
  @endpoint = endpoint
end

Instance Method Details

#request(verb, path, options = {}) ⇒ Object

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cm_quiz/project_api.rb', line 20

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