Archive for the ‘Tools, Frameworks & Best Practices’ Category.

Fix for CreateQualificationType returning 400 error using rturk

Update 7/14/11 – I believe this is fixed in rturk 2.4 – thanks Mark!
http://rubygems.org/gems/rturk
***
rturk, the Ruby gem for making calls to the Amazon Mechanical Turk API, uses a REST transport layer. That’s fine, but all calls are currently performed by a GET, which has a length limitation. When making calls that include long strings of data – such as the XML for a QuestionForm structure in a qualification tests – errors may occur with the non-explanatory message ‘400 Request Error’.

Was able to patch it by making a change to lib/rturk/requester.rb :

46,47c46,50
< RTurk.logger.debug "Sending request:\n\t #{credentials.host}?#{querystring}"
< RestClient.get("#{credentials.host}?#{querystring}")
---
> # RTurk.logger.debug “Sending request:\n\t #{credentials.host}?#{querystring}”
> # RestClient.get(”#{credentials.host}?#{querystring}”)
>
> RTurk.logger.debug “Posting request to #{credentials.host}:\n\t #{params.inspect}”
> RestClient.post(credentials.host.to_s, post_params)

A more robust fix might be to use POST only for longer requests, or make it an explicit option on the RTurk object

Workaround for corruption in saving bytea data thru rails

I’m not sure where the bug is, but when saving some binary data that was generated in a Rails3 before_create callback, it kept getting truncated in the actual INSERT INTO statement (though it appeared fine even in after_save callback, it was truncated in the database). Using PostgreSQL 8.4.7 with pg (0.9.0) and activerecord 3.0.5

Seemed like it could be related to this fixed bug, but my problem is on the save itself:
https://rails.lighthouseapp.com/projects/8994/tickets/611-cannot-write-certain-binary-data-to-postgresql-bytea-columns-in-2-1-0

In any case, found a simple workaround: uuencode the data first, and uudecode on loading.

   before_create do
      ... stuff that builds my_hash ...
      self.my_hash = Base64.encode64(Marshal.dump(my_hash))
   end

Then later, to reconstitute the hash,

   loaded_hash = Marshal.load(Base64.decode64(@record.my_hash))

Rails docs!

Found a real effort to doc the rails framework! Not all filled in, but a huge improvement over the tutorial-as-doc approach:

http://apidock.com/rails

ie link_to

Thanks APIDock!

How to override ‘Create New’ for activescaffold associations

In the helper file, ie tablenames_helper.rb, override the render_action_link method

 def render_action_link(link, url_options, record = nil, html_options = {})
    if link.parameters[:association] == :my_association
       # special handling here
    else
       super  # in case you have other associations for which you do want the default behavior
    end
 end

This confounded me for a while as it was easy to create a helper to override the link to associated records, but if the records did not yet exist the ‘Create New’ appeared hardcoded. After inspection of ./bundler/gems/active_scaffold-45451d963672/lib/active_scaffold/helpers/list_column_helpers.rb and ./bundler/gems/active_scaffold-45451d963672/lib/active_scaffold/helpers/view_helpers.rb the above override became apparent. Is there an easier way to determine this sort of thing?

case of the missing layout

After a bit of refactoring of one of my controllers in a Rails3 app, the layout mysteriously disappeared, with

Rendered vendor/plugins/active_scaffold/frontends/default/views/list.html.erb (188.5ms)

replacing

Rendered vendor/plugins/active_scaffold/frontends/default/views/list.html.erb within layouts/application (192.1ms)

in the Rails debug output. Turned out to be a simple error – I added a routine to initialize some instance variables and forgot the ’super’ at the end to call the parent class initializer:

def initialize
   ... my instance vars ...
   super
end

Ruby beats Perl for UTF-8 handling

Just had to write a script to do some processing on a tab-delimited file that contained some Hebrew UTF-8 chars, including a json chunk containing some Hebrew. Major headache with Perl – either got ‘wide char in subroutine’ errors, or escaped codes in the JSON. Needed to compare the UTF-8 tab-delimited fields with specific pieces of JSON, and have them both handled as UTF-8. Tried use utf8; binmode(STDIN, ':utf8') and various other permutations, but couldn’t get the output to look the way I wanted while keeping all the subroutines happy.

Implemented the same thing in Ruby, and it just worked. I’ve been a Perl girl for years, but am leaning Ruby…

Varnish -drop in website cache / accelerator

Check it out – Jeff Su, a developer on our team at Factual.com has had really good results using Varnish, written up here:

A Practical Guide to Varnish

For sites that need to ramp up suddenly, this could be a lifesaver…

setting JAVA_HOME

The instructions at http://www.cyberciti.biz/faq/linux-unix-set-java_home-path-variable/ are slightly out of date or incorrect. Instead of setting

export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.22/bin/java # do NOT do this

as you might expect from their instructions, you should

export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.22/

rpm complaint about NSS/NSPR headers really looking for sechash.h…

Needed to build a recent version of RPM from source. When running ./configure for rpm-4.8.1, I ran into the ‘configure: error: missing required NSPR / NSS header’ error referenced at

http://lists.rpm.org/pipermail/rpm-list/2009-May/000253.html

The solution there is good but incomplete, a peek into the configure file reveals that they are really checking for ’sechash.h’ which is in the nss-devel package. After doing a

yum install nss-devel
as well as
yum install nspr-devel

then I can successfully run

./configure CPPFLAGS=”-I/usr/include/nspr4 -I/usr/include/nss3″

(well, almost successfully…now I am on to the next error message about a Berkeley Db directory not present…if that seems tricky as well will post a solution here, if straightforward I won’t bother…)

Relations API enhancement

Just posted a patch: Expanded predicates for Relations API that gets one step closer to being able to combine free tagging with RDF. Basically the idea is simple: use the existing free tagging capabilities of Drupal to let users build up a smart domain-specific vocabulary, and use those words as predicates in RDF statements. I’m kind of surprised there didn’t seem to already be an easy way to do this.

The patch above does the step of including the vocabulary terms as predicates for Relations API. I still need to do another patch, probably to taxonomy_xml, to automatically expose them as RDF, and then expose something that makes it easy to output them in nice clean RDFa with the nodes. Once that is done, it seems pretty powerful to me. I like being able to write something and in a structured way say it was inspired by a book, that it implements a philosophy, or that it is useful for some specific goal.

This is partly done at bteaching.com now; the relations are visible to the user along with each node. They aren’t output in RDFa yet, though, that still needs to be done along with the automatic vocabulary exposure as RDF.

Anyway, I wanted to post what I have so far, as its in production on a live site and might be useful for someone else in its current form.