在Heroku上搭建自己的Tiny URL

随着Twitter的风行,TinyURL服务的需求也被挖掘了出来。虽然这个服务器的盈利模式仍然是一个未知数,但是并不能阻止它的流行.网上应用最广泛的的包括tinyurl.com,bit.ly等等。Source TechcrunchSource TechCrunch.

最近读到一篇不错的blog:Clone TinyURL in 40 lines of Ruby code. 这篇blog的作者用短短40行代码就实现了tinyurl的服务,大为感慨地同时让我对ruby越来越喜欢了.

get '/' do haml :index end
post '/' do
uri = URI::parse(params[:original])
raise "Invalid URL" unless uri.kind_of? URI::HTTP or uri.kind_of? URI::HTTPS
@url = Url.first(:original => uri.to_s)
@url = Url.create(:original => uri.to_s) if @url.nil?
haml :index
end
get '/:snipped' do redirect Url[params[:snipped].to_i(36)].original end
error do haml :index end

基于DSL的Sinatra Framework. 接触的不多。不过用在这种比较简洁的应用中真的非常直观好用。
params[:snipped].to_i(36)
Ruby方便的实现了数字的三十六进制(0-9,a-z)也是这段代码的亮点之一.


DataMapper.setup(:default, ENV['DATABASE_URL'] || 'mysql://root:root@localhost/snip')
class Url
include DataMapper::Resource
property :id, Serial
property :original, String, :length => 255
property :created_at, DateTime
def snipped() self.id.to_s(36) end
end

DataMapper简化下的数据持久化.好用也就是一个字啊。


__END__
@@ layout
!!! 1.1
%html
%head
%title Snip!
%link{:rel => 'stylesheet', :href => 'http://www.w3.org/StyleSheets/Core/Modernist', :type => 'text/css'}
= yield
@@ index
%h1.title Snip!
- unless @url.nil?
%code= @url.original
snipped to
%a{:href => env['HTTP_REFERER'] + @url.snipped}
= env['HTTP_REFERER'] + @url.snipped
#err.warning= env['sinatra.error']
%form{:method => 'post', :action => '/'}
Snip this:
%input{:type => 'text', :name => 'original', :size => '50'}
%input{:type => 'submit', :value => 'snip!'}
%small copyright ©
%a{:href => 'http://blog.saush.com'}
Chang Sau Sheong
%br
%a{:href => 'http://github.com/sausheong/snip'}
Full source code

HAML简化下的html模板.第一次看到html写成了code一般。好坏暂且不计,ruby就是牛啊。

看完以后当然就是自己搞一把,heroku现在也支持sinatra了,强强组合,不用不爽快。-,-
Step 1 下载Snip源码
git clone git://github.com/sausheong/snip.git.
一共就两个文件.基于Rack的配置文件config.ru和主程序snip.rb,简单明了,一目了然.

Step 2 上传至Heroku

heroku create yourapp
git push heroku master


另外一个属于ruby on rails的奇迹Heroku,就是这么喜爱.

Step 3 数据初始化

heroku clone
Ruby console for snip.heroku.com
>> DataMapper.auto_migrate!
=> [Url]

OK,搞定了,属于你自己的tiny url service.
我的Tiny URL: http://tinyurl.8gfriends.com,欢迎使用^@^

 

Ruby实例三个

Sinatra
微型ruby web framework.非常的简单使用

require ‘rubygems’
require ‘sinatra’
get ‘/hi’ do
“Hello World!”
end

$ gem install sinatra
$ ruby hi.rb
== Sinatra has taken the stage …
>> Listening on 0.0.0.0:4567

Twitter4r
基于Ruby的Twitter API封装,方便好用。
require ‘twitter’
puts ‘begin…’
#login
client = Twitter::Client.new(:login => ‘tony_luo’, :password =>’gene0801′);
#get login user information
user = client.my(:info)
puts user.name
#get followers information
followers = client.my(:followers)
puts followers.size
#get status according to timeline
client.timeline_for(:public) do |status|
puts status.user.screen_name, status.text
end
#post status
status = client.status(:post, ‘Cisco的收购脚步越来越快了!’)
status = client.status(:get, status.id)
#get messages
messages = client.messages(:received)
messages.each do |message|
puts message.text
end
puts ‘end…’
Twibot
基于Twitter4r和DSL的微型twitter framework,非常的方便好用。
设计本身也可圈可点,正在用它学习Ruby DSL的相关知识。

require ‘twibot’

# Receive messages, and tweet them publicly
#
message do |message, params|
post_tweet message
end

# Respond to @replies if they come from the right crowd
#
reply :from => [:cjno, :irbno] do |message, params|
post_tweet “@#{message.sender.screen_name} I agree”
end

# Listen in and log tweets
#
tweet do |message, params|
puts message
end

 

无觅相关文章插件,快速提升流量