Sha256: 2c32b55a6306a042622e62538abd8147e16caaf85fb6665a931a479146af598d

Contents?: true

Size: 1.63 KB

Versions: 38

Compression:

Stored size: 1.63 KB

Contents

#
# module RefererManager
#	Management referers in a day. Include in Diary class.
#

module TDiary
	module RefererManager
		private
		#
		# call this method when initialize
		#
		def init_referers
			@referers = {}
			@new_referer = true # for compatibility
		end

		public
		def add_referer( ref, count = 1 )
			newer_referer
			ref = ref.sub( /#.*$/, '' ).sub( /\?\d{8}$/, '' )
			if /^([^:]+:\/\/)([^\/]+)/ =~ ref
				ref = $1 + $2.downcase + $'
			end
			begin
				uref = CGI::unescape( ref )
			rescue ::Encoding::CompatibilityError
				return
			end
			if pair = @referers[uref] then
				pair = [pair, ref] if pair.class != Array # for compatibility
				@referers[uref] = [pair[0] + count, pair[1]]
			else
				@referers[uref] = [count, ref]
			end
		end

		def clear_referers
			@referers = {}
		end

		def count_referers
			@referers.size
		end

		def each_referer( limit = 10 )
			newer_referer
			# dirty workaround to avoid recursive sort that
			# causes SecurityError in @secure=true
			# environment since
			# http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=16081
			@referers.values.sort_by{|e| "%08d_%s" % e}.reverse.each_with_index do |ary,idx|
				break if idx >= limit
				yield ary[0], ary[1]
			end
		end

		private
		def newer_referer
			unless @new_referer then # for compatibility
				@referers.keys.each do |ref|
					count = @referers[ref]
					if count.class != Array then
						@referers.delete( ref )
						@referers[CGI::unescape( ref )] = [count, ref]
					end
				end
				@new_referer = true
			end
		end
	end
end

# Local Variables:
# mode: ruby
# indent-tabs-mode: t
# tab-width: 3
# ruby-indent-level: 3
# End:
# vim: ts=3

Version data entries

38 entries across 28 versions & 1 rubygems

Version Path
tdiary-5.3.0 lib/tdiary/referer_manager.rb
tdiary-5.2.4 lib/tdiary/referer_manager.rb
tdiary-5.2.3 lib/tdiary/referer_manager.rb
tdiary-5.2.2 lib/tdiary/referer_manager.rb
tdiary-5.2.1 lib/tdiary/referer_manager.rb
tdiary-5.2.0 lib/tdiary/referer_manager.rb
tdiary-5.1.7 lib/tdiary/referer_manager.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/lib/tdiary/referer_manager.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/lib/tdiary/referer_manager.rb
tdiary-5.1.6 lib/tdiary/referer_manager.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/lib/tdiary/referer_manager.rb
tdiary-5.1.5 lib/tdiary/referer_manager.rb
tdiary-5.1.4 lib/tdiary/referer_manager.rb
tdiary-5.1.3 lib/tdiary/referer_manager.rb
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/tdiary-5.0.14/lib/tdiary/referer_manager.rb
tdiary-5.1.2 lib/tdiary/referer_manager.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/tdiary-5.0.14/lib/tdiary/referer_manager.rb
tdiary-5.1.1 lib/tdiary/referer_manager.rb
tdiary-5.1.0 lib/tdiary/referer_manager.rb
tdiary-5.0.14 lib/tdiary/referer_manager.rb