module RedditApi::ResponseParser

Constants

ERROR_CODES

Public Class Methods

parse_response(response, count) click to toggle source
# File lib/reddit_api/response_parser.rb, line 8
def parse_response(response, count)
  if bad_response?(response)
    handle_bad_response(count)
  else
    handle_successful_response(response, count)
  end
end

Private Class Methods

bad_response?(response) click to toggle source
# File lib/reddit_api/response_parser.rb, line 18
def bad_response?(response)
  ERROR_CODES.cover?(response.code) || response["error"]
end
handle_bad_response(count) click to toggle source
# File lib/reddit_api/response_parser.rb, line 22
def handle_bad_response(count)
  if count > 1
    []
  else
    {}
  end
end
handle_plural_response(response) click to toggle source
# File lib/reddit_api/response_parser.rb, line 38
def handle_plural_response(response)
  response["data"]["children"]
end
handle_singular_response(response) click to toggle source
# File lib/reddit_api/response_parser.rb, line 42
def handle_singular_response(response)
  response["data"]
end
handle_successful_response(response, count) click to toggle source
# File lib/reddit_api/response_parser.rb, line 30
def handle_successful_response(response, count)
  if count > 1
    handle_plural_response(response)
  else
    handle_singular_response(response)
  end
end