class CrushPics::Response

Attributes

http[R]

Public Class Methods

new(http) click to toggle source
# File lib/crush_pics/response.rb, line 7
def initialize(http)
  @http = http
end

Public Instance Methods

body()
Alias for: parse
client_error?() click to toggle source
# File lib/crush_pics/response.rb, line 35
def client_error?
  http.is_a?(Net::HTTPClientError)
end
created?() click to toggle source
# File lib/crush_pics/response.rb, line 23
def created?
  http.is_a?(Net::HTTPCreated)
end
parse() click to toggle source
# File lib/crush_pics/response.rb, line 11
def parse
  return {} if unauthorized?

  parsed_response
end
Also aliased as: body
server_error?() click to toggle source
# File lib/crush_pics/response.rb, line 39
def server_error?
  http.is_a?(Net::HTTPServerError)
end
success?() click to toggle source
# File lib/crush_pics/response.rb, line 31
def success?
  http.is_a?(Net::HTTPSuccess)
end
unauthorized?() click to toggle source
# File lib/crush_pics/response.rb, line 19
def unauthorized?
  http.is_a?(Net::HTTPUnauthorized)
end
validation_error?() click to toggle source
# File lib/crush_pics/response.rb, line 27
def validation_error?
  http.is_a?(Net::HTTPUnprocessableEntity)
end
validation_error_message() click to toggle source
# File lib/crush_pics/response.rb, line 43
def validation_error_message
  msgs = parsed_response.fetch('message', {}).map do |k, v|
    msg = v
    msg = msg.join(', ') if msg.is_a?(Array)
    "#{ k }: #{ msg }"
  end
  msgs.join(', ')
end

Private Instance Methods

parsed_response() click to toggle source
# File lib/crush_pics/response.rb, line 54
def parsed_response
  @parsed_response ||= JSON.parse(http.body)
rescue TypeError, JSON::ParserError
  @parsed_response = {}
end