Class: SphereEngine::REST::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/sphere_engine/rest/request.rb

Constant Summary collapse

BASE_URL_COMPILERS_SERVICE =
'https://09de42b6.compilers.sphere-engine.com/api/V3'.freeze
BASE_URL_PROBLEMS_SERVICE =
'https://09de42b6.problems.sphere-engine.com/api/v3'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, request_method, service, path, options = {}) ⇒ SphereEngine::REST::Request

Parameters:

  • client (SphereEngine::Client)
  • request_method (String, Symbol)
  • path (String)
  • options (Hash) (defaults to: {})


17
18
19
20
21
22
23
24
25
# File 'lib/sphere_engine/rest/request.rb', line 17

def initialize(client, request_method, service, path, options = {})
  @client = client
  @token  = client.get_token(service)
  @service = service
  @request_method = request_method
  @uri = Addressable::URI.parse(path.start_with?('http') ? path : build_base_url + path)
  @path = uri.path
  @options = options
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/sphere_engine/rest/request.rb', line 10

def client
  @client
end

#headersObject

Returns the value of attribute headers.



10
11
12
# File 'lib/sphere_engine/rest/request.rb', line 10

def headers
  @headers
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/sphere_engine/rest/request.rb', line 10

def options
  @options
end

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/sphere_engine/rest/request.rb', line 10

def path
  @path
end

#request_methodObject

Returns the value of attribute request_method.



10
11
12
# File 'lib/sphere_engine/rest/request.rb', line 10

def request_method
  @request_method
end

#serviceObject

Returns the value of attribute service.



10
11
12
# File 'lib/sphere_engine/rest/request.rb', line 10

def service
  @service
end

#uriObject

Returns the value of attribute uri.



10
11
12
# File 'lib/sphere_engine/rest/request.rb', line 10

def uri
  @uri
end

Instance Method Details

#build_base_urlObject



55
56
57
58
59
60
61
62
# File 'lib/sphere_engine/rest/request.rb', line 55

def build_base_url
  return case @service
  when :compilers
    BASE_URL_COMPILERS_SERVICE
  when :problems
    BASE_URL_PROBLEMS_SERVICE
  end
end

#build_http_requestObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/sphere_engine/rest/request.rb', line 64

def build_http_request
  return case @request_method
  when :get
    HTTP.get(uri, params: build_request_params)
  when :post
    HTTP.post(uri, params: build_request_params)
  when :put
    HTTP.put(uri, params: build_request_params)
  end
end

#build_request_paramsObject



75
76
77
78
79
# File 'lib/sphere_engine/rest/request.rb', line 75

def build_request_params
  {
    access_token: @token
  }.merge(options)
end

#error(code, body) ⇒ Object



48
49
50
51
52
53
# File 'lib/sphere_engine/rest/request.rb', line 48

def error(code, body)
  klass = SphereEngine::Error::ERRORS[code]
  if klass
    klass.from_response(body)
  end
end

#fail_or_return_response_body(code, body) ⇒ Object



42
43
44
45
46
# File 'lib/sphere_engine/rest/request.rb', line 42

def fail_or_return_response_body(code, body)
  error = error(code, body)
  raise(error) if error
  body
end

#format_to_response(response) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/sphere_engine/rest/request.rb', line 34

def format_to_response(response)
  begin
    JSON.parse(response.to_s)
  rescue JSON::ParserError
    response.to_s
  end
end

#performArray, Hash

Returns:

  • (Array, Hash)


28
29
30
31
32
# File 'lib/sphere_engine/rest/request.rb', line 28

def perform
  response = build_http_request
  response_body = response.body.empty? ? '' : format_to_response(response)
  fail_or_return_response_body(response.code, response_body)
end