Permalink
Browse files

import to github

  • Loading branch information...
0 parents commit 8c4cead2caf27e3c77d959b94054a9a5c32822f3 @tobi committed Feb 21, 2008
Showing with 110 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +11 −0 README
  3. +22 −0 Rakefile
  4. +1 −0 init.rb
  5. +1 −0 install.rb
  6. +74 −0 lib/mephisto_plugins/text_link_ads.rb
@@ -0,0 +1 @@
+.svn
11 README
@@ -0,0 +1,11 @@
+MephistoTextlinkads
+===================
+
+Simple support for textlinkads.
+
+Example usage:
+
+
+ <ul>
+ {{ 'GC014CRPERVL1BFLJFJG' | textlinkads }}
+ </ul>
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the mephisto_textlinkads plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the mephisto_textlinkads plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'MephistoTextlinkads'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1 @@
+Liquid::Template.register_filter(MephistoPlugins::TextLinkAds)
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1,74 @@
+require 'net/http'
+require 'cgi'
+
+
+module MephistoPlugins
+ module TextLinkAds
+ mattr_accessor :affiliate_number
+ self.affiliate_number = 20722
+
+ class Server
+ class Url < Struct.new(:expiry, :value)
+ def expired?
+ expiry <= Time.now
+ end
+ end
+
+ def initialize(ttl)
+ @ttl = ttl
+ @cache = Hash.new
+ end
+
+ def fetch(url)
+ if @cache.has_key?(url) and not @cache[url].expired?
+ return @cache[url].value
+ end
+
+ xml = http_get(url)
+ if xml
+ url = Url.new(@ttl.from_now, xml)
+ @cache[url] = url
+ return url.value
+ else
+ nil
+ end
+ end
+
+ private
+
+ def http_get(url)
+ Timeout::timeout(20.0) do
+ XmlSimple.xml_in(Net::HTTP.get_response(URI.parse(url)).body.to_s)
+ end
+ rescue StandardError, RuntimeError
+ nil
+ end
+
+ end
+
+
+ include ActionView::Helpers::JavascriptHelper
+
+ def textlinkads(key)
+ req = @context.registers[:controller].request
+ url = "http://www.text-link-ads.com/xml.php?inventory_key=#{key}&referer=#{CGI::escape(req.env['REQUEST_URI'])}&user_agent=#{CGI::escape(req.env['HTTP_USER_AGENT'])}"
+
+ if response = server.fetch(url)
+
+ response['Link'].collect do |link|
+ content_tag :li, %(#{link['BeforeText'].first} <a href="#{link['URL'].first}" title="#{link['Text'].first}">#{link['Text'].first}</a> #{link['AfterText'].first})
+ end.join("\n\t")
+
+ end
+ rescue
+ "<li><a href=\"http://www.text-link-ads.com/?ref=#{affiliate_number}\">Buy links here</a></li>"
+ end
+
+ private
+
+ def server
+ $server ||= Server.new( 60.minutes )
+ end
+
+ end
+end

1 comment on commit 8c4cead

shouldn't line 50 in lib/mephisto_plugins/text_link_ads.rb
be include ActionView::Helpers::JavaScriptHelper
note the capital S

Please sign in to comment.