Class: Teamlab::Response
- Inherits:
-
Object
- Object
- Teamlab::Response
- Defined in:
- lib/teamlab/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#success ⇒ Object
readonly
Returns the value of attribute success.
Instance Method Summary collapse
-
#correct?(command) ⇒ Boolean
Check if responce is succeed, has nil error and has hash body.
-
#data ⇒ Hash
Data of response.
- #generate_err_msg(http_response) ⇒ Object
-
#initialize(http_response) ⇒ Response
constructor
A new instance of Response.
- #prettify_response(msg) ⇒ Object
Constructor Details
#initialize(http_response) ⇒ Response
Returns a new instance of Response.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/teamlab/response.rb', line 9 def initialize(http_response) @code = http_response.code @success = @code < 400 err_msg = generate_err_msg(http_response) if @code >= 400 if @success handle_success_responce(http_response) else raise TimeoutError, 'Portal is warming up' if http_response.parsed_response.include?('portal is being warmed') raise "Error #{@code}\n#{err_msg}" if @code >= 400 @body = http_response.respond_to?(:parsed_response) && http_response.parsed_response.key?('result') ? http_response.parsed_response['result'] : http_response.to_hash @error = @body['error']['message'] if @body.key?('error') && @body['error'].key?('message') end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/teamlab/response.rb', line 7 def body @body end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
7 8 9 |
# File 'lib/teamlab/response.rb', line 7 def code @code end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
7 8 9 |
# File 'lib/teamlab/response.rb', line 7 def error @error end |
#success ⇒ Object (readonly)
Returns the value of attribute success.
7 8 9 |
# File 'lib/teamlab/response.rb', line 7 def success @success end |
Instance Method Details
#correct?(command) ⇒ Boolean
Check if responce is succeed, has nil error and has hash body
46 47 48 49 50 51 52 53 |
# File 'lib/teamlab/response.rb', line 46 def correct?(command) result = @success && @error.nil? && @body.is_a?(Hash) raise("Response should be always successful for #{command}") unless @success raise("Response should not contain errors for #{command}") unless @error.nil? raise("Response should be Hash for #{command}") unless @body.is_a?(Hash) result end |
#data ⇒ Hash
Returns data of response.
39 40 41 |
# File 'lib/teamlab/response.rb', line 39 def data @body['response'] end |
#generate_err_msg(http_response) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/teamlab/response.rb', line 24 def generate_err_msg(http_response) "API request failed\n\noriginal request:\n"\ "#{http_response.request.http_method} #{http_response.request.path}\nbody: "\ "#{JSON.pretty_generate(http_response.request.[:body])}"\ "\n\nresponse:\n"\ "#{prettify_response(http_response.parsed_response)}" end |
#prettify_response(msg) ⇒ Object
32 33 34 35 36 |
# File 'lib/teamlab/response.rb', line 32 def prettify_response(msg) JSON.pretty_generate(msg) rescue Encoding::UndefinedConversionError msg.force_encoding(Encoding::UTF_8) end |