2013年6月19日 星期三

[Web Technology] Ruby CGI & Application

We can use ruby to process something about web application that use CGI to request server to process .


Ruby CGI tutorial

↑This web is a very good way to learn anything about RUBY. so ,we  do not make sample to

 show how Ruby CGI does. Just show example for ruby CGI in www technology.

Let 's talk about a trip web first .

EveryTrail

It can let people to search key words for the information of the place that they want to

go to . We use the web to make a example about ruby application .

<information>

[Server] : Apache

[Ruby]  : Ruby 1.9.3

First , we must the declaration first , and import library .

#!/usr/bin/env ruby
# encoding: UTF-8
require 'cgi'
require 'open-uri'

Because we need to catch the source code of a web page for the purpose of getting

trip information .

We can use the "open-uri" and  regular expression to process this part.

setting CGI and contentType for XML

cgi = CGI.new(:accept_charset => "UTF-8")
print cgi.header("text/xml")



// get source code 
codeString = open(url).read.encode!('UTF-8','UTF-8',:invalid =>:replace)

// regular expression
commentRegexp = /(\w+)/
commentRegexp1 = /<!-- start guides[\D\d]*end guides -->/
commentRegexp2 = /<img src="http[\d\D]+?>/
commentRegexp3 = /img src="([\d\D]+?)"/
commentRegexp4 = /title="([\d\D]+?)"/
commentRegexp5 = /<div class='small-text light-text'>[\s]{8}([\w \S]+)[\s]{21}<\/div>/
commentRegexp6 = /([A-Z].+?)\\t/
commentRegexp7 = /id="star1" title='\d\/5'|<div class="left" style="margin-left:0px">/
commentRegexp8 = /title='(\d)\/5'/
commentRegexp9 = /<a href='(.+?)'>/
commentRegexp10 = /\/.+[0-9a-zA-Z-]/
commentRegexp11 = /Viewed.(\d+?) times/

//code for getting information by using regular expression
#
# get needed data by using regular expression
#
codeString.scan(commentRegexp1){|matched|
    guideData = matched
}
#
# get attribute and value(Image and Title)
#
guideData.scan(commentRegexp2){|matched|
    getImg_Title += matched
}
#
# get Image Data
#
getImg_Title.scan(commentRegexp3){|matched|
    h = Hash.new
    h.store("Image",matched)
    resultData.push(h)
}
#
# get Title Data
#
getImg_Title.scan(commentRegexp4){|matched|
    name = ""
    name = matched
    resultData[i].store("Name",name)
    i = i + 1
}
i = 0 #initialize to zero
#
# get Location Data
#
guideData.scan(commentRegexp5){|matched|
        resultData[i].store("Location",matched)
        i = i + 1
}
i = 0
#
# get Rank Data
#
guideData.scan(commentRegexp7){|matched|
    if commentRegexp8 =~ matched.to_s
        matched.to_s.scan(commentRegexp8){|matched2|
            resultData[i].store("Rating out of 5",matched2)
            i = i + 1
        }
    else
        resultData[i].store("Rating out of 5",'N')
        i = i + 1
    end
}
i = 0
#
# get views times
#
guideData.scan(commentRegexp9){|matched|
    matched.to_s.scan(commentRegexp10){|matched2|
        resultData[i].store("Link",url2 + matched2.to_s)
        open(url2 + matched2.to_s){|f|
            f.each_line{|line|
                line.to_s.scan(commentRegexp11){|matched3|
                resultData[i].store("Viewed Times",matched3)
                break
                }
            }
        }
    break
    }
    i = i + 1
}
i = 0


Finally , print out to XML format
#
# print xml
#
puts "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
puts "<trips total=\"" + resultData.size.to_s + "\">"
while i < resultData.size do
        puts "<trip>"
        puts "<trip_name><![CDATA[" + resultData[i].fetch("Name")[0] + "]]></trip_name>"
        puts "<location><![CDATA[" + resultData[i].fetch("Location")[0] + "]]></location>"
        puts "<view_times><![CDATA[" + resultData[i].fetch("Viewed Times")[0] + "]]></view_times>"
        puts "<rating_out_of_5><![CDATA[" + resultData[i].fetch("Rating out of 5")[0] + "]]></rating_out_of_5>"
        puts "<image_url><![CDATA[" + resultData[i].fetch("Image")[0] + "]]></image_url>"
        puts "<guide_url><![CDATA[" + resultData[i].fetch("Link") + "]]></guide_url>"
        puts "</trip>"
        i = i + 1
end
puts "</trips>"



it will show XML about for the trip the program found.

You can get all source code from here . Click me

沒有留言:

張貼留言