# frozen_string_literal: true# This code was created based on https://github.com/myronmarston/vcr/blob/master/lib/vcr/util/version_checker.rb# Thanks to @myronmarston# Copyright (c) 2010-2012 Myron Marston# Permission is hereby granted, free of charge, to any person obtaining# a copy of this software and associated documentation files (the# "Software"), to deal in the Software without restriction, including# without limitation the rights to use, copy, modify, merge, publish,# distribute, sublicense, and/or sell copies of the Software, and to# permit persons to whom the Software is furnished to do so, subject to# the following conditions:# The above copyright notice and this permission notice shall be# included in all copies or substantial portions of the Software.# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.moduleWebMockclassVersionCheckerdefinitialize(library_name,library_version,min_patch_level,max_minor_version=nil,unsupported_versions=[])@library_name,@library_version=library_name,library_version@min_patch_level,@max_minor_version=min_patch_level,max_minor_version@unsupported_versions=unsupported_versions||[]@major,@minor,@patch=parse_version(library_version)@min_major,@min_minor,@min_patch=parse_version(min_patch_level)ifmax_minor_version@max_major,@max_minor=parse_version(max_minor_version)else@max_major,@max_minor=nil,nilend@comparison_result=compare_versionenddefcheck_version!warn_about_too_lowiftoo_low?warn_about_too_highiftoo_high?warn_about_unsupported_versionifunsupported_version?endprivatedeftoo_low?@comparison_result==:too_lowenddeftoo_high?@comparison_result==:too_highenddefunsupported_version?@unsupported_versions.include?(@library_version)enddefwarn_about_too_lowwarn_in_red"You are using #{@library_name}#{@library_version}. "+"WebMock supports version #{version_requirement}."enddefwarn_about_too_highwarn_in_red"You are using #{@library_name}#{@library_version}. "+"WebMock is known to work with #{@library_name}#{version_requirement}. "+"It may not work with this version."enddefwarn_about_unsupported_versionwarn_in_red"You are using #{@library_name}#{@library_version}. "+"WebMock does not support this version. "+"WebMock supports versions #{version_requirement}."enddefwarn_in_red(text)Kernel.warncolorize(text,"\e[31m")enddefcompare_versioncasewhen@major<@min_majorthen:too_lowwhen@major==@min_major&&@minor<@min_minorthen:too_lowwhen@major==@min_major&&@minor==@min_minor&&@patch<@min_patchthen:too_lowwhen@max_major&&@major>@max_majorthen:too_highwhen@max_major&&@major==@max_major&&@max_minor&&@minor>@max_minorthen:too_highelse:okendenddefversion_requirementreq=">= #{@min_patch_level}"req+=", < #{@max_major}.#{@max_minor+1}"if@max_minorreq+=", except versions #{@unsupported_versions.join(',')}"unless@unsupported_versions.empty?reqenddefparse_version(version)version.split('.').map{|v|v.to_i}enddefcolorize(text,color_code)"#{color_code}#{text}\e[0m"endendend