<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: uri code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 16 May 2008 10:42:38 GMT</pubDate>
    <description>DZone Snippets: uri code</description>
    <item>
      <title>ProjectX client-side code</title>
      <link>http://snippets.dzone.com/posts/show/5150</link>
      <description>This Ruby code uses a unified XML format to create a password record on a web server. It's intended to be run as a batch file which gets called from another Ruby application called maintain_projectx which gets called from a cronjob.&lt;br /&gt;&lt;br /&gt;In this example the password is stored on the server not for authentication but simply to provide a reminder service in the event the user forgets it.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'net/http'&lt;br /&gt;require 'rexml/document'&lt;br /&gt;include REXML&lt;br /&gt;&lt;br /&gt;class ProjectXClient&lt;br /&gt;  attr :doc&lt;br /&gt;  def initialize(raw_url)&lt;br /&gt;    url = URI.escape(raw_url)&lt;br /&gt;    xml_data = Net::HTTP.get_response(URI.parse(url)).body&lt;br /&gt;    @doc = Document.new(xml_data)&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if __FILE__ == $0&lt;br /&gt;&lt;br /&gt;  xml_project = &lt;&lt;PROJECT&lt;br /&gt;  &lt;project name='password'&gt;&lt;br /&gt;    &lt;method name='create'&gt;&lt;br /&gt;      &lt;params&gt;&lt;br /&gt;        &lt;param var='password' val='p6789c'/&gt;&lt;br /&gt;        &lt;param var='title' val='hotmail'/&gt;&lt;br /&gt;      &lt;/params&gt;&lt;br /&gt;    &lt;/method&gt;&lt;br /&gt;  &lt;/project&gt;&lt;br /&gt;PROJECT&lt;br /&gt;  &lt;br /&gt;  pxc = ProjectXClient.new("http://yourdomain.com/p/projectx.cgi?xml_project=" + xml_project)&lt;br /&gt;  doc = pxc.doc&lt;br /&gt;  puts doc&lt;br /&gt;    &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output -what's returned from the server is an XML response containing a result. The result echos the method executed and the output from that method, which in this instance is the xml record node 'entry'.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;result method='rtn_create'&gt;&lt;br /&gt;  &lt;append id='19367'&gt;&lt;br /&gt;    &lt;entry id='19367'&gt;&lt;br /&gt;      &lt;password&gt;p6789c&lt;/password&gt;&lt;br /&gt;      &lt;title&gt;hotmail&lt;/title&gt;&lt;br /&gt;      &lt;description/&gt;&lt;br /&gt;    &lt;/entry&gt;&lt;br /&gt;  &lt;/append&gt;&lt;br /&gt;&lt;/result&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Mon, 18 Feb 2008 18:49:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5150</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Rails URL Validation</title>
      <link>http://snippets.dzone.com/posts/show/4532</link>
      <description>No regexes, allows URLs with ports or IPs. Inspiration from &lt;a href="http://actsasblog.wordpress.com/2006/10/16/url-validation-in-rubyrails/"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  validates_each :href, :on =&gt; :create do |record, attr, value|&lt;br /&gt;    begin&lt;br /&gt;      uri = URI.parse(value)&lt;br /&gt;      if uri.class != URI::HTTP&lt;br /&gt;        record.errors.add(attr, 'Only HTTP protocol addresses can be used')&lt;br /&gt;      end&lt;br /&gt;    rescue URI::InvalidURIError&lt;br /&gt;      record.errors.add(attr, 'The format of the url is not valid.')&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Sep 2007 14:03:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4532</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>find out the current url / uri in *.rhtml file</title>
      <link>http://snippets.dzone.com/posts/show/2841</link>
      <description>// find out the current url / uri in *.rhtml file&lt;br /&gt;// is quite simple with the request object&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% page = request.request_uri %&gt;&lt;br /&gt;page: &lt;%= page %&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;// but then different urls mean the same page &lt;br /&gt;// (../admin = ../admin/ = ../admin/index = ..admin/index/)&lt;br /&gt;// and maybe the id is unwanted too (../admin/show/8)&lt;br /&gt;// so below is an alternative with control on which parameter is used&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %&gt;&lt;br /&gt;page: &lt;%= page %&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Mon, 16 Oct 2006 19:40:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2841</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
    <item>
      <title>URL/URI Validations in Rails</title>
      <link>http://snippets.dzone.com/posts/show/2563</link>
      <description>Here is a quick/basic URI validation library for your Rails app: &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'net/http'&lt;br /&gt;&lt;br /&gt;# Original credits: http://blog.inquirylabs.com/2006/04/13/simple-uri-validation/&lt;br /&gt;# HTTP Codes: http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTPResponse.html&lt;br /&gt;&lt;br /&gt;class ActiveRecord::Base&lt;br /&gt;  def self.validates_uri_existence_of(*attr_names)&lt;br /&gt;    configuration = { :message =&gt; "is not valid or not responding", :on =&gt; :save, :with =&gt; nil }&lt;br /&gt;    configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)&lt;br /&gt;    &lt;br /&gt;    raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp)&lt;br /&gt;    &lt;br /&gt;    validates_each(attr_names, configuration) do |r, a, v|&lt;br /&gt;        if v.to_s =~ configuration[:with] # check RegExp&lt;br /&gt;              begin # check header response&lt;br /&gt;                  case Net::HTTP.get_response(URI.parse(v))&lt;br /&gt;                    when Net::HTTPSuccess then true&lt;br /&gt;                    else r.errors.add(a, configuration[:message]) and false&lt;br /&gt;                  end &lt;br /&gt;              rescue # Recover on DNS failures.. &lt;br /&gt;                  r.errors.add(a, configuration[:message]) and false&lt;br /&gt;              end&lt;br /&gt;        else&lt;br /&gt;          r.errors.add(a, configuration[:message]) and false&lt;br /&gt;        end &lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Save the code above into a file in your 'lib' directory. ex: validates_uri_existence_of.rb. Include the file in your environment.rb (ex: require 'validates_uri_existence_of'). &lt;br /&gt;&lt;br /&gt;And now you can use the validator in any model by calling:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;validates_uri_existence_of :url, :with =&gt;&lt;br /&gt;        /(^$)|(^(http|https)://[a-z0-9] ([-.]{1}[a-z0-9] )*.[a-z]{2,5}(([0-9]{1,5})?/.*)?$)/ix&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Above code validates the URI/URL against a regular expression first, and then does a HEAD query to confirm that the page is alive (HTTPSuccess code is returned). You could easily modify the code to extend the functionality as you wish. &lt;br /&gt;&lt;br /&gt;More info in &lt;a href="http://www.igvita.com/blog/2006/09/07/validating-url-in-ruby-on-rails/"&gt; this post.&lt;/a&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 08 Sep 2006 07:56:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2563</guid>
      <author>igrigorik (Ilya Grigorik)</author>
    </item>
    <item>
      <title>uri reader</title>
      <link>http://snippets.dzone.com/posts/show/2558</link>
      <description>// reads and extracts regexp using open-uri&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require "open-uri"; include OpenURI&lt;br /&gt;alpha=('a'..'z').to_a&lt;br /&gt;&lt;br /&gt;snames=File.new('stations.txt','w')&lt;br /&gt;alpha.each {|a|&lt;br /&gt;open("http://www.irishrail.ie/your_journey/stationpicker.asp?id=#{a}&amp;box=From") {|f|&lt;br /&gt;    f.each_line {|line| snames.puts line}&lt;br /&gt;}}&lt;br /&gt;&lt;br /&gt;snames=File.new('stations.txt','r')&lt;br /&gt;data=snames.read&lt;br /&gt;rx1=/sendValue\('([a-zA-Z]+)/&lt;br /&gt;sarr=data.scan(rx1)&lt;br /&gt;stations=sarr.collect{|s| s[0]}&lt;br /&gt;stations.uniq!&lt;br /&gt;puts stations&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 07 Sep 2006 15:26:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2558</guid>
      <author>mazz70 (kjk)</author>
    </item>
    <item>
      <title>Using Rio from Ruby to easily save a Web page to file</title>
      <link>http://snippets.dzone.com/posts/show/2432</link>
      <description>Found at &lt;a href="http://www.juretta.com/log/2006/08/13/ruby_net_http_and_open-uri/"&gt;http://www.juretta.com/log/2006/08/13/ruby_net_http_and_open-uri/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# (sudo) gem install rio&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'rio'&lt;br /&gt;# open an URI an copy the content into a file&lt;br /&gt;rio('http://www.juretta.com/') &gt; rio('juretta_index.html')&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Cool syntax!</description>
      <pubDate>Wed, 16 Aug 2006 21:38:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2432</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
    <item>
      <title>Validate URIs by Pinging the Server</title>
      <link>http://snippets.dzone.com/posts/show/1913</link>
      <description>&lt;code&gt;&lt;br /&gt;require 'open-uri'&lt;br /&gt;&lt;br /&gt;class ActiveRecord::Base&lt;br /&gt;  def self.validates_uri_existence_of(*attr_names)&lt;br /&gt;    configuration = { :message =&gt; "is not a valid web address" }&lt;br /&gt;    configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)&lt;br /&gt;    validates_each attr_names do |m, a, v|&lt;br /&gt;      begin&lt;br /&gt;        # Try to open the URI&lt;br /&gt;        open v&lt;br /&gt;      rescue&lt;br /&gt;        # Report the error if it throws an exception&lt;br /&gt;        m.errors.add(a, configuration[:message])&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Details on my blog.</description>
      <pubDate>Fri, 14 Apr 2006 02:12:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1913</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
    <item>
      <title>URI Encoding in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/1260</link>
      <description>To encode the web parameters for a URL query string.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'uri'&lt;br /&gt;val = URI.escape("my parameter value")&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 21 Jan 2006 23:30:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1260</guid>
      <author>cyu (Calvin Yu)</author>
    </item>
    <item>
      <title>Make string usable as a URI</title>
      <link>http://snippets.dzone.com/posts/show/328</link>
      <description>This function will turn any string into a safe string usable in a URI.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;function dirify($s) {&lt;br /&gt;     $s = convert_high_ascii($s);  ## convert high-ASCII chars to 7bit.&lt;br /&gt;     $s = strtolower($s);           ## lower-case.&lt;br /&gt;     $s = strip_tags($s);       ## remove HTML tags.&lt;br /&gt;     $s = preg_replace('!&amp;[^;\s]+;!','',$s);         ## remove HTML entities.&lt;br /&gt;     $s = preg_replace('![^\w\s.]!','',$s);           ## remove non-word/space/period chars.&lt;br /&gt;     $s = preg_replace('!\s+!','-',$s);               ## change space chars to dashes.&lt;br /&gt;     return $s;    &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function convert_high_ascii($s) {&lt;br /&gt; 	$HighASCII = array(&lt;br /&gt; 		"!\xc0!" =&gt; 'A',    # A`&lt;br /&gt; 		"!\xe0!" =&gt; 'a',    # a`&lt;br /&gt; 		"!\xc1!" =&gt; 'A',    # A'&lt;br /&gt; 		"!\xe1!" =&gt; 'a',    # a'&lt;br /&gt; 		"!\xc2!" =&gt; 'A',    # A^&lt;br /&gt; 		"!\xe2!" =&gt; 'a',    # a^&lt;br /&gt; 		"!\xc4!" =&gt; 'Ae',   # A:&lt;br /&gt; 		"!\xe4!" =&gt; 'ae',   # a:&lt;br /&gt; 		"!\xc3!" =&gt; 'A',    # A~&lt;br /&gt; 		"!\xe3!" =&gt; 'a',    # a~&lt;br /&gt; 		"!\xc8!" =&gt; 'E',    # E`&lt;br /&gt; 		"!\xe8!" =&gt; 'e',    # e`&lt;br /&gt; 		"!\xc9!" =&gt; 'E',    # E'&lt;br /&gt; 		"!\xe9!" =&gt; 'e',    # e'&lt;br /&gt; 		"!\xca!" =&gt; 'E',    # E^&lt;br /&gt; 		"!\xea!" =&gt; 'e',    # e^&lt;br /&gt; 		"!\xcb!" =&gt; 'Ee',   # E:&lt;br /&gt; 		"!\xeb!" =&gt; 'ee',   # e:&lt;br /&gt; 		"!\xcc!" =&gt; 'I',    # I`&lt;br /&gt; 		"!\xec!" =&gt; 'i',    # i`&lt;br /&gt; 		"!\xcd!" =&gt; 'I',    # I'&lt;br /&gt; 		"!\xed!" =&gt; 'i',    # i'&lt;br /&gt; 		"!\xce!" =&gt; 'I',    # I^&lt;br /&gt; 		"!\xee!" =&gt; 'i',    # i^&lt;br /&gt; 		"!\xcf!" =&gt; 'Ie',   # I:&lt;br /&gt; 		"!\xef!" =&gt; 'ie',   # i:&lt;br /&gt; 		"!\xd2!" =&gt; 'O',    # O`&lt;br /&gt; 		"!\xf2!" =&gt; 'o',    # o`&lt;br /&gt; 		"!\xd3!" =&gt; 'O',    # O'&lt;br /&gt; 		"!\xf3!" =&gt; 'o',    # o'&lt;br /&gt; 		"!\xd4!" =&gt; 'O',    # O^&lt;br /&gt; 		"!\xf4!" =&gt; 'o',    # o^&lt;br /&gt; 		"!\xd6!" =&gt; 'Oe',   # O:&lt;br /&gt; 		"!\xf6!" =&gt; 'oe',   # o:&lt;br /&gt; 		"!\xd5!" =&gt; 'O',    # O~&lt;br /&gt; 		"!\xf5!" =&gt; 'o',    # o~&lt;br /&gt; 		"!\xd8!" =&gt; 'Oe',   # O/&lt;br /&gt; 		"!\xf8!" =&gt; 'oe',   # o/&lt;br /&gt; 		"!\xd9!" =&gt; 'U',    # U`&lt;br /&gt; 		"!\xf9!" =&gt; 'u',    # u`&lt;br /&gt; 		"!\xda!" =&gt; 'U',    # U'&lt;br /&gt; 		"!\xfa!" =&gt; 'u',    # u'&lt;br /&gt; 		"!\xdb!" =&gt; 'U',    # U^&lt;br /&gt; 		"!\xfb!" =&gt; 'u',    # u^&lt;br /&gt; 		"!\xdc!" =&gt; 'Ue',   # U:&lt;br /&gt; 		"!\xfc!" =&gt; 'ue',   # u:&lt;br /&gt; 		"!\xc7!" =&gt; 'C',    # ,C&lt;br /&gt; 		"!\xe7!" =&gt; 'c',    # ,c&lt;br /&gt; 		"!\xd1!" =&gt; 'N',    # N~&lt;br /&gt; 		"!\xf1!" =&gt; 'n',    # n~&lt;br /&gt; 		"!\xdf!" =&gt; 'ss'&lt;br /&gt; 	);&lt;br /&gt; 	$find = array_keys($HighASCII);&lt;br /&gt; 	$replace = array_values($HighASCII);&lt;br /&gt; 	$s = preg_replace($find,$replace,$s);&lt;br /&gt;     return $s;&lt;br /&gt;}&lt;/code&gt;</description>
      <pubDate>Thu, 26 May 2005 15:43:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/328</guid>
      <author>ceejayoz (ceejayoz)</author>
    </item>
    <item>
      <title>Canonical path</title>
      <link>http://snippets.dzone.com/posts/show/187</link>
      <description>This function transforms an HTTP request path (ie, $_SERVER['PATH_INFO']) into its canonical form, removing empty path elements and relative path elements (//, /./, /../). It assumes that there is a trailing slash on the path if it's a directory.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;function canonical_path($path) {&lt;br /&gt;    $canonical = preg_replace('|/\.?(?=/)|','',$path);&lt;br /&gt;    while (($collapsed = preg_replace('|/[^/]+/\.\./|','/',$canonical,1)) !== $canonical) {&lt;br /&gt;        $canonical = $collapsed;&lt;br /&gt;    }&lt;br /&gt;    $canonical = preg_replace('|^/\.\./|','/',$canonical);&lt;br /&gt;    return $canonical;&lt;br /&gt;}&lt;/code&gt;</description>
      <pubDate>Sun, 17 Apr 2005 08:59:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/187</guid>
      <author>adrian (Adrian Sampson)</author>
    </item>
  </channel>
</rss>
