Class: Typekitable::ResponseFormatter
- Inherits:
-
Object
- Object
- Typekitable::ResponseFormatter
- Defined in:
- lib/typekitable/response_formatter.rb
Constant Summary collapse
- ERRORS =
{ "400" => "Error in data sent", "401" => "Not authorized", "403" => "Rate limit reached", "404" => "Not found", "500" => "Unable to process request", "503" => "Service is offline" }
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #data_heading ⇒ Object
- #error? ⇒ Boolean
-
#initialize(response) ⇒ ResponseFormatter
constructor
A new instance of ResponseFormatter.
- #output_body ⇒ Object
- #output_heading ⇒ Object
- #parsed_body ⇒ Object
- #table_body ⇒ Object
- #table_headers ⇒ Object
Constructor Details
#initialize(response) ⇒ ResponseFormatter
Returns a new instance of ResponseFormatter.
14 15 16 |
# File 'lib/typekitable/response_formatter.rb', line 14 def initialize(response) @response = response end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
3 4 5 |
# File 'lib/typekitable/response_formatter.rb', line 3 def response @response end |
Instance Method Details
#data_heading ⇒ Object
50 51 52 |
# File 'lib/typekitable/response_formatter.rb', line 50 def data_heading main_key.to_s.capitalize end |
#error? ⇒ Boolean
18 19 20 |
# File 'lib/typekitable/response_formatter.rb', line 18 def error? ERRORS.keys.include?(response.code) end |
#output_body ⇒ Object
58 59 60 |
# File 'lib/typekitable/response_formatter.rb', line 58 def output_body display_table(table_body, table_headers) end |
#output_heading ⇒ Object
54 55 56 |
# File 'lib/typekitable/response_formatter.rb', line 54 def output_heading display_line(data_heading) end |
#parsed_body ⇒ Object
22 23 24 |
# File 'lib/typekitable/response_formatter.rb', line 22 def parsed_body JSON.parse(response.body, :symbolize_names => true) end |
#table_body ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/typekitable/response_formatter.rb', line 38 def table_body if error? elsif collection? collection_data elsif singular_resource? singular_resource_data else [parsed_body] end end |
#table_headers ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/typekitable/response_formatter.rb', line 26 def table_headers if error? error_key elsif collection? collection_headers elsif singular_resource? singular_resource_headers else [main_key] end end |