ClickAider
You are currently browsing the Development category.
RSS feed

The Rails Way: new blog from Jamis Buck and Michael Koziarski

The Rails Way is a new blog from Rails gurus Jamis Buck and Michael Koziarski. They accept submissions of real world web applications and suggest ways to improve the code through the right MVC factorings. 

Interesting reading. (Also check out some of Jamis’s past posts, such as Skinny Controller, Fat Model.)

With all the attention Rails has received in the last year, there has been an enormous influx of people wanting to learn how to write web applications with it. Rails itself attempts to shepherd programmers along well-trod paths, to help them build well-designed applications “by default”. Alas, Rails’ conventions are not always enough.

This blog is our attempt to back up Rails’ opinions on what things should be done, with our own opinions on how things should be done.

Here’s how we’ll do it: if you have an application or a piece of code which you think could be done better, but you aren’t quite sure how, send it in. We’ll review all submissions, then publish an article or two showing the changes we’d make, and explain why we’d make them.

Readers get the chance to learn tips and tricks for enhancing your design, submitters get their application refactored for free, and we get some free-inspiration for our blog posts. Nobody loses!

Ruby Module for Searching using the Alexa Web Services API

This post includes an improved version of the query example in Ruby for hitting the Alexa Web Search web service. Sample usage is as follows.

require 'alexa'
Alexa.search('bogle').each {|hit| puts hit.title}

For convenience, you might want to edit the code below to replace INSERT_YOUR_ACCESS_KEY_HERE with your actual access key and INSERT_YOUR_SECRET_ACCESS_KEY with your secret access key; otherwise these will need to be passed in as the AWSAccessKeyId and SecretAccessKey options.

Read Full Post

Ruby Wrapper for MSN Live Search API

After a couple painful hours of trial and error, I’d like to share some code that simplifies the process of accessing the MSN Live Search API from Ruby

The code assumes that you have auto-generated the SOAP driver for the MSN Search web services in a directory called msn_search_driver, using the following command.

wsdl2ruby –wsdl http://soap.search.msn.com/webservices.asmx?wsdl –type client

msn_search.rb takes care of the nasty details for you, so that you can write simple code like this:

require 'msn_search'
MsnSearch.new.search("bogle").results.each {|r| puts r.url}

<RANT>
Although it’s possible in to call the SOAP driver directly, the API is hard to use because of complex, nested request and response types, poorly documented mandatory arguments, and unhelpful error messages on invalid requests. (These shortcomings are not unique to the MSN SOAP interface– I’ve experience similar shortcomings in many other SOAP apis. REST-based APIs tend to win hands down in terms of simplicity and debuggability.  

Note that neither Google, Yahoo, nor MSN will allow to fetch beyond the first 1000 results in a search, independent of their daily hit limits. If you need to use a web index for an application that’s more than a toy, you’re probably much better off using something like Alexa Web Search Platform than any of the major search APIs. The downside to the Alexa platform is that C and Java are the only supported languages.   The Alexa platform supports C, Java, and Ruby.  Alexa provides Ruby-based tutorial on how to build an image search application using their index.
</RANT>

Now that I’ve got that rant off my chest, here’s the code.


msn_search.rb
($:).push('msn_search_driver')

require ‘defaultDriver.rb’

class MsnSearch
	attr_accessor :driver

	def initialize(appID = ‘INSERT_YOUR_APP_ID_HERE’)
		@driver = MSNSearchPortType.new
		@appID = appID
	end

	def search(query, offset=0, count=50, source=”Web”,
safeSearch=”Off”, culture=”en-US”)
		s = Search.new
		s.request = with_new(SearchRequest) do |r|
			r.cultureInfo = culture
			r.safeSearch = safeSearch
			r.query = query
			r.appID = @appID
			r.requests = source_requests =
ArrayOfSourceRequestRequests.new
			source_requests << with_new(SourceRequest) do |sr|
				sr.offset = offset
				sr.count = count
				sr.source = source
				sr.resultFields = “All”
			end
		end
		@driver.search(s).response.responses.sourceResponse
	end

	def set_debug_mode(dev = STDOUT)
		@driver.wiredump_dev = dev
	end

protected
	def with_new(klass)
		instance = klass.new
		yield instance
		return instance
	end

end


3d Triangles in Javascript

Here’s a nifty trick for real-time 3D in Javascript, via Grant Rodgers.  The solution uses only Javascript, DOM, and CSS.

Named references in Rails test fixtures

Court3nay has created support for named references in test fixtures in Rails:

I’ve written some fun code to ease the pain of associating between
fixtures.  ’Tis tested and documented!  Essentially it allows you to
replace this syntax

  user_id: 5

with this:

  user: :joe

as long as you have users.yml with

joe:

This is a big time save for anyone creating Rails tests that use associations.