diff --git a/src/accept.cr b/src/accept.cr index fec57ae..25dcee0 100644 --- a/src/accept.cr +++ b/src/accept.cr @@ -11,8 +11,9 @@ struct Athena::Negotiation::Accept < Athena::Negotiation::BaseAccept parts = @type.split '/' - # TODO: Use more specific exception - raise ANG::Exceptions::InvalidMediaType.new @type, "Invalid media type: '#{@type}'." if parts.size != 2 || !parts[0].presence || !parts[1].presence + if parts.size != 2 || !parts[0].presence || !parts[1].presence + raise ANG::Exceptions::InvalidMediaType.new @type + end @base_part = parts[0] @sub_part = parts[1] diff --git a/src/accept_language.cr b/src/accept_language.cr index 7df3f2d..a4db6cb 100644 --- a/src/accept_language.cr +++ b/src/accept_language.cr @@ -21,7 +21,7 @@ struct Athena::Negotiation::AcceptLanguage < Athena::Negotiation::BaseAccept @script = parts[1] @region = parts[2] else - raise "Invalid language: '#{@value}'." + raise ANG::Exceptions::InvalidLanguage.new @type end end end diff --git a/src/athena-negotiation.cr b/src/athena-negotiation.cr index b57f7a5..44a2305 100644 --- a/src/athena-negotiation.cr +++ b/src/athena-negotiation.cr @@ -14,7 +14,7 @@ require "./exceptions/*" alias ANG = Athena::Negotiation module Athena::Negotiation - # Returns an `ANG::Negotiation` singleton instance. + # Returns an `ANG::Negotiator` singleton instance. class_getter(negotiator) { ANG::Negotiator.new } # Returns an `ANG::CharsetNegotiator` singleton instance. diff --git a/src/exceptions/invalid_language.cr b/src/exceptions/invalid_language.cr new file mode 100644 index 0000000..8cde019 --- /dev/null +++ b/src/exceptions/invalid_language.cr @@ -0,0 +1,7 @@ +require "./negotiation_exception" + +class Athena::Negotiation::Exceptions::InvalidLanguage < Athena::Negotiation::Exceptions::Exception + def initialize(type : String, cause : Exception? = nil) + super type, "Invalid language: '#{type}'.", cause + end +end diff --git a/src/exceptions/invalid_media_type.cr b/src/exceptions/invalid_media_type.cr index 66e725c..28f7d49 100644 --- a/src/exceptions/invalid_media_type.cr +++ b/src/exceptions/invalid_media_type.cr @@ -1,9 +1,7 @@ require "./negotiation_exception" class Athena::Negotiation::Exceptions::InvalidMediaType < Athena::Negotiation::Exceptions::Exception - getter type : String - - def initialize(@type : String, message : String? = nil, cause : Exception? = nil) - super message, cause + def initialize(type : String, cause : Exception? = nil) + super type, "Invalid media type: '#{type}'.", cause end end diff --git a/src/exceptions/negotiation_exception.cr b/src/exceptions/negotiation_exception.cr index f91de18..5a1adf8 100644 --- a/src/exceptions/negotiation_exception.cr +++ b/src/exceptions/negotiation_exception.cr @@ -1,2 +1,7 @@ abstract class Athena::Negotiation::Exceptions::Exception < ::Exception + getter type : String + + def initialize(@type : String, message : String? = nil, cause : Exception? = nil) + super message, cause + end end diff --git a/src/negotiator.cr b/src/negotiator.cr index 28c0e26..8189348 100644 --- a/src/negotiator.cr +++ b/src/negotiator.cr @@ -50,8 +50,6 @@ class Athena::Negotiation::Negotiator < Athena::Negotiation::AbstractNegotiator (accept_plus == "*" || priority_plus == '*' || plus_equal) && intercection.size == accept.parameters.size ) - # TODO: Calculate intercection between each header's parameters - score = 100 * (base_equal ? 1 : 0) + 10 * (sub_equal ? 1 : 0) + (plus_equal ? 1 : 0) + intercection.size return ANG::AcceptMatch.new accept.quality * priority.quality, score, index end