 	_parseUnknownBody: () ->
 		@_parseBodyAsJson()
 		delete @error # this is a fallback, ignore error
-		@_parseBodyAsForm() if not @_parsedBody
+		@_parseBodyAsForm() if not @body
 		delete @error # this is a fallback, ignore error
 
 	_parseBody: (parseFunc) ->
 		try
-			@_parsedBody = parseFunc(@_body)
+			@body = parseFunc(@_unparsedBody)
 		catch ex
-			@_setError('Unable to parse body of request_token response')
+			@_setError('Unable to parse response')
 			return
-		if not @_parsedBody
+		if not @body
 			return
-		if @_parsedBody.error or @_parsedBody.error_description
-			@_setError(@_parsedBody.error_description || 'Error while requesting token')
-			delete @_parsedBody
+		if @body.error or @body.error_description
+			@_setError(@body.error_description || 'Unable to parse response')
+			delete @body
 
 	_hasKeyAndSecret: () ->
-		return !!@_parsedBody and
-		!!@_parsedBody.oauth_token and
-		!!@_parsedBody.oauth_token_secret
+		return !!@body and
+		!!@body.oauth_token and
+		!!@body.oauth_token_secret
 
 	_setError: (message) ->
-		@_error = new check.Error(message)
-		@_error.body = @_body+		@error = new check.Error(@_errorPrefix + ' ' + message)
+		@error.body = @_unparsedBody
+
+module.exports = OAuth1ResponseParser
		if @error
			return

		@_parseUnknownBody() if not @_parsedBody
		if not @_hasKeyAndSecret()
			@_setError('Could not find request_token in response')
			return

		@oauth_token = @_parsedBody.oauth_token
		@oauth_token_secret = @_parsedBody.oauth_token_secret

	_isResponseOk: () ->
		return @_response.statusCode == 200

	_hasBody: () ->
		return !!@_body

	_isJsonResponse: () ->
		return @_format == 'json' or

querystring = require 'querystring'
check = require './check'

class OAuth1ResponseParser
	constructor: (response, body, format, tokenType) ->
		@_response = response
		@_unparsedBody = body
		@_format = format
		@_contentType = @_response.headers['content-type']
		@_errorPrefix = 'Error while parsing response for ' + tokenType
		
		if not @_isResponseOk
			@_setError('HTTP status code: ' + @_response.statusCode)
			return
		if not @_hasBody()
			@_setError('Empty response')
			return

		if @_isJsonResponse()
