Using Yan in Ruby Web Application

Mon 21 December 2009
  • 把戏 tags:
  • captcha
  • ruby
  • web
  • Yan published: true comments: true

I will show you the usage of Yan captcha service. In this tutorial, it's based on a simple ruby web application of the Sinatra web framework.

Before we start to use the service, it is necesary to get Yan running. Download the code from the project page, then build and run it with maven: mvn jetty:run

To enable the application to use Yan, we have to register our application to get an API Key. If you use Yan 0.3, there is a secret registration page at http://localhost:8080/yan/reg.jsp The page is protected by HTTP Basic Authentication, the username and password are store in 'realm.properties' which is considered to locate in the root directory. Open the file you can see the plain text username and password. If you are running the latest development version, there is no long any UI for API Key creation, but restful interface. This won't be hard to you, pickup your tools such as curl or poster (a firefox extension) to send a HTTP request. Take curl as example, do it like this: curl -X PUT "http://localhost:8080/yan/apikey/" -d "SinatraTestApp" -u "username:password"

If it works, you will get a line of json: {"apikey":"b251b0dc2eed31cac38555b61d4fa6a453923bfd","appName":"SinatraTestApp"} Save this apikey.

Sinatra is generally considered to be the world's lightest and smallest web framework. And our application is rather simple. Just check the code:

require "rubygems"
require "sinatra"
require "net/http"
require "yaml"

apikey='b251b0dc2eed31cac38555b61d4fa6a453923bfd'

get '/' do
conn = Net::HTTP.new('localhost', 8080)
q = "ip=#{@env['REMOTE_ADDR']}&apikey=#{apikey}&alt=yaml&mode=0"
resp, data = conn.get("/yan/ticket?#{q}")
@ticket = YAML::load(data)
haml :sinatra_captcha
end

post '/' do
conn = Net::HTTP.new('localhost', 8080)
q = "ip=#{@env['REMOTE_ADDR']}&apikey=#{apikey}&key=#{params['key']}&code=#{params['captcha']}"
resp, data = conn.get("/yan/validate?#{q}")
data
end

use_in_file_templates!
__END__

@@ sinatra_captcha
%html
%head
%title Yan Captcha on Sinatra
%body
%form{:action=>"/", :method=>"post"}
%p
Username:
%input{:name=>"username", :type=>"text"}
%p
Password:
%input{:name=>"password", :type=>"password"}
%p
Captcha:
%img{:src=>@ticket['url']}
%br
%input{:name=>"captcha", :type=>"text"}
%input{:name=>"key", :type=>"hidden", :value=>@ticket['key']}
%input{:type=>'submit'} There are two parts of this application: ruby code and haml. I just use in-file-template for convenience. We define a get handler and a post handler on the path '/'. The get handler will request a ticket from Yan which contains captcha image url and ticket key. The post handler will extract user input and submit the Yan's validator and return user the result. And the HAML code is template for page rendering after GET request.

Maybe you need to install sinatra and some dependency: sudo gem install sinatra haml

Run the code with a build-in WEBrick ruby sinatra-yan.rb

Browse to the default url, test it:

For another similar tutorial using python, check Yan's wiki page: http://bitbucket.org/sunng/yan/wiki/SampleCode

Thank you for your support. btw, today is my dear girl friend's birthday, I just wish her happy everyday.