Method: ChinoBaseAPI#parse_response

Defined in:
lib/chino_ruby.rb

#parse_response(response, raw = false) ⇒ Object

base function to parse the response and raise "chino" errors if problems occurred



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/chino_ruby.rb', line 256

def parse_response(response, raw=false)
    if response.is_a?(Net::HTTPServerError)
        raise ChinoError.new("Chino Server Error: #{response} - #{response.body}", response)
        elsif response.is_a?(Net::HTTPUnauthorized)
        d = JSON.parse(response.body)
        raise ChinoAuthError.new("Chino authentication error: #{d['message']}", response)
        elsif !response.is_a?(Net::HTTPSuccess)
        begin
            d = JSON.parse(response.body)
            rescue
            raise ChinoError.new("Chino Server Error: body=#{response.body}", response)
        end
        if d['user_error'] and d['error']
            raise ChinoError.new(d['error'], response, d['user_error'])  #user_error is translated
            elsif d['error']
            raise ChinoError.new(d['error'], response)
            else
            raise ChinoError.new(response.body, response)
        end
    end
    
    return response.body if raw
    
    begin
        return JSON.parse(response.body)
        rescue JSON::ParserError
        raise ChinoError.new("Unable to parse JSON response: #{response.body}", response)
    end
end