Alexa Web Crawler - TamperedWithCookie Error

Posted by patjoyce
Dec 02, 2008

I’ve recently started receiving a ton of CGI::Session::CookieStore::TamperedWithCookie errors on an application I manage. All of the errors are coming from the Alexa Web Crawler.

The application is using the Cookie Session Store (Railscast) introduced in Rails 2. Upon inspection it appears that the problem is related to the newline characters rails inserts every 60 characters. Rails encodes these newlines as %0A. The alexa web crawler seems to convert these to \n. This is the only difference between the two cookies that I can see.

Rails Cookie:


_application_session=BAh7CToMY3NyZl9pZCIlZDA1ODAzY2MwZGZjNzJkN2I4NGFjZTE5OTcxNzZh%0ANjI6FnNlYXJjaF9jYXRlZ29yaWVzWxRvOg1DYXRlZ29yeQc6FkBhdHRyaWJ1%0AdGVzX2NhY2hlewA6EEBhdHRyaWJ1dGVzewciCW5hbWUiFUF1dG8gLyBUcmFu%0Ac3BvcnQiB2lkIgYxbzsHBzsIewA7CXsHIgluYW1lIhNCZWF1dHkgLyBJbWFn%0AZSIHaWQiBjVvOwcHOwh7ADsJewciCW5hbWUiEkJvYXQgLyBNYXJpbmUiB2lk%0AIggxMTVvOwcHOwh7ADsJewciCW5hbWUiFUJ1c2luZXNzIC8gQWRtaW4iB2lk%0AIgY5bzsHBzsIewA7CXsHIgluYW1lIhRDb21wdXRlciAvIFRlY2giB2lkIgcx%0AMW87Bwc7CHsAOwl7ByIJbmFtZSIVQ3JlYXRpdmUgLyBNZWRpYSIHaWQiBzEy%0AbzsHBzs;

Alexa Crawler Request Cookie:


_application_session=BAh7CToMY3NyZl9pZCIlOGJlNGQ2ZGYzYWZjODRhZGI4YmNlMWUxZTkwNmNl\nYjA6FnNlYXJjaF9jYXRlZ29yaWVzWxRvOg1DYXRlZ29yeQc6FkBhdHRyaWJ1\ndGVzX2NhY2hlewA6EEBhdHRyaWJ1dGVzewciCW5hbWUiFUF1dG8gLyBUcmFu\nc3BvcnQiB2lkIgYxbzsHBzsIewA7CXsHIgluYW1lIhNCZWF1dHkgLyBJbWFn\nZSIHaWQiBjVvOwcHOwh7ADsJewciCW5hbWUiEkJvYXQgLyBNYXJpbmUiB2lk\nIggxMTVvOwcHOwh7ADsJewciCW5hbWUiFUJ1c2luZXNzIC8gQWRtaW4iB2lk\nIgY5bzsHBzsIewA7CXsHIgluYW1lIhRDb21wdXRlciAvIFRlY2giB2lkIgcx\nMW87Bwc7CHsAOwl7ByIJbmFtZSIVQ3JlYXRpdmUgLyBNZWRpYSIHaWQiBzEy\nbzsHBzs

I’m not the only person having this problem. Any ideas?


Error - rake ultrasphinx:configure

Posted by patjoyce
Nov 07, 2008

A new Web Designer I work with was running into an error when attempting to configure ultrasphinx for his local Rails development environment.


rake ultrasphinx:configure

rake aborted!
Anonymous modules have no name to be referenced by
/var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:402:in `to_constant_name'
/var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:214:in `qualified_name_for'
/var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:477:in `const_missing'
/home/deploy/apps/stylespotted/releases/20080131024018/vendor/plugins/ultrasphinx/lib/ultrasphinx/configure.rb:33:in `load_constants'

I was baffled for this for a few days. We finally figured it out this afternoon.

He is a UI developer, and new to Rails, so hadn’t created his DB. The fix was simple:


rake db:create
rake db:migrate
rake ultrasphinx:configure

And he had a working development.conf for Ultrasphinx


alias versus alias_method

Posted by patjoyce
Jan 22, 2008

Last week Ian and I were running into a problem with alias_method. It turned out that we were passing the method name to alias method as you would to alias, instead of passing a string or a symbol of the method name. Here is the correct usage of alias and alias method

  
  alias new_method_name old_method_name
  alias_method :new_method_name, :old_method_name
  alias_method 'new_method_name', 'old_method_name'
  

Here is the blog post that pointed out our error. It is in Spanish, so for the first time ever all that time I spent learning Spanish helped me as a programmer ;).

For the benefit of English speakers I decided to translate the blog post. Here is the translated text:

alias and alias_method do the same thing: copy a method and assign it a different name.

  
  class Test
    def test
      puts "hello" 
    end
    alias test_copy test
    alias_method :test_copy2, :test
  end

  Test.new.test        # > "hello" 
  Test.new.test_copy   # > "hello" 
  Test.new.test_copy2  # > "hello" 
  

That piqued my curiosity to know the difference between alias and alias_method. It turns out that they are the same, except that:

  1. alias is a reserved keyword in Ruby
  2. alias takes the method identifiers as parameters, without the need to use symbols or strings (for example in “def method_name”, method_name is an identifier and not a string or a symbol) This behavior can be pretty confusing when you’re starting out.
  3. alias_method is a method of the class Module
  4. alias_method takes its parameters separated by comma, just like any other method.

The consequences are simple:

  • alias_method can be redefined, and alias can’t.
  • alias can be used incorrectly (outside the context of method definitions)

By being a method of Module, alias_method makes it easier to use it correctly: in the context of method definition of a class or module.

Conclusion: In the majority of cases, alias_method is what you need.


Speaking at the November NovaRUG Meeting

Posted by patjoyce
Oct 17, 2007

I will be speaking about the Liquid templating language at the November meeting of the Northern Virginia Ruby Users Group.

I’ll put up an abstract for the talk at some point in the future and will post details on the date when I have them.


Wow.

Posted by patjoyce
Sep 28, 2007

Me and Ian attended RubyEast today. It was a good conference, obviously much smaller than RailsConf, but a nice break from working on the startup and a good chance to meet other rubyists. I’ll write up a longer recap later in the weekend but for now I just want to post one quote I found interesting.

Before his presentation Obie Fernandez was asked about the financial incentives for writing a book. He’s a good person to ask as The Rails Way is coming out soon and is pretty much preordained to be a hit by technical publishing standards. His answer:

“I will probably make a quarter of a million dollars a year for the next several years because I wrote the book. Not from the book, but because of it”

What a remarkably candid response.


Confusing Rails Error Message

Posted by patjoyce
Sep 18, 2007

ArgumentError: Admin is not missing constant UsersController!
    method load_missing_constant in dependencies.rb at line 240
    method const_missing in dependencies.rb at line 452
    method send in dependencies.rb at line 470
    method const_missing in dependencies.rb at line 470
    method setup_without_fixtures in users_controller_test.rb at line 11
    method setup in fixtures.rb at line 576

Note to self: the above error message normally means that you are missing the scope prefix and the :: operator. So in the above example the error would be fixed by prepending “Admin::” to the reference to the class “UsersController” on line 11 of users_controller_test.rb.


Rails Environments Gotcha

Posted by patjoyce
Jul 23, 2007

I’ve been working on an application that sends emails. (You’ll hear more about the application very soon) The emails often contains links to the application. In development and test I want these links to point to localhost:3000, and in production I want them to point to mydomainname.com. Therefore, the ActionMailer class needs to know the root of the application.

So, in environment.rb I added the following line:

APPLICATION_ROOT = "http://localhost:3000"

And in production.rb I added the following line:

APPLICATION_ROOT = "http://mydomainname.com"

Unfortunately, when I deployed to production the emails were being sent with http://localhost:3000 as the base for links.

I checked my mongrel config and everything seemed to be set up right for production. I checked the logs and saw that the application was running as production.

Then I ran script/console production, saw the following error message, and immediately got an idea what I did wrong.

production.rb:19: warning: already initialized constant APPLICATION_ROOT

APPLICATION_ROOT was still returning ‘http://localhost:3000’

The problem was I was counting on settings in production.rb to override those in environment.rb. And they do except I defined my setting as a CONSTANT.

I moved the constant declaration out of the environment.rb and into both development.rb and test.rb and everything now works fine.

The thing I still don’t understand is that according to page 330 of the PickAxe Ruby is supposed to allow you to alter the value of a constant.

Anyone know why the constant’s value didn’t change in my config files?


July NOVA RUG Recap

Posted by patjoyce
Jul 18, 2007

I went to the NOVA RUG meeting tonight. It was my first NOVA RUG meeting and I’m glad that I went.

There were three presentations, one on Ruby Mocking Libraries, one on HAML and SASS, and one on working with legacy databases in Rails. My thoughts follow.

Mockfight

Patrick Reagan, Director of Application Development, Viget Labs slides

I’ve heard a lot about mocking, but as someone who is still pretty new to test driven design I haven’t used it before. This presentation really showed me the benefit by walking through the example of testing a method that tests if a given URL exists. All examples were given in both FlexMock and Mocha.

Patrick has used both FlexMock and Mocha. He is currently using Mocha. He said that they are roughly feature equivalent so it comes down to a matter of taste regarding syntax style. From the examples I thought that the Mocha syntax seemed more natural.

HAML and SASS

Devin Mullins

HAML (HAML Aint a Markup Language) is an alternative to using erb for view templates. HAML is less verbose than rhtml and uses indentation to denote nesting and end tags. It generates well indented, valid xhtml. It is an interesting idea, but I haven’t ever felt real pain with writing xhtml and erb, so I doubt I will use it.

The fact that indentation is significant is reminiscent of Python. A lot of people were complaining about that similarity because they don’t like it in Python. I have wrote a little bit of Python a few years ago, but not enough to have a real opinion. That said, I think that you should always have consistently indented code or markup so I don’t see how a language that enforces that is a problem.

SASS (EDIT: SASS Aint Style Sheets Syntactically Awesome StyleSheets) is an alternative to css. It is really cool. It has a simple syntax and allows you to define constants, include other files, and eliminate duplication by nesting rules. I like CSS, but the inability to declare constants for things like border colors has always driven me crazy. SASS looks like it was designed specifically to adress all my pain points. I think that designers would love this.

The talk centered on examples, namely on Devin converting the scaffold generated view from rhtml to HAML and on showing the use of SASS.

I don’t see myself using HAML. I’m definitely going to investigate SASS.

Working with Legacy Databases

Matt Scilipoti, Possiamo Consulting, LLC

The talk centered on his experience in working with legacy databases. He is working with a large SQL Server DB that is used for protein sequencing data.

I am tired of typing and I don’t plan on having to work with legacy databases from Rails anytime soon so this is going to be short.

He started out specifying all the non standard schema stuff in his active record models. That grew painful as he had to specify the keys and table names with each relationship.

Next he moved to using Views to create an interface to the legacy DB that conformed to rails naming conventions. This was an improvement, but still sort of a pain.

Now he uses a class method whose name escapes me at the moment to handle the conversion. If anyone has slides or knows what it was that he did please leave a comment and I’ll update this section.

He also has started a Ruby Forge project Working with Legacy Databases in Rails. He will be releasing his class method as a plugin shortly.

Check it out at http://rubyforge.org/projects/weldir/

It was a good, and at times funny, talk. I learned quite a bit and am glad I heard it.


The Rails Way: Jamis Buck and Michael Kosiarski

Posted by patjoyce
May 20, 2007

The Rails Way

Goal is to describe the code style of rails through some examples.

Skinny Controller Fat Modeler

Common anti pattern is a fat controller. Difficult to test. Common newbie mistake is to equate model with DB table. A model can be anything that encapsulates your data. It doesn’t have to have anything to do with the DB. One benefit of splitting things out are that you can now easily test independently from your controller. Also you can now use the form builder helpers that rails provides.

with_scope antipattern

before_create hook naming

Don’t depend on naming hook methods before_create etc. that is a readability issue. give the method a meaningful name and then use the before_create class method to hook it up.

Association proxies

Code at the level of thought, not DB. example: @john.documents instead of Document.find_all_by_user_id(@john.id)

Apart from the readability benefit there are performance gain because subsequent calls in the same request won’t have to hit the DB.

Routes file

Use map.with_options to reduce documentation.


Ruby Tooling: State of the Art

Posted by patjoyce
May 19, 2007

Brian Leanord

How’s Neteans Like Rails
  • Free
  • Open Source
  • Plug-in Architecture
  • Large community

Download latest Ruby-only IDE from ther CI server

Or you can download the IDE 6.0 preview (M9)

Daily improvements. They do a milestone every 5 weeks.

Not actually Ruby, it runs with JRuby But configurable to use Native Ruby

Code complete, integrated RDoc. You can jump from controller to view and vice versa. You can also jump directly to the tests via keyboard shortcut.

Can run servers (mongrel or webbrick)

Can run arbitrary generators and get their descriptions in one place from the IDE.

The integrated RDoc and code completion is really cool. I love textmate, but I often wish I didn’t always have to remember the exact name for every single method call. DHH even botched a method name during his keynote yesterday. I don’t know if it would be a good idea to learn rails or ruby using this but I definitely think that there is a huge amount of potential for tooling moving forward. What I’d be really interested in is how they handle figuring out scoping and such with such a dynamic language but unfortunately this seems like more of a commercial than a technical discussion.

It has Local history! This is a feature that I really loved in Eclipse and miss in all the other editors I use.

I like their visual diff.

They have “templates” that are similar to TextMate snippets. They have a bunch, and are planning on cloning all the TextMate snippets.

They have built in regex documentation and builder that is ctrl-space sensitive. They also have one for date format strings. Nice!

Debugging

They have a graphical debugger that is built of of the Fast Ruby Degugger gem. Only works with native Ruby for now, not JRuby. Similar to Visual Studio or Eclipse interactive debugger. Will loook really familiar to any of their users. I think that the irb based debugger that DHH demoed yesterday may be more powerful, but not having to context switch may be valuable. I think that this debugger might be really useful for learning the framework or an app.

Can set breakpoints in view code. Pretty cool.

Can you kick into the debugger from tests?

He thinks so.

Can you run the tests?

Tests are run from the rake task in a console window. Not yet a harness that shows everything green and red or continuously runs them in the background or anything.

They’ve really doe a huge amount of work, but part of what this shows is how little

To-do

  • Rails Console
  • Automated Refactoring
  • Better code completion for rails framework.

DTrace on Rails BoF

Posted by patjoyce
May 18, 2007

Just got back from the a birds of a feather session about “DTrace” and the work that Joyent has done to instrument Ruby and Rails. I’d read about DTrace, but listening to the guy who used it to knock 80% off of Twitter’s CPU usage is pretty cool. Afterwards I got a chance to talk (and by talk I mean stand next to and listen because I don’t have anything to add) one of the guys from Joyent, some of the developers from Kongregate talk about XMPP. The guy from Joyent talked about watching the TCP window shrink via DTrace while a Ruby XMPP thread was blocked. I’m botching the retelling of the story now, but it was incredibly impressive that you could watch the internals of the TCP protocol at work.

I just can’t describe how unbelievably cool DTrace is. You can time all your rails function calls, then all the system calls they kick out to the db, then all the internal calls in the db and all the way back up. With this level of instrumentation you can pinpoint exactly where your bottleneck is.

DTrace is going to be in Leapord along with a really sweet GUI called Xray. I just hope I have an app someday where I’ll have enough load that I need it.