lib/tikkie/api/responses/base.rb in tikkie-api-0.2.0 vs lib/tikkie/api/responses/base.rb in tikkie-api-0.2.1

- old
+ new

@@ -10,22 +10,22 @@ attr_reader :response, :data def initialize(response) if response.respond_to?(:body) @response = response - @data = JSON.parse(response.body, symbolize_names: true) + @data = parse_body(response.body) else @data = response end end def response_code response.code.to_i if response end def success? - response_code == 200 || response_code == 201 + (response_code == 200 || response_code == 201) && !@invalid end def error? !success? end @@ -44,9 +44,23 @@ end end errors end + end + + private + + def parse_body(body) + body = body.respond_to?(:read) ? body.read : body + + JSON.parse(body, symbolize_names: true) + rescue JSON::ParserError => ex + @invalid = true + + { + message: "Unable to parse JSON: #{ex.message}" + } end end end end end