Class: Typekitable::ResponseFormatter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#responseObject (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_headingObject



50
51
52
# File 'lib/typekitable/response_formatter.rb', line 50

def data_heading
  main_key.to_s.capitalize
end

#error?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/typekitable/response_formatter.rb', line 18

def error?
  ERRORS.keys.include?(response.code)
end

#output_bodyObject



58
59
60
# File 'lib/typekitable/response_formatter.rb', line 58

def output_body
  display_table(table_body, table_headers)
end

#output_headingObject



54
55
56
# File 'lib/typekitable/response_formatter.rb', line 54

def output_heading
  display_line(data_heading)
end

#parsed_bodyObject



22
23
24
# File 'lib/typekitable/response_formatter.rb', line 22

def parsed_body
  JSON.parse(response.body, :symbolize_names => true)
end

#table_bodyObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/typekitable/response_formatter.rb', line 38

def table_body
  if error?
    error_message
  elsif collection?
    collection_data
  elsif singular_resource?
    singular_resource_data
  else
    [parsed_body]
  end
end

#table_headersObject



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