Some cleanup

This commit is contained in:
George Dietrich 2020-12-23 18:42:21 -05:00
parent b860bdfd27
commit ae80dccf0c
7 changed files with 19 additions and 10 deletions

View file

@ -11,8 +11,9 @@ struct Athena::Negotiation::Accept < Athena::Negotiation::BaseAccept
parts = @type.split '/' parts = @type.split '/'
# TODO: Use more specific exception if parts.size != 2 || !parts[0].presence || !parts[1].presence
raise ANG::Exceptions::InvalidMediaType.new @type, "Invalid media type: '#{@type}'." if parts.size != 2 || !parts[0].presence || !parts[1].presence raise ANG::Exceptions::InvalidMediaType.new @type
end
@base_part = parts[0] @base_part = parts[0]
@sub_part = parts[1] @sub_part = parts[1]

View file

@ -21,7 +21,7 @@ struct Athena::Negotiation::AcceptLanguage < Athena::Negotiation::BaseAccept
@script = parts[1] @script = parts[1]
@region = parts[2] @region = parts[2]
else else
raise "Invalid language: '#{@value}'." raise ANG::Exceptions::InvalidLanguage.new @type
end end
end end
end end

View file

@ -14,7 +14,7 @@ require "./exceptions/*"
alias ANG = Athena::Negotiation alias ANG = Athena::Negotiation
module Athena::Negotiation module Athena::Negotiation
# Returns an `ANG::Negotiation` singleton instance. # Returns an `ANG::Negotiator` singleton instance.
class_getter(negotiator) { ANG::Negotiator.new } class_getter(negotiator) { ANG::Negotiator.new }
# Returns an `ANG::CharsetNegotiator` singleton instance. # Returns an `ANG::CharsetNegotiator` singleton instance.

View file

@ -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

View file

@ -1,9 +1,7 @@
require "./negotiation_exception" require "./negotiation_exception"
class Athena::Negotiation::Exceptions::InvalidMediaType < Athena::Negotiation::Exceptions::Exception class Athena::Negotiation::Exceptions::InvalidMediaType < Athena::Negotiation::Exceptions::Exception
getter type : String def initialize(type : String, cause : Exception? = nil)
super type, "Invalid media type: '#{type}'.", cause
def initialize(@type : String, message : String? = nil, cause : Exception? = nil)
super message, cause
end end
end end

View file

@ -1,2 +1,7 @@
abstract class Athena::Negotiation::Exceptions::Exception < ::Exception 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 end

View file

@ -50,8 +50,6 @@ class Athena::Negotiation::Negotiator < Athena::Negotiation::AbstractNegotiator
(accept_plus == "*" || priority_plus == '*' || plus_equal) && (accept_plus == "*" || priority_plus == '*' || plus_equal) &&
intercection.size == accept.parameters.size 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 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 return ANG::AcceptMatch.new accept.quality * priority.quality, score, index
end end