Skip to content

Commit

Permalink
Fix JSON::ParserError in CEP implementation. The lack of tests was th…
Browse files Browse the repository at this point in the history
…e origin.
  • Loading branch information
luizfonseca committed Jan 26, 2013
1 parent 5485497 commit 8712bfd
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,25 @@ def as_json


def result
if valid?
return check if json.include?("localidade")
if valid? && valid_json?
json = JSON.parse(body)
return json if json.include?("localidade")
return false
else
false
end
end


def check
request = Net::HTTP.get_response(request_uri)
json = JSON.parse(request.body)
return json
def valid_json?
JSON.parse(response_body)
return true
rescue JSON::ParserError
return false
end

def response_body
Net::HTTP.get_response(request_uri).body
end

def valid?
@cep.length == 8
Expand Down

0 comments on commit 8712bfd

Please sign in to comment.