<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://coderack.org/</id>
  <title>CodeRack</title>
  <subtitle>A warm place to hang your code</subtitle>
  <link type="text/html" href="http://coderack.org/" rel="alternate"/>
  <link type="application/atom+xml" href="http://coderack.org/middlewares.xml" rel="self"/>
  <updated>2012-03-15 17:16:42 UTC</updated>
  <author>
    <name>Lunar Logic Polska</name>
    <email>info@coderack.org</email>
  </author>
  <entry>
    <id>http://coderack.org/middlewares/154-rack-taint</id>
    <title>rack-taint</title>
    <published>2012-03-15 17:13:05 UTC</published>
    <updated>2012-03-15 17:16:42 UTC</updated>
    <link href="http://coderack.org/middlewares/154-rack-taint"/>
    <content type="html">&lt;p&gt;Taint the query string (and thus GET parameters), input (and thus POST parameters), headers (and thus cookies), and everything else that comes in on a request.&lt;/p&gt;
&lt;p&gt;Among other use cases, this may prove helpful as a component in a &lt;a href="http://jkfill.com/2012/03/10/preventing-mass-assignment-injection-in-rails/"&gt;scheme that limits mass assignment in Rails&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Add to your &lt;code&gt;Gemfile&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem 'rack-taint'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If it's not Rails, you'll also need to add &lt;code&gt;Rack:Taint&lt;/code&gt; to your application stack. Earlier is better.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>http://coderack.org/middlewares/153-rackfunkycache</id>
    <title>Rack::FunkyCache</title>
    <published>2011-11-12 21:07:34 UTC</published>
    <updated>2011-11-12 21:07:34 UTC</updated>
    <link href="http://coderack.org/middlewares/153-rackfunkycache"/>
    <content type="html">&lt;p&gt;Rack middleware which together with Passenger implements funky caching.&lt;/p&gt;&lt;p&gt;Funky caching is technique popularized by &lt;span class="caps"&gt;PHP&lt;/span&gt;.net site. It was first mentioned by Rasmus Lerdorf in 2002. Middleware captures Rack output and caches it as static file to public directory. All subsequent requests are then served by Passenger and they never reach Rack.&lt;/p&gt;&lt;p&gt;Check usage from &lt;a href="http://www.appelsiini.net/projects/rack-funky-cache"&gt;project page&lt;/a&gt;.&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>http://coderack.org/middlewares/152-rackfacebookmethodfix</id>
    <title>Rack::Facebook::MethodFix</title>
    <published>2011-11-06 22:06:28 UTC</published>
    <updated>2011-11-06 22:06:28 UTC</updated>
    <link href="http://coderack.org/middlewares/152-rackfacebookmethodfix"/>
    <content type="html">&lt;p&gt;In early 2011 Facebook started to send all application requests as &lt;span class="caps"&gt;POST&lt;/span&gt;. This middleware converts &lt;span class="caps"&gt;POST&lt;/span&gt; requests back to &lt;span class="caps"&gt;GET&lt;/span&gt; when applicable.&lt;/p&gt;&lt;p&gt;Rack::Facebook::MethodFix looks for incoming &lt;span class="caps"&gt;POST&lt;/span&gt; requests. If the request contains signed_request parameter it converts request to &lt;span class="caps"&gt;GET&lt;/span&gt; as originally intended. Optionally if you pass in Facebook application secret_id it will also require the contents of signed_request to be valid.&lt;/p&gt;&lt;p&gt;Check usage from &lt;a href="http://www.appelsiini.net/projects/rack-facebook-methodfix"&gt;project page&lt;/a&gt;.&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>http://coderack.org/middlewares/151-naughty-step</id>
    <title>Naughty Step</title>
    <published>2011-10-03 08:11:49 UTC</published>
    <updated>2012-03-16 17:54:38 UTC</updated>
    <link href="http://coderack.org/middlewares/151-naughty-step"/>
    <content type="html">&lt;p&gt;A Rack middleware for simple 404 and 500 status handling&lt;/p&gt;&lt;p&gt;= Naughty Step&lt;/p&gt;
&lt;p&gt;Well, Naughty Step is nothing ground-breaking. This is just a basic way of catching errors (Status 500) and pages not found (Status 404) and use related html pages you&amp;#8217;ve written for that purpose. The middleware even has default values:&lt;/p&gt;
&lt;p&gt;- public/404.html&lt;br /&gt;
- public/500.html&lt;/p&gt;
&lt;p&gt;So a very simple example might be to require the Gem and then:&lt;/p&gt;
&lt;p&gt;use ::Rack::NaughtyStep&lt;/p&gt;
&lt;p&gt;Or if you want to declare your paths:&lt;/p&gt;
&lt;p&gt;use ::Rack::NaughtyStep &amp;#8216;/path/to/404/page.html&amp;#8217;, &amp;#8216;/path/to/500/page.html&amp;#8217;&lt;/p&gt;
&lt;p&gt;Here you go. Truth is that I&amp;#8217;ve been naughty as well. I did not write a proper full example, neither a spec. Just because I was in a rush, but I&amp;#8217;ll do it as soon as I can. Please don&amp;#8217;t spank me.&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>http://coderack.org/middlewares/150-rack-golem</id>
    <title>rack-golem</title>
    <published>2011-09-23 08:23:22 UTC</published>
    <updated>2011-10-03 08:05:41 UTC</updated>
    <link href="http://coderack.org/middlewares/150-rack-golem"/>
    <content type="html">&lt;p&gt;A Controller middleware that is euh&amp;#8230; basic. I would say it is a sort of Ramaze for kids&lt;/p&gt;&lt;h1&gt;GOLEM&lt;/h1&gt;

&lt;p&gt;I would describe Golem as a Ramaze for kids.&lt;br/&gt;
Golem is not a framework though, just a controller, but you know... the kind of controller that leaves you in the train
even if you did not buy a ticket.&lt;br/&gt;
It leaves you on the rails if you will (incredibly good pun intended).&lt;/p&gt;

&lt;p&gt;Install with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo gem install rack-golem
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Config.ru is one of his names, so say it in a Rackup file.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'db' # Loads ORM models and all
require 'go' # Our controller (I don not like that word really)
use Rack::ContentLength
use Rack::Session::Cookies
run Go
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And the winner is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'rack/golem'

class Go
    include Rack::Golem # To hell with sub-classes !!!

    before do
      # Here you can do many things
      # In order to help you here are some variables you can read and override:
      # @r =&amp;gt; the Rack::Request object
      # @res =&amp;gt; the Rack::Response object
      # @action =&amp;gt; Name of the public method that will handle the request
      # @action_arguments =&amp;gt; Arguments for the action (really?)
    end

    def index(*args)
      # When no public method is found
      # Of course you don't have to declare one and it is gonna use Controller#not_found instead
      # But if it is declared, keep in mind it's a catch-all so make it deal with args
      @articles = Post.all
      erb :index
    end

    def post(id=nil)
      @post = Post[id]
      if @post.nil?
        not_found
      else
        erb :post
      end
    end

    def say(listener='me', *words)
      "Hey #{listener} I don't need ERB to tell you that #{words.join(' ')}"
    end

    def not_found(*args)
      # This one is defined by Golem but here we decided to override it
      # Like :index this method receives the arguments in order to make something with it
      Email.alert('Too many people are looking for porn here') if args.includes?("porn")
      super(args)
    end

    after do
      Spy.analyse.send_info_to([:government, :facebook, :google, :james_bond])
    end

  end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Hopefully no headache.&lt;/p&gt;

&lt;h1&gt;WHAT GOLEM DOES NOT&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Support templates other than ERB (I plan to use Tilt more cleverly though in order to achieve that without selling my soul)&lt;/li&gt;
&lt;li&gt;Session/Cookies administration (Like for many things, use a middleware instead ex: Rack::Session::Cookies)&lt;/li&gt;
&lt;li&gt;Prepare the coffee (Emacs does but Ed is the standard text editor)&lt;/li&gt;
&lt;li&gt;So many things, why bother...&lt;/li&gt;
&lt;/ul&gt;

</content>
  </entry>
  <entry>
    <id>http://coderack.org/middlewares/149-rack-backend-api</id>
    <title>rack-backend-api</title>
    <published>2011-09-21 15:25:51 UTC</published>
    <updated>2011-10-03 08:05:21 UTC</updated>
    <link href="http://coderack.org/middlewares/149-rack-backend-api"/>
    <content type="html">&lt;p&gt;The purpose of this Rack Middleware is to provide an &lt;span class="caps"&gt;API&lt;/span&gt; that interfaces with database actions in order to build a &lt;span class="caps"&gt;CMS&lt;/span&gt;.&lt;/p&gt;&lt;h1&gt;HOW TO USE IT&lt;/h1&gt;

&lt;p&gt;BackendAPI is a Rack middleware that you have to put before your actual backend/CMS,
and generally after an authentication middleware.
And it takes care of everything involving interaction with your database.&lt;/p&gt;

&lt;p&gt;In reality, it does not HAVE to be with the Backend but it makes sense and that way,
both share the authentication middleware.&lt;/p&gt;

&lt;p&gt;A rackup stack for your application might look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;map '/' do
  run Frontend
end

map '/admin' do
  use Rack::Auth::Basic, "your-realm" do |username, password|
    [username, password] == ['username', 'password']
  end
  use BackendAPI
  run Backend.new
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Your backend receives every request that the Restful API doesn't recognise.
The BackendAPI recognises requests following this scheme:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;METHOD /Backend-path/model_class/ID
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The ID is not always relevant.
So if you have a model class called BlogPost and you want to get the form for the entry with ID 4:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;GET /admin/blog_post/4
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you don't put an ID, it means you want the form for a brand new entry:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;GET /admin/blog_post
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then if you need to delete the entry with ID 4:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;DELETE /admin/blog_post/4
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The API also understands a CamelCased class name:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;DELETE /admin/BlogPost/4
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is my fave personally, but unfortunately it seems that windows servers are case insensitive.
Which means that if you have one, you need to stick with the under_scored names.&lt;/p&gt;

&lt;p&gt;To be honest, that is almost everything you need because the ORM adapter builds the forms
and therefore use the right action and method for POST and PUT requests.&lt;/p&gt;

&lt;p&gt;The problem sometimes with a Restful API is that in real life,
in spite of the fact that not every requests are GET or POST it is sometimes forced.
The href of a link is always a GET, and the method for a form is
overridden if it is not GET or POST.&lt;/p&gt;

&lt;p&gt;This is why Rack has a very handy middleware called MethodOverride.
You don't have to &lt;code&gt;use&lt;/code&gt; it because BackendAPI puts it on the stack for you.
Basically when you have it, you can send the method you really wanted in the POSTed parameter called "_method",
and the middleware override the method for you.
This is how the adapter makes forms with PUT requests.&lt;/p&gt;

&lt;p&gt;But unfortunately you can only use MethodOverride on POST requests,
but you might want to have it on links.&lt;/p&gt;

&lt;p&gt;Here is a concrete example:&lt;br/&gt;
You want to put in your CMS a link for deleting blog post.
But a link is going to be a GET request.
Of course you could use Ajax and anyway you probably will,
but it is a good practice to make it possible without javascript.
So your link could look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;a href="/admin/blog_post/4?_method=DELETE"&amp;gt; X &amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But it doesn't work because links are GET requests.
Fortunately this is a common task so there is a method that makes DELETE buttons available as a form:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@blog_post.backend_delete_form("/admin/blog_post/4", { :destination =&amp;gt; "/admin/list/blog_post" })
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;:destination&lt;/code&gt; is where you go when the job is done.
You also can change the option &lt;code&gt;:submit_text&lt;/code&gt; which is what the button says.
By default, the DELETE form button says "X".&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;:destination&lt;/code&gt; option is also in the API as &lt;code&gt;_destination&lt;/code&gt;.
Use it in order to specify where to go when the entry is validated.
Because before it is validated you'll get the form again with error messages.&lt;/p&gt;

&lt;p&gt;Say we need a link for creating a blog post, and then when validated, we want to go back to the list page:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;a href="/admin/blog_post?_destination=%2Fadmin%2Flist%2Fblog_post"&amp;gt; Create new Blog Post &amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Of course, the page &lt;code&gt;/admin/list/blog_post&lt;/code&gt; is a page of your Backend/CMS.
The form will be POSTed because there is no ID, which means it is a new entry.
On that list page, you could have a list of your posts with an "Edit" link:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;My Interesting Post Number 4 - &amp;lt;a href="/admin/blog_post/4?_destination=%2Fadmin%2Flist%2Fblog_post"&amp;gt; Edit &amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You also have another option called &lt;code&gt;fields&lt;/code&gt; which allows you to say which fields you want in that form.
The purpose of that is mainly to be able to edit a single value at a time:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Title: My Super Blog - &amp;lt;a href="/admin/blog_post/4?fields[]=title&amp;amp;_destination=%2Fadmin%2Flist%2Fblog_post"&amp;gt; Edit &amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will make a link to a form for editing the title of that Blog Post.
Please note that the option &lt;code&gt;fields&lt;/code&gt; is an array.&lt;/p&gt;

&lt;p&gt;Also don't forget to escape the URI like in the examples above.
You can do that with Rack::Utils :&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;::Rack::Utils.escape "/admin/list/blog_post"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The option &lt;code&gt;:submit_text&lt;/code&gt; is also available through the API as &lt;code&gt;_submit_text&lt;/code&gt;.
It says "SAVE" by default but you might want it to say "CREATE" and "UPDATE" in appropriate cases,
like we did in the example.&lt;/p&gt;

&lt;p&gt;Another thing to note is that you don't have to use a destination for when something is created or updated.
If you do not use destination, the API will call the instance method &lt;code&gt;Model#backend_show&lt;/code&gt; on the entry.
By default it just says &lt;code&gt;'OK'&lt;/code&gt; but you can override the method in order to send whatever you want.
This comes handy when you use ajax and want a representation of the entry once it's created.&lt;/p&gt;

&lt;h2&gt;More Info&lt;/h2&gt;

&lt;p&gt;For further details on how to use the middleware, please visit the &lt;a href="http://github.com/mig-hub/backend-api"&gt;github page of the project&lt;/a&gt;.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>http://coderack.org/middlewares/148-rack-cerberus</id>
    <title>rack-cerberus</title>
    <published>2011-09-21 15:19:34 UTC</published>
    <updated>2011-10-03 08:05:32 UTC</updated>
    <link href="http://coderack.org/middlewares/148-rack-cerberus"/>
    <content type="html">&lt;p&gt;A Rack middleware for form-based authentication. Aim is a compromise between fonctionality, beauty and customization.&lt;/p&gt;&lt;h1&gt;Cerberus&lt;/h1&gt;

&lt;p&gt;Cerberus is a Rack middleware for form-based authentication. Its purpose is only
to offer a nicer (or more actual) replacement for Basic HTTP authentication.&lt;/p&gt;

&lt;p&gt;Install with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# sudo gem install rack-cerberus
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can use it almost the same way you use &lt;code&gt;Rack::Auth::Basic&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'cerberus'
use Rack::Session::Cookie, :secret =&amp;gt; 'change_me'
use Cerberus do |login, pass|
  pass=='secret'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Like in that example, make sure you have a session, because Cerberus use it for
persistent login.&lt;/p&gt;

&lt;p&gt;There is an optional hash you can add for customisation it. Options are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;:company_name&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:fg_color&lt;/code&gt; (foreground color)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:bg_color&lt;/code&gt; (background color)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:text_color&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:icon_url&lt;/code&gt; (for a company logo or any icon)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:css_location&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Which is used that way:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;use Cerberus, {:company_name =&amp;gt; 'Nintendo'} do |login, pass|
  pass=='secret'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The purpose of Cerberus is to be basic, which is why there are enough options to have
a page fairly customized with colors and logo (&lt;code&gt;:icon_url&lt;/code&gt;). The logo can even replace
the company name if you leave &lt;code&gt;:company_name&lt;/code&gt; blank. But should you be fussy, this is possible
to have more control using an external CSS file with the option &lt;code&gt;:css_location&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Just like &lt;code&gt;Rack::Auth::Basic&lt;/code&gt;, Cerberus yields login and pass, and delegate authentication
to the block you send it which should return a boolean.&lt;/p&gt;

&lt;p&gt;If you want to see a concrete example, go into the Cerberus directory and run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# rackup example.ru
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It's gonna start the example at http://localhost:9292&lt;/p&gt;

&lt;h2&gt;Logout&lt;/h2&gt;

&lt;p&gt;Any request to &lt;code&gt;/logout&lt;/code&gt; on the path where the middleware is mounted will log you out.
In other words, if you put the middleware at &lt;code&gt;/admin&lt;/code&gt;, query &lt;code&gt;/admin/logout&lt;/code&gt; to be
logged out. Pretty simple.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>http://coderack.org/middlewares/147-ennou</id>
    <title>Ennou</title>
    <published>2011-08-28 14:38:30 UTC</published>
    <updated>2011-08-28 14:38:30 UTC</updated>
    <link href="http://coderack.org/middlewares/147-ennou"/>
    <content type="html">&lt;p&gt;Rack middleware for Windows (Http.sys).&lt;/p&gt;&lt;p&gt;Ennou interacts between Rack and Http.sys without &lt;span class="caps"&gt;GVL&lt;/span&gt;. It consists two different middleware. Ennou &amp;#8211; for developing server for single process and EnnouMu &amp;#8211; for production level server for management multiple processes.&lt;/p&gt;&lt;p&gt;rackup -s Ennou&lt;br /&gt;
or&lt;br /&gt;
rackup -s Ennoumu&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>http://coderack.org/middlewares/146-rack-track</id>
    <title>rack-track</title>
    <published>2011-08-03 09:09:56 UTC</published>
    <updated>2011-08-09 09:46:00 UTC</updated>
    <link href="http://coderack.org/middlewares/146-rack-track"/>
    <content type="html">&lt;p&gt;Makes it easy to manage and document tracking pixels on different pages and areas by keeping them all in a central location.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;MyApp::Application.config.middleware.use(Rack::Track) do
  area :confirmation_pages, "/checkout/order_confirmation", "/basket/complete"

  pixel "Generic GA", :on =&amp;gt; :all_pages do
    %Q{
      &amp;lt;!-- GOOGLE ANALYTICS --&amp;gt; 
      blah
      &amp;lt;!-- END GOOGLE ANALYTICS --&amp;gt; 
    }
  end

  pixel "Goal GA", :on =&amp;gt; :confirmation_pages do
    %Q{
      &amp;lt;!-- GOOGLE ANALYTICS --&amp;gt; 
      blah
      &amp;lt;!-- END GOOGLE ANALYTICS --&amp;gt; 
    }
  end
end
&lt;/code&gt;&lt;/pre&gt;
</content>
  </entry>
  <entry>
    <id>http://coderack.org/middlewares/145-closed</id>
    <title>Closed</title>
    <published>2011-07-02 22:02:02 UTC</published>
    <updated>2011-10-03 08:15:00 UTC</updated>
    <link href="http://coderack.org/middlewares/145-closed"/>
    <content type="html">&lt;p&gt;For websites that are open 9 till 5&lt;/p&gt;&lt;p&gt;During office hours website displays as usual. Outside of office hours a closed page is displayed.&lt;/p&gt;&lt;p&gt;see github readme&lt;/p&gt;</content>
  </entry>
</feed>

