class Cortex::Result

Attributes

contents[R]
errors[R]
next_page[R]
page[R]
per_page[R]
prev_page[R]
range[R]
range_end[R]
range_start[R]
raw_headers[R]
status[R]
total_items[R]
total_pages[R]

Public Class Methods

new(body, headers, status) click to toggle source
# File lib/cortex/result.rb, line 5
def initialize(body, headers, status)
  @contents = body
  @raw_headers = headers
  @status = status
  parse_headers(headers)
  @errors = find_errors
end

Public Instance Methods

is_error?() click to toggle source
# File lib/cortex/result.rb, line 13
def is_error?
  @status >= 400 || (@contents.is_a?(Hash) && @contents.errors?)
end

Private Instance Methods

find_errors() click to toggle source
# File lib/cortex/result.rb, line 34
def find_errors
  errors = nil
  if is_error?
    if @contents.is_a?(Hash)
      if @contents.errors?
        errors = @contents.errors
      else
        errors = @contents.message
      end
    else
      errors = @contents
    end
  end

  Array(errors)
end
parse_headers(headers) click to toggle source
# File lib/cortex/result.rb, line 19
def parse_headers(headers)
  if headers['X-Total']
    @total_items = headers['X-Total']
    @count = headers['X-Total'].to_i
    @page = headers['X-Page'].to_i
    @per_page = headers['X-Per-Page'].to_i
    @range_start = (@page-1) * @per_page
    @range_end = @per_page * @page - 1
    @range = "#{@range_start}-#{@range_end}"
    @total_pages = headers['X-Total-Pages']
    @next_page = headers['X-Next-Page']
    @prev_page = headers['X-Prev-Page']
  end
end