Class: SphereEngine::REST::Request
- Inherits:
-
Object
- Object
- SphereEngine::REST::Request
- 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
-
#client ⇒ Object
Returns the value of attribute client.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#options ⇒ Object
Returns the value of attribute options.
-
#path ⇒ Object
Returns the value of attribute path.
-
#request_method ⇒ Object
Returns the value of attribute request_method.
-
#service ⇒ Object
Returns the value of attribute service.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
- #build_base_url ⇒ Object
- #build_http_request ⇒ Object
- #build_request_params ⇒ Object
- #error(code, body) ⇒ Object
- #fail_or_return_response_body(code, body) ⇒ Object
- #format_to_response(response) ⇒ Object
- #initialize(client, request_method, service, path, options = {}) ⇒ SphereEngine::REST::Request constructor
- #perform ⇒ Array, Hash
Constructor Details
#initialize(client, request_method, service, path, options = {}) ⇒ SphereEngine::REST::Request
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, = {}) @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 = end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
10 11 12 |
# File 'lib/sphere_engine/rest/request.rb', line 10 def client @client end |
#headers ⇒ Object
Returns the value of attribute headers.
10 11 12 |
# File 'lib/sphere_engine/rest/request.rb', line 10 def headers @headers end |
#options ⇒ Object
Returns the value of attribute options.
10 11 12 |
# File 'lib/sphere_engine/rest/request.rb', line 10 def @options end |
#path ⇒ Object
Returns the value of attribute path.
10 11 12 |
# File 'lib/sphere_engine/rest/request.rb', line 10 def path @path end |
#request_method ⇒ Object
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 |
#service ⇒ Object
Returns the value of attribute service.
10 11 12 |
# File 'lib/sphere_engine/rest/request.rb', line 10 def service @service end |
#uri ⇒ Object
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_url ⇒ Object
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_request ⇒ Object
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_params ⇒ Object
75 76 77 78 79 |
# File 'lib/sphere_engine/rest/request.rb', line 75 def build_request_params { access_token: @token }.merge() 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 |
#perform ⇒ 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 |