views:

141

answers:

1

Hi Everyone,

I was wondering if someone could do me massive favour..

I really don't understand how to make use of APIs - so I was wondering if, using Basecamp as an example, someone could talk me though the basics.

So far I have an application with a dashboard controller/view, I have put basecamp.rb into my /lib directory, added the following to my application_controller:

  def basecamp_connect
    Basecamp.establish_connection!('XXXXXX.basecamphq.com', 'USER', 'PASS', false)
    @basecamp = Basecamp.new
  end

Obviously changing the required parts to my credentials.

Next up I have added the following to my dashboard_controller:

  def index
    Basecamp::TodoList.find(:all)
  end

Next I presume I have to somehow list the Todos on the dashboard using some sort of loop.

Am I doing the right thing, if so - how on earth do I display all the todo items and if not - what am I doing wrong/missing.

It doesn't have to be todos, anything from Basecamp or any other popular API service would be a good start. It's just that I happen to have a basecamp account!

Thanks,

Danny

+1  A: 

Your view expects to have some variables defined. You can loop through those variables and display their content as you want.

So you could do, in your action :

def index
    @list = Basecamp::TodoList.find(:all)
end

Then in your view you have access to the @list variable and you can to the following :

<ul>
    <% @list.each do |item| %>
        <li><%= item.to_json</li>
    <% end %>
</ul>

Replacing the json dump by the elements as you wish to display them of course.

You might want to read the rails guides to get a lot more of informations.

Damien MATHIEU
Ok, great. So for the list, how do I determine what tags basecamp uses? For example todo.title todo.body etc. ?
dannymcc
Well the attributes names should be documented in the libarie's API. Take a look at it ...
Damien MATHIEU
Great, I will have a read through both the Rails docs and the 37signals API docs and see how I get on.Thanks for your help!
dannymcc