class Typekitable::ResponseFormatter

Constants

ERRORS

Attributes

response[R]

Public Class Methods

new(response) click to toggle source
# File lib/typekitable/response_formatter.rb, line 14
def initialize(response)
  @response = response
end

Public Instance Methods

data_heading() click to toggle source
# File lib/typekitable/response_formatter.rb, line 50
def data_heading
  main_key.to_s.capitalize
end
error?() click to toggle source
# File lib/typekitable/response_formatter.rb, line 18
def error?
  ERRORS.keys.include?(response.code)
end
output_body() click to toggle source
# File lib/typekitable/response_formatter.rb, line 58
def output_body
  display_table(table_body, table_headers)
end
output_heading() click to toggle source
# File lib/typekitable/response_formatter.rb, line 54
def output_heading
  display_line(data_heading)
end
parsed_body() click to toggle source
# File lib/typekitable/response_formatter.rb, line 22
def parsed_body
  JSON.parse(response.body, :symbolize_names => true)
end
table_body() click to toggle source
# 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_headers() click to toggle source
# 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

Private Instance Methods

collection?() click to toggle source
# File lib/typekitable/response_formatter.rb, line 72
def collection?
  !error? && data_element.is_a?(Array)
end
collection_data() click to toggle source
# File lib/typekitable/response_formatter.rb, line 90
def collection_data
  data_element.map do |line|
    line
  end
end
collection_headers() click to toggle source
# File lib/typekitable/response_formatter.rb, line 84
def collection_headers
  data_element.map do |data|
    data.keys
  end.flatten.uniq
end
data_element() click to toggle source
# File lib/typekitable/response_formatter.rb, line 64
def data_element
  parsed_body[main_key]
end
display_line(line) click to toggle source
# File lib/typekitable/response_formatter.rb, line 112
def display_line(line)
  Formatador.display_line(line)
end
display_table(table_data, headers) click to toggle source
# File lib/typekitable/response_formatter.rb, line 116
def display_table(table_data, headers)
  Formatador.display_table(table_data, headers)
end
empty_response?() click to toggle source
# File lib/typekitable/response_formatter.rb, line 76
def empty_response?
  data_element.any?
end
error_key() click to toggle source
# File lib/typekitable/response_formatter.rb, line 104
def error_key
  [response.code]
end
error_message() click to toggle source
# File lib/typekitable/response_formatter.rb, line 108
def error_message
  [{response.code => data_element.pop}]
end
main_key() click to toggle source
# File lib/typekitable/response_formatter.rb, line 80
def main_key
  parsed_body.keys.first
end
singular_resource?() click to toggle source
# File lib/typekitable/response_formatter.rb, line 68
def singular_resource?
  !error? && data_element.is_a?(Hash)
end
singular_resource_data() click to toggle source
# File lib/typekitable/response_formatter.rb, line 100
def singular_resource_data
  [data_element]
end
singular_resource_headers() click to toggle source
# File lib/typekitable/response_formatter.rb, line 96
def singular_resource_headers
  data_element.keys
end