Do the Lighthouse mambo!
And be happy you are alive.

Did you know that:

A pig's orgasm lasts for 30 minutes?

Search the Feed

Codeboxer search results for Lighthouse
Alexa CSS Dreamhost Javascript Joyent Lighthouse Migration MySQL Oracle PHP Pivot SQL ssh status codes svn Unit Test

Lighthouse API integration in 10 minutes or less

15 November 2008 | 2:45 am by codeboxer

As a second part in this series, I am going to talk about Lighthouse for a second. They rock. I built up a calendar app based on their API and the Ruby calendar_helper, and it barely took me a day. True story. I ended up using an API token, and I learned a lot about a lib/ based connection object, very interesting.

Here are a few caveats:
Don't mess with the root class too much. It was not really necessary for most of what I had to do. This is actually one of the cases where it makes sense to load the logic into the controller (a little bit). Plus there isn't much more meat on the bone, so to speak, the root class covers 100% of a ludicrously simple back-end.
All Lighthouse database objects are not created equal. Tickets are king, and everything else is nothing. I will explain more later - just don't try to get picky with your call for Milestones. All I have ever been able to do is return a simple dump.

Installation: all I had to do was click through to the GIT repository. From there click lib/, and then lighthouse.rb. Cut and paste the code you see into a lighthouse.rb file in your lib/ folder. Everything you need is right there.

code snippet from my controller action:

if @project
#calculate i differential
month_now = Time.now.month
year_now = Time.now.year
i = ((@year - year_now)*12)+ (@month - month_now).abs
if i == 1
@tickets = @project.tickets(:q => "created:'before #{Time.now.day} day#{Time.now.day>1 ? 's' : ''} ago'")
elsif i > 1
@tickets = @project.tickets(:q => "created:'before #{i-1} months ago'")
else
@tickets = @project.tickets(:q => "created:'since 1 month ago'")
end
@milestones = @project.milestones(:q => "due_on:'#{@month}/#{@year}'")
else
false
end

Thankfully, you can get picky with your call for Tickets. That is where the meat of the API is. The text based date searching is pretty cool, because you get a hard limit of 30 rows returned from your query, so you need to be granular enough to catch only what you want.

This is a remote controller action that I use to get info about a particular ticket:

def ticket_body
ticket_id = params[:id]
@project = Lighthouse::Project.find(:first)
ticket = Lighthouse::Ticket.find ticket_id, :params => {:project_id => @project.id}
out_string = ""
#counter = 0
ticket.versions.each do |version|
this_body = version.body_html
out_string << "on #{(version.created_at - (60 * 60 * 8)).strftime("%m/%d at %I:%M%p")} #{get_owner_name(version.user_id)} said: #{this_body ? this_body : "status change"}"
end
render :text => out_string
end

Notice the 8 hour time differential from UTC code, and that you have to iterate through the ticket.versions to pull out the notes (thanks to Lighthouse help staff for that one).

And then, of course, I have this at the top of my controller:

def initialize
Lighthouse.account = 'xxx'
Lighthouse.token = '1111919192384584XXXXXXXXXXX'
@project = Lighthouse::Project.find(:first)
end

like I said before, somehow the @project.milestones method won't take a :q argument, so you're stuck with just a simple 'select *' query for your @milestones recordset. Maybe they will change this soon.Original post blogged on codeboxer.com.

My Site Links

Screenshots are featured above. If you visit gmgpulse, you may login as demo/demo.

Rockstar Television


© 2008-10 Krister Axel and codeboxer.com