Gittr http://www.gittr.com eval("eval(\"eval(\\\"puts 'a ruby weblog'\\\")\")") Mon, 01 Oct 2012 17:02:06 +0000 en hourly 1 http://wordpress.org/?v=3.2.1 Manually Resolving a Rails Route http://www.gittr.com/index.php/archive/manually-resolving-a-rails-route/ http://www.gittr.com/index.php/archive/manually-resolving-a-rails-route/#comments Mon, 01 Oct 2012 17:02:06 +0000 cschneid http://www.gittr.com/?p=134 "show", :controller => "accounts", :id => "1" } This appears to be undocumented, but ohh well. Now it’s written up here :)]]> (Rails 3.1)

We had a need to take a string that had a route (/accounts/1) and get back the controller it belonged to (AccountsController).

The way to do that is:


MyApp::Application.routes.recognize_path("/accounts/1")

which returns


{
:action => "show",
:controller => "accounts",
:id => "1"
}

This appears to be undocumented, but ohh well. Now it’s written up here :)

]]>
http://www.gittr.com/index.php/archive/manually-resolving-a-rails-route/feed/ 0
Ruby Spreadsheet Gem Doesn’t Play Nice With BigDecimal http://www.gittr.com/index.php/archive/ruby-spreadsheet-gem-doesnt-play-nice-with-bigdecimal/ http://www.gittr.com/index.php/archive/ruby-spreadsheet-gem-doesnt-play-nice-with-bigdecimal/#comments Thu, 02 Aug 2012 14:55:29 +0000 cschneid http://www.gittr.com/?p=131 In a report I was building, we were doing all our underlying math in BigDecimal for accuracy. But when I go to spool it out to an excel export via Spreadsheet, it fails pretty miserably with very odd behaviors.

A cell that should have ‘1844.05’ instead had the value: ‘1844.046875’.

I didn’t go hunting much further than lib/spreadsheet/excel/writer/worksheet.rb, but there are lots of explicit typechecks in there, and apparently BigDecimal confuses them.

Quick Fix: use .to_f on your BigDecimals before handing them off

Correct Fix: Use the Axlsx gem instead, which seems a bunch more stable, and nicer. Plus it spits out .xlsx files instead of the stupid BIFF8 .xls files.

]]>
http://www.gittr.com/index.php/archive/ruby-spreadsheet-gem-doesnt-play-nice-with-bigdecimal/feed/ 0
Sinatra Cookie handling in 0.9.4 http://www.gittr.com/index.php/archive/sinatra-cookie-handling-in-0-9-4/ http://www.gittr.com/index.php/archive/sinatra-cookie-handling-in-0-9-4/#comments Wed, 23 Sep 2009 15:33:17 +0000 admin http://www.gittr.com/?p=109 I had an older post about cookie handling with Sinatra, and a few details are out of date.  This is a current view (as of September 2009) of how cookies work in Sinatra.

First, you need to set cookies via the response object, as Sinatra no longer has a helper for it.

response.set_cookie("foo", "bar")

Second, set_cookie always takes two arguments. If you need to set advanced options, pass a hash:

# the large number is several years in the future, in seconds
response.set_cookie("foo", {:value => "bar", :expiration => Time.today + 94608000}

There are a good handful of options available. One gotcha with the expires option is that it must respond to .gmtime, which the Date object does not. So you need to use Time or DateTime as far as I can tell.

set_cookie("thing", { :value => "thing2",
                      :domain => myDomain,
                      :path => myPath,
                      :expires => Time.today,
                      :secure => true,
                      :httponly => true } )

To read the underlying code for setting cookies, it’s in the rack gem, rack-1.0.0/lib/rack/response.rb line 56.

To fetch cookies, it’s simply calling the cookie method:

cookies["foo"] # => "bar"
]]>
http://www.gittr.com/index.php/archive/sinatra-cookie-handling-in-0-9-4/feed/ 7
CouchDB Skeleton for Monk http://www.gittr.com/index.php/archive/couchdb-skeleton-for-monk/ http://www.gittr.com/index.php/archive/couchdb-skeleton-for-monk/#comments Thu, 27 Aug 2009 17:37:51 +0000 admin http://www.gittr.com/?p=104 Monk is starting to take off, and people are already starting to make new skeletons specific for their own needs. Check out lazybones by TobyJoe for a CouchDB based skeleton. (Github)

]]>
http://www.gittr.com/index.php/archive/couchdb-skeleton-for-monk/feed/ 1
Monk – Quickstart Sinatra Projects http://www.gittr.com/index.php/archive/monk-quickstart-sinatra-projects/ http://www.gittr.com/index.php/archive/monk-quickstart-sinatra-projects/#comments Thu, 20 Aug 2009 22:25:43 +0000 admin http://www.gittr.com/?p=92 A few coworkers at Citrusbyte (@soveran, @djanowski) have been working on Monk, a new open source web framework.  At its core is Sinatra, with a set of bundled technologies to help new projects get moving as quick as possible.  It helps you figure out the best structure for your Sinatra app, and gets you moving quickly with logging, settings, and testing. 

Monk consists of several components:

Monk Binary

At the core of Monk is a command line tool for starting new projects.  Think of the rails command, and what it does and you have a good idea of what the monk command does.  It lists, manages and instantiates new skeletons for your project. The Monk binary can instatiate any skeleton you want, which means it is a great way to start projects in any language.

Monk Skeletons

A Monk skeleton is a set of files defining the structure of a new project.  There are almost no rules here, the new project can be Sinatra, Django, an ircbot setup, or anything else you want, heck, it could even be Rails. 

Monk Default Skeleton

The default Monk skeleton is where lots of work has gone in, and where you’ll find the meat of Monk.  It’s a set of amazing tools forming a full-stack Sinatra based web development platform.

  • Sinatra - super lightweight web DSL. You know what this is (and if you don’t, go read the rest of this blog, and check it out)
  • Ohm - data access layer using Redis, a stupid fast key-value database.  Super simple models for your app
  • Monk Glue - a Reloader, Logger, and Settings handler for your new application. Don’t worry about it, Sinatra Logging finally made easy…  Monk Glue is destined to keep growing too, including a handful of mix & match helpers for common tasks.
  • Rack-Test, Contest, Stories, Webrat, Faker, Spawn - An amazing unit and integration test stack, allowing both low level unit tests of your code, and high level integration tests, similar in style to Cucumber, but without that nasty repeating-yourself part.  You owe it to yourself to try the stories & webrat approach to testing your Sinatra application.
  • Dependencies - trivial management of all the gems you rely on. Just define a dependencies file, and the dependencies gem will handle vendoring and unpacking your requirements.

Monk Glue

Logging - a perennial problem and hassle in setting up new projects is now simple with Monk, and Monk Glue. 

get '/' do
  logger.info("Entered homepage route")
  "homepage text"
end

Reloader - an alternative to the classic Sinatra reloading (with weird require related issues), and Shotgun, which causes a fork() each request, which is fairly heavyweight.  Glue’s Reloader is the happy midpoint, between the other two solutions. It reloads everything, but in the same Ruby process, making it much faster.

Settings - a minimalist implementation of the settings.yml file, with environment support. Easily switch up your database settings, your email address, url and more.  Bonus Feature: the default skeleton creates an automatic settings.yml file when you first run monk:start. 

get '/' do
  @foobar = settings(:foobar)
  "the value of the foobar setting is:" + @foobar
end

 

Ohm Persistence Layer

Ohm is a lightweight data management layer that uses Redis as it’s backend. Redis is a fast key-value database, and Ohm makes mappings that are simple to define, and automates the nasty parts of key-value databases like reverse lookups (indexes). Ohm isn’t technically an ORM, since there’s no relational database backing it, but it fills the same niche as ActiveRecord or Datamapper, storing your data for future use.

Awesome Reddit Clone Example

Check out http://news.monkrb.com/, the code is located at http://github.com/monkrb/reddit-clone

Pay attention for a future blog post digging further into this example code, and dissecting it.

Get Monk & Find out More

Install Monk

gem install monk

Links

]]>
http://www.gittr.com/index.php/archive/monk-quickstart-sinatra-projects/feed/ 2
Wrapping get, post, put, delete – Add Magic JSON Power http://www.gittr.com/index.php/archive/wrapping-get-post-put-delete-add-magic-json-power/ http://www.gittr.com/index.php/archive/wrapping-get-post-put-delete-add-magic-json-power/#comments Wed, 05 Aug 2009 03:55:14 +0000 cschneid http://www.gittr.com/?p=89 Several times recently, there have been questions in the #sinatra channel, and on the mailing list about how to automatically render JSON, or any other kind of format as the response from Sinatra.

My favorite way of accomplishing this is to wrap the normal get and post methods with additional JSON abilities.

require 'rubygems'
require 'sinatra'
require 'json'
 
def json_get(route, options={}, &block)
  get(route, options) do 
    block.call.to_json
  end
end
 
json_get '/' do
  {"bar" => "baz", "foo" => "bar"}
end

And of course, this can be expanded to other cases where you’d want to pre-process or post-process certain routes. You could easily define an admin_get that checks that the user is logged in as an admin. Or an xml_get, or a timed_get method.

]]>
http://www.gittr.com/index.php/archive/wrapping-get-post-put-delete-add-magic-json-power/feed/ 2
Rendering different layouts with Sinatra http://www.gittr.com/index.php/archive/rendering-different-layouts-with-sinatra/ http://www.gittr.com/index.php/archive/rendering-different-layouts-with-sinatra/#comments Tue, 14 Jul 2009 15:38:33 +0000 admin http://www.gittr.com/?p=86 ‘admin’ everywhere, so I came up with this (very small) code snippet to make [...]]]> I had a project recently that had a requirement of a frontend, and an admin backend. Pretty normal for an application, but of course the admin had a different layout, set of tabs, etc. I didn’t want to say :layout => ‘admin’ everywhere, so I came up with this (very small) code snippet to make rendering a different layout easy.

def admin_haml(template, options={}) 
  haml(template, options.merge(:layout => :'admin/layout')) 
end

Which I then used in my code like:

get '/admin/users' do
  @users = User.all
  admin_haml :'admin/user/index'
end

This also shows how to have nested directories in the views folder if you were curious. This will look for the view views/admin/users/index.haml, and render it with the layout views/admin/layout.haml.

]]>
http://www.gittr.com/index.php/archive/rendering-different-layouts-with-sinatra/feed/ 1
Installing Sinatra 0.10.1 http://www.gittr.com/index.php/archive/installing-sinatra-0-10-1/ http://www.gittr.com/index.php/archive/installing-sinatra-0-10-1/#comments Tue, 14 Jul 2009 15:07:42 +0000 admin http://www.gittr.com/?p=81 The current version of Sinatra (as of the time I write this) is version 0.10.1. The gem on rubyforge is outdated (0.9.2). To install the current gem, use the github built version:

sudo gem install sinatra-sinatra --source=http://gems.github.com
]]>
http://www.gittr.com/index.php/archive/installing-sinatra-0-10-1/feed/ 2
ActiveRecord timezones without Rails http://www.gittr.com/index.php/archive/activerecord-timezones-without-rails/ http://www.gittr.com/index.php/archive/activerecord-timezones-without-rails/#comments Tue, 23 Jun 2009 14:23:00 +0000 cschneid http://www.gittr.com/?p=77 I was using Active Record in a batch application to manipulate some data, and I needed the timezone handling built into the newer versions. But I had a hell of a time trying to figure out how to make it all work.

In plain Rails, it’s simple:

Rails::Initializer.run do |config|
  config.time_zone = 'Pacific Time (US & Canada)'
end

But I wasn’t running Rails proper, and didn’t want to pull in the whole Rails boot sequence. So the Rails::Initializer call didn’t work. To do it manually, time zone config turns out it’s a 3 lines of setup calls:

require 'activerecord'
Time.zone = "Pacific Time (US & Canada)"
ActiveRecord::Base.time_zone_aware_attributes = true
ActiveRecord::Base.default_timezone = "Pacific Time (US & Canada)"

When you do that, time zone attributes work both going into the database, and coming back out. If you miss the last line, they’ll just not work coming out, which is a damn confusing thing to deal with.

Ohh, and one other thing I ran into when getting this setup is that AR pulls in Active Support, which in turn pulls in Builder (to hack xml support or something). Just watch out for that, you’ll get weird crashes if you don’t have builder installed. Honestly, I just went and commented out the areas of Active Support that did it, I didn’t want builder anyway.

]]>
http://www.gittr.com/index.php/archive/activerecord-timezones-without-rails/feed/ 1
Fix for: “no such file to load — blankslate” http://www.gittr.com/index.php/archive/fix-for-no-such-file-to-load-blankslate/ http://www.gittr.com/index.php/archive/fix-for-no-such-file-to-load-blankslate/#comments Tue, 12 May 2009 17:43:29 +0000 admin http://www.gittr.com/?p=75 I was getting this error, and it turns out it’s just that builder isn’t installed. sudo gem install builder, and you’re fixed.

/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- blankslate (MissingSourceFile)
	from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
	from ./vendor/rails/activerecord/lib/../../activesupport/lib/active_support/basic_object.rb:21
	from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
	from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
	from ./vendor/rails/activerecord/lib/../../activesupport/lib/active_support/duration.rb:1
	from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
	from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
	from ./vendor/rails/activerecord/lib/../../activesupport/lib/active_support/core_ext/time/calculations.rb:1
	 ... 18 levels...
	from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
]]>
http://www.gittr.com/index.php/archive/fix-for-no-such-file-to-load-blankslate/feed/ 2