Larry Price

And The Endless Cup Of Coffee

Using the Trello API in Ruby

| Comments

So, you want to gather data using the Trello API so that you can do something cool with it? And you’re using Ruby? Enter ruby-trello. Install!

1
$ gem install ruby-trello

We’ll start off easy, and assume that we’re writing a personal application where we only need to access data for one user at a time. We start by configuring ruby-trello. I’m going to assume that you’ve already generated a public key and received a member token and stored them in your environment.

global_config_test.rb
1
2
3
4
5
6
7
8
9
10
require 'trello'

Trello.configure do |config|
  # API key generated by visiting https://trello.com/1/appKey/generate
  config.developer_public_key = ENV['PUBLIC_KEY']

  # Member token
  # larry-price.com/blog/2014/03/18/connecting-to-the-trello-api/
  config.member_token = ENV['MEMBER_TOKEN']
end

This connects me to a specific member as found through ENV['MEMBER_TOKEN']. I previously wrote another post about getting a member token from a user.

For demonstration, I’ll find myself, grab my first board, and then display the name, names of lists, members who have worked on the project, and some numbers about each of the cards in the board. This is essentially my proof of concept for a super-cool web-app I wrote called Ollert.

global_config_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...

# find myself
me = Trello::Member.find("_larryprice")

# find first board
board = me.boards.first
puts board.name
puts "Lists: #{board.lists.map {|x| x.name}.join(', ')}"
puts "Members: #{board.members.map {|x| x.full_name}.join(', ')}"
board.cards.each do |card|
      puts "- \"#{card.name}\""
      puts "-- Actions: #{card.actions.nil? ? 0 : card.actions.count}"
      puts "-- Members: #{card.members.count}"
      puts "-- Labels: #{card.labels.count}"
end

Wow, cool! Such data! This is really great for a single user because we only have to make the connection to Trello once (which is not incredibly fast). However, this won’t work in a multi-user environment since we configured ruby-trello to use a specific member token. So how do we connect to multiple members at a time? Let’s print out the same data we did above for a single user, but using Trello::Client to connect to Trello.

client_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
require 'trello'

me = Trello::Client.new(
  :developer_public_key => ENV['PUBLIC_KEY'],
  :member_token => ENV['MY_MEMBER_TOKEN']
)

you = Trello::Client.new(
  :developer_public_key => ENV['PUBLIC_KEY'],
  :member_token => ENV['YOUR_MEMBER_TOKEN']
)

[me, you].each do |user|
  puts user.fullname
  board = user.boards.first
  puts board.name
  puts "Lists: #{board.lists.map {|x| x.name}.join(', ')}"
  puts "Members: #{board.members.map {|x| x.full_name}.join(', ')}"
  board.cards.each do |card|
        puts "- \"#{card.name}\""
        puts "-- Actions: #{card.actions.nil? ? 0 : card.actions.count}"
        puts "-- Members: #{card.members.count}"
        puts "-- Labels: #{card.labels.count}"
  end
end

Now, as your friend and teacher, I command you to use this knowledge to go do cool stuff with Trello!

Comments