<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Webmaster-Source &#187; Ruby</title>
	<atom:link href="https://www.webmaster-source.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.webmaster-source.com</link>
	<description>Useful Resources For Webmasters</description>
	<lastBuildDate>Thu, 24 Aug 2017 02:01:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Finding a Website&#8217;s Favicon with Ruby</title>
		<link>https://www.webmaster-source.com/2013/09/25/finding-a-websites-favicon-with-ruby/</link>
		<comments>https://www.webmaster-source.com/2013/09/25/finding-a-websites-favicon-with-ruby/#comments</comments>
		<pubDate>Wed, 25 Sep 2013 11:46:02 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[favicon]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=5187</guid>
		<description><![CDATA[For a project I&#8217;ve been working on, I wanted to to have my Sidekiq worker (which is part of an RSS crawler) discover the favicon for a web site and cache it for later display. It was fun figuring out a way to do this, so I just had to share. A Brief History of [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>For a project I&#8217;ve been working on, I wanted to to have my <a href="http://sidekiq.org/">Sidekiq</a> worker (which is part of an RSS crawler) discover the favicon for a web site and cache it for later display. It was fun figuring out a way to do this, so I just had to share.</p>
<h3>A Brief History of Favicons</h3>
<p><a href="http://en.wikipedia.org/wiki/Favicon">Favicons</a>, or &#8220;shortcut icons,&#8221; can be defined in multiple ways. Like all too many things in web design, browsers handle them in slightly different and mildly incompatible ways, meaning there&#8217;s plenty of redundancy. Favicons came to be when Microsoft added them to Internet Explorer 5 in 1999, implementing a feature where the browser would check the server for a file named <code>favicon.ico</code> and display it in certain parts of the UI. The following year, the W3C published a standard method for defining a favicon. Rather than simply having the browser look for a file in the root directory, an HTML document should specify a file in the header with a <code>&lt;link&gt;</code> tag, just like with stylesheets.<span id="more-5187"></span></p>
<p>Fast forward to the present, and you have a bit of screwiness.</p>
<ul>
<li>All major web browsers check for the link tag first, and fall back to <code>favicon.ico</code> if it&#8217;s not found.</li>
<li>You can define multiple icons in the HTML header. You can have ICO/PNG/GIF formats, as well as different sizes.</li>
<li>Some browsers support larger 32&#215;32 favicons, while others will only use the 16&#215;16 ones. Chrome for Mac prefers the 32&#215;32 ones, and scales them down to 16&#215;16 on Macs without Retina displays.</li>
<li>Big Bad Internet Explorer only supports ICO files for favicons, not PNGs.</li>
</ul>
<p>The most compatible way to set up your favicon is to define both 32&#215;32 and 16&#215;16 icons in your header, using the PNG format, and make a 16&#215;16 ICO formatted one to name &#8220;favicon.ico&#8221; and drop into your web root. Browsers that play nicely will use the PNG ones in whatever dimensions they prefer, and IE will fall back to the ICO file.</p>
<h3>Writing the Class</h3>
<p>Now that the history lesson is out of way, you can see why there&#8217;s a little bit of a challenge here. Depending on how badly you want to find and display that icon, you may have to write logic for the different methods. For this tutorial, I will focus on two. The simplest, which is looking to see if there&#8217;s a <code>favicon.ico</code>, and a basic implementation of checking for a link tag defining a shortcut icon.</p>
<p>Before we do anything else, we need to install a few dependencies. Either add them to your Gemfile and do a <code>bundle install</code>, or use the <code>gem install</code> command to install them manually.</p>
<ul>
<li><a href="http://johnnunemaker.com/httparty/">HTTParty</a> (<code>gem install httparty</code>)</li>
<li><a href="http://nokogiri.org/">Nokogiri</a> (<code>gem install nokogiri</code>)</li>
</ul>
<p>Now require the necessary libraries at the top of a new Ruby file and we can get going.</p>
<pre class="brush: ruby; title: ; notranslate">
require &quot;httparty&quot;
require &quot;nokogiri&quot;
require &quot;base64&quot;
</pre>
<p>We can define a class to make a nice, clean interface for this to keep it modular and easier to reuse. As you can see below, I&#8217;ve made a <code>Favicon</code> class and added some accessors for instance variables, as well as an <code>initialize</code> method that assigns the parameter it receives to the <code>@host</code> instance variable before calling the method we will be defining next.</p>
<pre class="brush: ruby; title: ; notranslate">
require &quot;httparty&quot;
require &quot;nokogiri&quot;
require &quot;base64&quot;


class Favicon


  attr_reader :host
  attr_reader :uri
  attr_reader :base64


  def initialize(host)
    @host = host
    check_for_ico_file
  end


end
</pre>
<p>We&#8217;ll be implementing the simplest part first. The <code>check_for_ico_file</code> method will send an HTTP GET request to <code>/favicon.ico</code> on the server specified in @host and check to see if a file exists. (The server will send a <code>200 OK</code> response if it does, and a <code>404 Not Found</code> error otherwise.) If it does, the URL will be saved to an instance variable and the icon file&#8217;s contents will be base64 encoded before being saved to an instance variable as well.</p>
<p>The HTTParty gem is great for this, since it drastically simplifies simple HTTP requests like this.</p>
<pre class="brush: ruby; title: ; notranslate">
# Check /favicon.ico
def check_for_ico_file
  uri = URI::HTTP.build({:host =&gt; @host, :path =&gt; '/favicon.ico'}).to_s
  res = HTTParty.get(uri)
  if res.code == 200
    @base64 = Base64.encode64(res.body)
    @uri = uri
  end
end
</pre>
<p>If you want, you could go ahead and instantiate the class to try out what we have so far. If you pass it the domain name of a site that uses the <code>/favicon.ico</code> convention, the object should find it without issue.</p>
<pre class="brush: ruby; title: ; notranslate">
favicon = Favicon.new(&quot;arstechnica.com&quot;)

puts favicon.uri
#Outputs http://arstechnica.com/favicon.ico

puts favicon.base64
#Outputs a bunch of base64-encoded gibberish. More on this later

puts puts favicon.host
#Outputs arstechnica.com
</pre>
<p>Now let&#8217;s handle link tags! The process for that is a little bit more in-depth. First we need to request a web page from the server, such as the index page, and parse it for tags that resemble <code>&lt;link rel="shortcut icon" href="..." /&gt;</code>. Then we have to evaluate the contents of <code>href</code> to make sure it&#8217;s an absolute URL, and prepend the domain name if it is not. After that, we can finally make a request to get the icon itself and save it.</p>
<p>Still with me? Excellent, now here&#8217;s the code to do that. I&#8217;ll comment it a little more thoroughly, since it looks messier at a glance.</p>
<pre class="brush: ruby; title: ; notranslate">
# Check &quot;shortcut icon&quot; tag
def check_for_html_tag

  # Load the index page with HTTParty and pass the contents to Nokogiri for parsing
  uri = URI::HTTP.build({:host =&gt; @host, :path =&gt; '/'}).to_s
  res = HTTParty.get(uri)
  doc = Nokogiri::HTML(res)

  # Use an xpath expression to tell Nokogiri what to look for.
  doc.xpath('//link[@rel=&quot;shortcut icon&quot;]').each do |tag|

    # This is the contents of the &quot;href&quot; attribute, which we pass to Ruby's URI module for analysis
    taguri = URI(tag['href'])

    unless taguri.host.to_s.length &lt; 1
      # There is a domain name in taguri, so we're good
      iconuri = taguri.to_s
    else
      # There is no domain name in taguri. It's a relative URI!
      # So we have to join it with the index URL we built at the beginning of the method
      iconuri = URI.join(uri, taguri).to_s
    end

    # Grab the icon and set the instance variables
    res = HTTParty.get(iconuri)
    if res.code == 200
      @base64 = Base64.encode64(res.body)
      @uri = iconuri
    end
    
  end

end
</pre>
<p>Now there&#8217;s one more thing to do before we&#8217;re done. The initialize method needs to be tweaked so it calls our newest method:</p>
<pre class="brush: ruby; title: ; notranslate">
def initialize(host)
  @host = host
  check_for_ico_file
  check_for_html_tag
end
</pre>
<p>Now the class will check for the <code>favicon.ico</code> file first, then the HTML tag. If the HTML tag is present, it will take precedence.</p>
<p class="alertbubble"><strong>Available as a Gist!</strong> For your convenience, the results of this tutorial are <a href="https://gist.github.com/redwallhp/6692349">available as a GitHub Gist.</a></p>
<p></p>
<h3>Using the Class</h3>
<p>Now all you have to do is include the class with a require statement, and grab favicons.</p>
<pre class="brush: ruby; title: ; notranslate">
require &quot;favicon&quot;

favicon = Favicon.new(&quot;arstechnica.com&quot;)

puts favicon.uri
#Outputs http://static.arstechnica.net/favicon.ico

puts favicon.base64
#Outputs a bunch of base64-encoded gibberish. More on this later

puts puts favicon.host
#Outputs arstechnica.com
</pre>
<p>Now&#8230;what of that &#8220;base64-encoded gibberish?&#8221; It&#8217;s the perfect format for a little trick called Data URIs, which you can <a href="http://css-tricks.com/data-uris/">read all about over at CSS-Tricks</a>. If you cache that base64 string somewhere, probably in a database, you can output it like so:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;img width=&quot;16&quot; height=&quot;16&quot; alt=&quot;favicon&quot; src=&quot;data:image/gif;base64,BLAHBBLAHGIBBERISHGOESHERE&quot; /&gt;
</pre>
<p>It will display like any other image, but won&#8217;t use an additional HTTP request, because the image data is already embedded on the page. This makes it perfect for a list of web sites with icons beside them. Instead of kicking off several HTTP requests for individual tiny images, you just embed them right in the page.</p>
<p>If you&#8217;re unfortunate enough that you must support antique versions of Internet Explorer (version seven or prior) then you can&#8217;t use Data URIs, as they were not supported. However, all is not lost. You could conceivably adapt the class and have it write the image data to files on the server instead of base64-encoding them.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.webmaster-source.com/2013/09/25/finding-a-websites-favicon-with-ruby/feed/</wfw:commentRss>
		<slash:comments>101</slash:comments>
		</item>
		<item>
		<title>Dashing — The Exceptionally Handsome Dashboard Framework</title>
		<link>https://www.webmaster-source.com/2013/09/18/dashing-the-exceptionally-handsome-dashboard-framework/</link>
		<comments>https://www.webmaster-source.com/2013/09/18/dashing-the-exceptionally-handsome-dashboard-framework/#comments</comments>
		<pubDate>Wed, 18 Sep 2013 11:37:56 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[Software & Scripts]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=5183</guid>
		<description><![CDATA[Need to throw together a quick dashboard with live-updated information and statistical readouts? Dashing is a fun new framework built atop Sinatra that lets you quickly setup dashboards, much in the style of Microsoft&#8217;s &#8220;Metro&#8221; UI. You can leverage premade widgets (which include numerical readouts, meters, graphs and lists) or make your own with HTML, [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Need to throw together a quick dashboard with live-updated information and statistical readouts? <a href="http://shopify.github.io/dashing/">Dashing</a> is a fun new framework built atop <a href="http://www.sinatrarb.com/">Sinatra</a> that lets you quickly setup dashboards, much in the style of Microsoft&#8217;s &#8220;Metro&#8221; UI. You can leverage premade widgets (which include numerical readouts, meters, graphs and lists) or make your own with HTML, SCSS, CoffeeScript and a bit of Ruby. Dashing uses Batman.js and rufus-scheduler to run server-side jobs at scheduled intervals and update the browser.</p>
<p>A live demo of Dashing can be <a href="http://dashingdemo.herokuapp.com/sample">seen here</a>.</p>
<p style="text-align: center;"><a href="http://shopify.github.io/dashing/"><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-5184 imgborder" alt="Dashing" src="//www.webmaster-source.com/wp-content/uploads/2013/09/dashing-framework.png" width="600" height="342" /></a></p>
<p>I know I&#8217;m going to have a bit of fun setting up personal dashboards that pull in various fun statistics. There are already quite a few <a href="https://github.com/Shopify/dashing/wiki/Additional-Widgets">pre-made widgets</a> out there, which should be helpful.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.webmaster-source.com/2013/09/18/dashing-the-exceptionally-handsome-dashboard-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Try Ruby: A Clever Interactive Programming Tutorial</title>
		<link>https://www.webmaster-source.com/2011/04/15/try-ruby-a-clever-interactive-programming-tutorial/</link>
		<comments>https://www.webmaster-source.com/2011/04/15/try-ruby-a-clever-interactive-programming-tutorial/#comments</comments>
		<pubDate>Fri, 15 Apr 2011 11:28:01 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=4010</guid>
		<description><![CDATA[Teaching a newbie how to program is a difficult task, whether you&#8217;re writing a book, recording a screencast or teaching a class. Similarly, it&#8217;s a bit of a hassle for someone who is proficient with one or two languages to pick up a new one. Try Ruby is a clever interactive tutorial that would work [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Teaching a newbie how to program is a difficult task, whether you&#8217;re writing a book, recording a screencast or teaching a class. Similarly, it&#8217;s a bit of a hassle for someone who is proficient with one or two languages to pick up a new one.</p>
<p><a href="http://tryruby.org/">Try Ruby</a> is a clever interactive tutorial that would work well for either scenario. It takes the form of an in-browser command line Ruby interpreter with a pane along the bottom that gives you instructions. It tells you to type in a command, and explains what exactly is going on.</p>
<p><a href="http://tryruby.org/"><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-4011" title="TryRuby.org" src="//www.webmaster-source.com/wp-content/uploads/tryruby-org.jpg" alt="" width="600" height="411" /></a></p>
<p>Doesn&#8217;t that make you want to learn Ruby? I went through a few steps, from basic arithmetic to strings to arrays, and it was kind of fun. Having to type out everything as you go seems to reinforce your understanding of what you&#8217;re doing better than simply reading a book. And once you&#8217;ve got the language down, you should be able to move on to watching screencasts for the Rails framework. I may have to give Ruby a try sometime.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.webmaster-source.com/2011/04/15/try-ruby-a-clever-interactive-programming-tutorial/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/


Served from: www.webmaster-source.com @ 2026-06-10 05:04:30 by W3 Total Cache
-->