Archive for the 'Ruby' Category

Programatically adding tags to contacts in Highrise

I have been playing with adding tags to contacts in Highrise programatically. Unfortunately, 37Signals has not added this functionality to the API for Highrise so you have to hack it in. Based on a post on adding contacts, the following code will add a single tag to a contact.

require 'rubygems'
require 'net/http'

contact_id = 12345
connection = Net::HTTP.new("your_account.highrisehq.com")
req = Net::HTTP::Post.new("/parties/#{contact_id}/tags")
req["content-type"] = "application/xml"
req.basic_auth("your_api_token", 'X')
req.set_form_data({"name" => "new_or_existing_tag"}, ";")
response = connection.request(req)
puts response

This will return a bunch of JavaScript that you can ignore.

For comparison, here is the same code using HTTParty. A very nice, easy to use library for interacting with ReST services.

require 'rubygems'
require 'httparty'

class Tag
  include HTTParty
  base_uri "your_site.highrisehq.com"
  basic_auth 'your_api_token', 'X'

  def add_tag_to_contact(tag, contact_id)
    self.class.post("/parties/#{contact_id}/tags", :query => { :name => tag })
  end
end

puts Tag.new.add_tag_to_contact("tag_as_string", "1")

The goal of all this is to create a Highrise API wrapper that includes the ability to manipulate tags. Highrise is great, but the lack of Tags in the API really reduces what you can do with the tool since there is no facility for creating or manipulating lists.

Nokogiri works on Solaris 10

As a quick follow up to my previous post, Nokogiri works fine on Solaris 10 as long as you link to the most current version of Libxml2 (2.6.31). Our problem was that, although I had installed the newest binaries, Nokogiri was linking to a previous version (2.6.10). Once we updated the link, by changing the LD_LOAD_PATH environment variable, everything worked great.

Weird behaviour from Nokogiri on Solaris

At work I have been playing around with Webrat to see if it can speed up our acceptance tests. We have been using Selenium, but the tests are taking far too long to run (somewhere around 40 minutes or so, rough guess). And the majority of the tests are not testing JavaScript or AJAX.

In the tests that I did, Webrat worked amazing with a huge speed increase. One test went from 134 seconds down to 2.5 seconds. The glitch is that Webrat is designed to be used with a Ruby web app–Rails, Merb, Sinatra, etc–and we want it to test a Java app running in Tomcat. Thankfully, this is fairly easy to do. You just need to set the mode to Mechanize and add a few lines to include Webrat::Methods and Webrat::Matchers into RSpec.

The problem we ran into was when we tried to run the tests on our Solaris box. Nokogiri is only returning the doctype and comment parts of the fetched HTML. Running the following in IRB only returns the doctype:

>> require 'mechanize'
>> require nokogiri'
>> a = WWW::Mechanize.new
>> r = a.get("http://www.google.com")
>> s = Nokogiri::HTML(r.content.to_s)
>> puts s
=> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

On Ubuntu and my MacBook Pro, this prints out the entire HTML, under Solaris, it only prints out the Doctype. I have raised a ticket with Nokogiri, but am wondering if anyone else has experienced this? The only difference between Solaris and our Ubuntu dev boxes is the version of Ruby, 1.8.6 p0 and 1.8.6 p111 respectively.

Getting autotest to work with Ruby projects

If your a fan of creating Rails applications using ZenTest’s autotest and want to use it on a Ruby project, then you will need to follow this convention:

  • All test files need to be in a directory called test
  • All test files need to start with the word test
  • All your test classes must start with Test
  • and finally, your code should be in a directory called lib

Just a little something I learned trying to get autotest to work with my Ruby project.

Rubular – quick online Ruby RegEx tester

Rubular is a website that I have incredibly useful over the past few weeks. It is an online Ruby app that tests Ruby regular expressions. You enter your regex online with a string that you want to match it against and the app will show you what matches. In addition, if you have any groups, it also shows the what is in the groups.

rubular

Problems with CC.rb, OS X 10.5, and Subversion

I have been attempting to get CruiseControl.rb 1.3 running on my new Mac with little success and am wondering if other people have been having the same problems. I have two Macs that I am trying this on and neither is working. Both show the same error.

I am able to start CC.rb using ./cruise start and am able to add a project. The problem is when CC.rb goes to do the first build. I keep getting the following error:

BuilderError: svn: PROPFIND request failed on '/svn/trunk/project_name'

If I expand that, the actual problem is with the following:

svn --non-interactive log --limit 1 --revision HEAD:1 --verbose --xml

The important part is the --non-interactive switch. If I remove that and run the command directly from the directory where the project is checked out to, it works fine. I include --non-interactive and it fails.

Both machines use default versions of svn (i.e., whatever came with the computer).

My theory is that the problem lies with OS X’s Keychain program, which is where the credentials are stored for the svn repository. In interactive mode, when svn asks for the username and password, keychain steps in and supplies them. However, in non-interactive mode, these credentials are never supplied because they are not cached properly by Subversion.

My problem is that I am not sure how to test or resolve this. I am somewhat reluctant to install Subversion over top of the currently installed version because I am not sure what I may screw up. However, I am going to continue to investigate this and see if I can’t figure it out.

Has anyone else had this problem and, if so, how did you fix it?

Script for opening Ruby gems in Textmate

The Effectif Development website has posted a very neat trick for opening Ruby gems in Textmate. This is very handing and the cool part is that it includes tab completion for the gems.

Next Page »