class SphereEngine::REST::Request
Constants
- BASE_URL_COMPILERS_SERVICE
- BASE_URL_PROBLEMS_SERVICE
Attributes
client[RW]
headers[RW]
options[RW]
path[RW]
request_method[RW]
service[RW]
uri[RW]
Public Class Methods
new(client, request_method, service, path, options = {})
click to toggle source
@param client [SphereEngine::Client] @param request_method
[String, Symbol] @param path [String] @param options [Hash] @return [SphereEngine::REST::Request]
# 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
Public Instance Methods
build_base_url()
click to toggle source
# 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_request()
click to toggle source
# 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_params()
click to toggle source
# File lib/sphere_engine/rest/request.rb, line 75 def build_request_params { access_token: @token }.merge(options) end
error(code, body)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
perform()
click to toggle source
@return [Array, Hash]
# 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