lib/spidr/headers.rb in spidr-0.3.2 vs lib/spidr/headers.rb in spidr-0.4.0

- old
+ new

@@ -114,10 +114,71 @@ def content_types (headers['content-type'] || []) end # + # The charset included in the Content-Type. + # + # @return [String, nil] + # The charset of the content. + # + # @since 0.4.0 + # + def content_charset + content_types.each do |value| + if value.include?(';') + value.split(';').each do |param| + param.strip! + + if param.start_with?('charset=') + return param.split('=',2).last + end + end + end + end + + return nil + end + + # + # Determines if any of the content-types of the page include a given + # type. + # + # @param [String] type + # The content-type to test for. + # + # @return [Boolean] + # Specifies whether the page includes the given content-type. + # + # @example Match the Content-Type + # page.is_content_type?('application/json') + # + # @example Match the sub-type of the Content-Type + # page.is_content_type?('json') + # + # @since 0.4.0 + # + def is_content_type?(type) + if type.include?('/') + # otherwise only match the first param + content_types.any? do |value| + value = value.split(';',2).first + + value == type + end + else + # otherwise only match the sub-type + content_types.any? do |value| + value = value.split(';',2).first + value = value.split('/',2).last + + value == type + end + end + end + + # # Determines if the page is plain-text. # # @return [Boolean] # Specifies whether the page is plain-text. # @@ -289,37 +350,21 @@ # @since 0.2.2 # def cookie_params params = {} - cookies.each do |cookie| - cookie.split('; ').each do |key_value| - key, value = key_value.split('=',2) + cookies.each do |value| + value.split(';').each do |param| + param.strip! - unless RESERVED_COOKIE_NAMES.include?(key) - params[key] = (value || '') + name, value = param.split('=',2) + + unless RESERVED_COOKIE_NAMES.include?(name) + params[name] = (value || '') end end end return params - end - - protected - - # - # Determines if any of the content-types of the page include a given - # type. - # - # @param [String] type - # The content-type to test for. - # - # @return [Boolean] - # Specifies whether the page includes the given content-type. - # - # @since 0.2.4 - # - def is_content_type?(type) - content_types.any? { |content| content.include?(type) } end end end