lib/chatwork/chatwork_error.rb in chatwork-0.4.1 vs lib/chatwork/chatwork_error.rb in chatwork-0.5.0

- old
+ new

@@ -1,10 +1,10 @@ require 'json' module ChatWork class ChatWorkError < StandardError - def self.from_response(status, body) + def self.from_response(status, body, headers) # HTTP status 204 don't have body. return APIError.new(status, "") if status == 204 hash = begin @@ -14,10 +14,14 @@ end unless hash['errors'] return APIConnectionError.new("Invalid response #{body}") end + if headers.has_key?('WWW-Authenticate') + return AuthenticateError.new(headers['WWW-Authenticate'], status, hash['errors']) + end + APIError.new(status, hash['errors']) end attr_reader :status attr_reader :error_response @@ -45,8 +49,22 @@ attr_reader :errors def initialize(status, error_response) @errors = error_response super(error_response[0], status, error_response) + end + end + + class AuthenticateError < ChatWorkError + attr_reader :error, :error_description + + def initialize(message, status, error_response) + message =~ /error="([^\"]+)"/ + @error = Regexp.last_match(1) + + message =~ /error_description="([^\"]+)"/ + @error_description = Regexp.last_match(1) + + super(message, status, error_response) end end end