| Line | |
|---|
| 1 | # pathtraq_counter.rb $Revision 1.0 $ |
|---|
| 2 | # |
|---|
| 3 | # Copyright (c) 2008 SHIBATA Hiroshi <shibata.hiroshi@gmail.com> |
|---|
| 4 | # You can redistribute it and/or modify it under GPL2. |
|---|
| 5 | # |
|---|
| 6 | |
|---|
| 7 | require 'timeout' |
|---|
| 8 | require 'open-uri' |
|---|
| 9 | require 'rubygems' |
|---|
| 10 | require 'json/ext' |
|---|
| 11 | |
|---|
| 12 | def call_pathtraq_json( url, mode ) |
|---|
| 13 | json = nil |
|---|
| 14 | begin |
|---|
| 15 | timeout(10) do |
|---|
| 16 | open( "http://api.pathtraq.com/page_counter?api=json&url=#{url}&m=#{mode}" ) do |f| |
|---|
| 17 | json = JSON.parse( f.read ) |
|---|
| 18 | end |
|---|
| 19 | end |
|---|
| 20 | rescue => e |
|---|
| 21 | @conf.debug( e ) |
|---|
| 22 | end |
|---|
| 23 | return json |
|---|
| 24 | end |
|---|
| 25 | |
|---|
| 26 | def pathtraq_counter |
|---|
| 27 | url = @conf.base_url |
|---|
| 28 | mode = ['popular', 'hot', 'upcoming'] |
|---|
| 29 | |
|---|
| 30 | r = %Q|<ul>\n| |
|---|
| 31 | begin |
|---|
| 32 | mode.each do |m| |
|---|
| 33 | json = call_pathtraq_json( url, m ) |
|---|
| 34 | r << %Q|<li>#{m}: #{json["count"]} hits</li>\n| unless json.nil? |
|---|
| 35 | end |
|---|
| 36 | rescue => e |
|---|
| 37 | @conf.debug( e ) |
|---|
| 38 | end |
|---|
| 39 | r << %Q|</ul>\n| |
|---|
| 40 | r << %Q|<div class="iddy"><span class="iddy-powered">Powered by <a href="http://pathtraq.com/">Pathtraq</a></span></div>\n| |
|---|
| 41 | |
|---|
| 42 | return r |
|---|
| 43 | end |
|---|