<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: camping code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 06 Sep 2008 08:09:53 GMT</pubDate>
    <description>DZone Snippets: camping code</description>
    <item>
      <title>response caching in camping</title>
      <link>http://snippets.dzone.com/posts/show/4988</link>
      <description>A basic implementation of response caching in camping.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Camping.goes :MyCampingApp&lt;br /&gt;&lt;br /&gt;module MyCampingApp&lt;br /&gt;  module Controller&lt;br /&gt;    class View &lt; R '/'&lt;br /&gt;      def get&lt;br /&gt;        cache('root') do&lt;br /&gt;          'Expensive operation!'&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def flush(id)&lt;br /&gt;    f = File.dirname(__FILE__) + "/cache/#{id}"&lt;br /&gt;    File.delete(f) if File.exists?(f)&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def cache(id, timeout = 1.hour)&lt;br /&gt;    f = File.dirname(__FILE__) + "/cache/#{id}"&lt;br /&gt;    &lt;br /&gt;    if File.exists?(f) &amp;&amp; (Time.now - File.stat(f).mtime) &lt; timeout&lt;br /&gt;      File.read(f)&lt;br /&gt;    else&lt;br /&gt;      r = yield&lt;br /&gt;      open(f, 'w'){|wr| wr.write(r)}&lt;br /&gt;      r&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def self.create&lt;br /&gt;    cache_dir = File.dirname(__FILE__) + "/cache"&lt;br /&gt;    Dir.mkdir(cache_dir) unless File.directory?(cache_dir)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 16 Jan 2008 13:27:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4988</guid>
      <author>remvee (Remco van 't Veer)</author>
    </item>
    <item>
      <title>Fix for camping multipart parsing issue.</title>
      <link>http://snippets.dzone.com/posts/show/4356</link>
      <description>// description of your code here&lt;br /&gt;I wrote a nice description of this issue, but snippets ate it.  &lt;br /&gt;&lt;br /&gt;In short, this parses complicated form names into nice hashes, i.e. name[asd][asd] for multipart forms.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module Camping&lt;br /&gt;module Base&lt;br /&gt; def initialize(r, e, m) #:nodoc:&lt;br /&gt;      e = H[e.to_hash]&lt;br /&gt;      @status, @method, @env, @headers, @root = 200, m.downcase, e,&lt;br /&gt;          {'Content-Type'=&gt;'text/html'}, e.SCRIPT_NAME.sub(/\/$/,'')&lt;br /&gt;      @k = C.kp(e.HTTP_COOKIE)&lt;br /&gt;      qs = C.qs_parse(e.QUERY_STRING)&lt;br /&gt;      @in = r&lt;br /&gt;      todo = []&lt;br /&gt;      if %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)|n.match(e.CONTENT_TYPE)&lt;br /&gt;        b = /(?:\r?\n|\A)#{Regexp::quote("--#$1")}(?:--)?\r$/&lt;br /&gt;        until @in.eof?&lt;br /&gt;          fh=H[]&lt;br /&gt;          for l in @in&lt;br /&gt;            case l&lt;br /&gt;            when Z: break&lt;br /&gt;            when /^Content-Disposition: form-data;/&lt;br /&gt;              fh.u H[*$'.scan(/(?:\s(\w+)="([^"]+)")/).flatten]&lt;br /&gt;            when /^Content-Type: (.+?)(\r$|\Z)/m&lt;br /&gt;              puts "=&gt; fh[type] = #$1"&lt;br /&gt;              fh[:type] = $1&lt;br /&gt;            end&lt;br /&gt;          end&lt;br /&gt;          fn=fh[:name]&lt;br /&gt;          o=if fh[:filename]&lt;br /&gt;            o=fh[:tempfile]=Tempfile.new(:C)&lt;br /&gt;            o.binmode&lt;br /&gt;          else&lt;br /&gt;            fh=""&lt;br /&gt;          end&lt;br /&gt;          while l=@in.read(16384)&lt;br /&gt;            if l=~b&lt;br /&gt;              o&lt;&lt;$`.chomp&lt;br /&gt;              @in.seek(-$'.size,IO::SEEK_CUR)&lt;br /&gt;              break&lt;br /&gt;            end&lt;br /&gt;            o&lt;&lt; l&lt;br /&gt;          end&lt;br /&gt;          if(fn)&lt;br /&gt;            qs.merge!(C.qs_parse(fn + "=1"))&lt;br /&gt;            parts = fn.split(/\[/)&lt;br /&gt;            line = ""&lt;br /&gt;            parts.each do |pp|&lt;br /&gt;              pp = pp.gsub(/\]/,"")&lt;br /&gt;              line += "['#{pp}']"&lt;br /&gt;            end&lt;br /&gt;            #p "line is:"&lt;br /&gt;            #p line&lt;br /&gt;            #p fh&lt;br /&gt;            todo &lt;&lt; ["qs#{line}", fh]&lt;br /&gt;            #eval("qs#{line} = fh")&lt;br /&gt;            #p qs&lt;br /&gt;          end&lt;br /&gt;          fh[:tempfile].rewind if fh.is_a?H&lt;br /&gt;        end&lt;br /&gt;      elsif @method == "post"&lt;br /&gt;        qs.merge!(C.qs_parse(@in.read))&lt;br /&gt;      end&lt;br /&gt;      #post process vars&lt;br /&gt;      todo.each do |n|&lt;br /&gt;        eval("#{n.first} = n.last")&lt;br /&gt;      end&lt;br /&gt;      #p "END QUERY"&lt;br /&gt;      #p qs&lt;br /&gt;      #exit&lt;br /&gt;      @cookies, @input = @k.dup, qs.dup&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;end &lt;br /&gt;end&lt;br /&gt;// insert code here..&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 26 Jul 2007 01:53:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4356</guid>
      <author>NetherBen (Ben)</author>
    </item>
    <item>
      <title>The Camping Short, Short Example</title>
      <link>http://snippets.dzone.com/posts/show/1781</link>
      <description>Camping encourages short, elegant applications. In this example, we're going to skip the database and put together a simple home page with a few of your favorite links.&lt;br /&gt;&lt;br /&gt;The site can be stored in a single file called home_page.rb:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; #!/usr/local/bin/ruby -rubygems&lt;br /&gt; require 'camping'&lt;br /&gt;&lt;br /&gt; module Camping::Controllers&lt;br /&gt;&lt;br /&gt;   # The root slash shows the `index' view.&lt;br /&gt;   class Index &lt; R '/'&lt;br /&gt;     def get&lt;br /&gt;       render :index &lt;br /&gt;     end&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   # Any other page name gets sent to the view&lt;br /&gt;   # of the same name.&lt;br /&gt;   #&lt;br /&gt;   #   /index -&gt; Views#index&lt;br /&gt;   #   /sample -&gt; Views#sample&lt;br /&gt;   #&lt;br /&gt;   class Page &lt; R '/(\w+)'&lt;br /&gt;     def get(page_name)&lt;br /&gt;       render page_name&lt;br /&gt;     end&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; module Camping::Views&lt;br /&gt;&lt;br /&gt;   # If you have a `layout' method like this, it&lt;br /&gt;   # will wrap the HTML in the other methods.  The&lt;br /&gt;   # `self &lt;&lt; yield' is where the HTML is inserted.&lt;br /&gt;   def layout&lt;br /&gt;     html do&lt;br /&gt;       title { 'My HomePage' }&lt;br /&gt;       body { self &lt;&lt; yield }&lt;br /&gt;     end&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   # The `index' view.  Inside your views, you express&lt;br /&gt;   # the HTML in Ruby.  See http://code.whytheluckystiff.net/markaby/.&lt;br /&gt;   def index&lt;br /&gt;     p 'Hi my name is Charles.'&lt;br /&gt;     p 'Here are some links:'&lt;br /&gt;     ul do&lt;br /&gt;      li { a 'Google', :href =&gt; 'http://google.com' }&lt;br /&gt;      li { a 'A sample page', :href =&gt; '/sample' }&lt;br /&gt;     end&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   # The `sample' view.&lt;br /&gt;   def sample&lt;br /&gt;     p 'A sample page'&lt;br /&gt;   end&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; if __FILE__ == $0&lt;br /&gt;   puts Camping.run&lt;br /&gt; end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Source: &lt;a href="http://code.whytheluckystiff.net/camping/wiki/TheCampingShortShortExample#TheCampingShortShortExample"&gt;The Camping Short, Short Example&lt;/a&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 28 Mar 2006 15:19:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1781</guid>
      <author>413x ()</author>
    </item>
  </channel>
</rss>
