#-- # gravaty # Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Marco Bresciani # # This file is part of gravaty. # # gravaty is free software: you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation, either version 3 of the License, or (at your # option) any later version. # # gravaty is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # for more details. # # You should have received a copy of the GNU General Public License # along with gravaty. If not, see . #++ require 'cgi' require_relative '../constants' module Gravaty module Parsers # This class is an implementation of the Parsable duck type that # checks the default option. The only needed parameter is a valid # default option. # # Author:: {Marco Bresciani}[mailto:marcobresciani_1974@libero.it] # Copyright:: Copyright © 2013, 2014, 2015, 2016, 2017, 2018, # 2019 Marco Bresciani # License:: GNU General Public License version 3 class Default # The parsable duck type interface to every parser usage. def parse(value = nil) unless value.nil? or value.empty? unless DEFAULT_OPTIONS.include? value valid = %w(http https).index {|scheme| valid?(value, scheme)} raise ArgumentError, I18n.t('error.uri', value: value) if valid.nil? end return 'd=' + CGI::escape(value).to_s end '' end # ------------------------ here starts the list of private methods private def valid?(value, scheme) return false if value.nil? return false if value.empty? parser = URI::Parser.new an_uri = parser.extract(value.downcase, scheme) return false if an_uri.nil? return false if an_uri.empty? begin an_uri = parser.parse an_uri[0] #See URI validation rules at # http://en.gravatar.com/site/implement/images/#default-image #rule 2 return false if (an_uri.scheme == 'http') and (an_uri.port != Net::HTTP.http_default_port) return false if (an_uri.scheme == 'https') and (an_uri.port != Net::HTTP.https_default_port) #rule 3 allowed = IMAGES_FORMATS .index {|format| an_uri.path.end_with? format} return false if allowed.nil? #rule 4 return false unless an_uri.query.nil? rescue return false end true end end end end