26th
Tue
寄付の募り方
http://tin
手軽なマネタイズ方法としてつけることも検討してみよう
posted by
satoko
on Tue 26 Feb 2008
at 07:38
http://tin
手軽なマネタイズ方法としてつけることも検討してみよう
下記URLから入手できます。
http://svn
URLを渡すとTinyURLに変換してリンクを作ります。
1 module ApplicationHelper 2 include TinyurlHel per 3 end
1 <%= link_to_tinyurl "http://www .yahoo.co. jp" %>
1 script/generate plugin tinyurl_he lper
1 require 'net/http' 2 3 module TinyurlHelper 4 def link_to_ti nyurl(url, html_optio ns = nil) 5 uri = 'http://tin yurl.com/a pi-create. php?url=' + url 6 uri = URI.parse(uri) 7 tiny_url = Net::HTTP.get_respon se(uri).body 8 options = {:title => url, :alt => url} 9 options = html_optio ns.nil? ? options : options.merge(html_optio ns) 10 link_to tiny_url, tiny_url, options 11 end 12 end
テストを書く(アプリテスト、プラグイン単体テスト両方通るように書くと吉)
1 - 2 require 'test/unit' 3 require File.expand_path(File.dirname(__FILE__) + "/../lib/ti nyurl_help er") 4 5 class TinyurlHel perTest < Test::Unit ::TestCase 6 include TinyurlHel per 7 8 #dummy link_to 9 def link_to(name, options = {}, html_optio ns = nil) 10 [name, html_optio ns[:title]] 11 end 12 13 def test_link_ to_tinyurl 14 url = 'http://www .yahoo.co. jp/' 15 tiny_url, title = link_to_ti nyurl(url) 16 assert_equ al 'http://tin yurl.com/9 10', tiny_url 17 assert_equ al url, title 18 end 19 20 def test_link_ to_tinyurl _with_titl e 21 url = 'http://www .yahoo.co. jp/' 22 tiny_url, title = link_to_ti nyurl(url, {:title => 'title'}) 23 assert_equ al 'http://tin yurl.com/9 10', tiny_url 24 assert_equ al 'title', title 25 end 26 end
README, MIT-LICENS
公開用リポジトリにコミット
1 ActionView::Base.send :include, TinyurlHel per