Skip to content

Commit

Permalink
refactor for code quality metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Motoshi-Nishihira committed May 21, 2016
1 parent c7f8ad6 commit ee7d5d5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
34 changes: 20 additions & 14 deletions lib/oauth2/error.rb
@@ -1,5 +1,3 @@
require 'kconv'

module OAuth2
class Error < StandardError
attr_reader :response, :code, :description
Expand All @@ -10,25 +8,33 @@ def initialize(response)
response.error = self
@response = response

message = []

if response.parsed.is_a?(Hash)
@code = response.parsed['error']
@description = response.parsed['error_description']
message << "#{@code}: #{@description}"
error_description = "#{@code}: #{@description}"
end

if message[0] && message[0].respond_to?(:encoding)
script_encoding = message[0].encoding
response_body_encoding = response.body.encoding
response_body = response.body.kconv(script_encoding, response_body_encoding)
else
response_body = response.body
end
super(error_message(response.body, :error_description => error_description))
end

# Makes a error message
# @param [String] response_body response body of request
# @param [String] opts :error_description error description to show first line
def error_message(response_body, opts = {})
message = []

opts[:error_description] && message << opts[:error_description]

error_message = if opts[:error_description] && opts[:error_description].respond_to?(:encoding)
script_encoding = opts[:error_description].encoding
response_body.encode(script_encoding)
else
response_body
end

message << response_body
message << error_message

super(message.join("\n"))
message.join("\n")
end
end
end
4 changes: 2 additions & 2 deletions spec/oauth2/client_spec.rb
@@ -1,6 +1,6 @@
# coding: utf-8
require 'helper'
require 'kconv'
require 'nkf'

describe OAuth2::Client do
let!(:error_value) { 'invalid_token' }
Expand All @@ -18,7 +18,7 @@
stub.post('/redirect') { |env| [303, {'Content-Type' => 'text/plain', 'location' => '/reflect'}, ''] }
stub.get('/error') { |env| [500, {'Content-Type' => 'text/plain'}, 'unknown error'] }
stub.get('/empty_get') { |env| [204, {}, nil] }
stub.get('/different_encoding') { |env| [500, {'Content-Type' => 'application/json'}, MultiJson.encode(:error => error_value, :error_description => "∞").kconv(Kconv::EUC, Kconv::UTF8)] }
stub.get('/different_encoding') { |env| [500, {'Content-Type' => 'application/json'}, NKF.nkf('-We', MultiJson.encode(:error => error_value, :error_description => "∞"))] }
end
end
end
Expand Down

0 comments on commit ee7d5d5

Please sign in to comment.