LoginSignup
17
16

More than 5 years have passed since last update.

ruby-trello gem で API を利用して Trello を操作する #trello #ruby #api

Last updated at Posted at 2015-02-12

概要

ruby-trello gem で API を利用して Trello を操作します

Installation

$ gem install ruby-trello

Usage

前提

環境変数の設定に dotenv gem を利用します 1
dotenv についてはこちらの記事にまとめてあります。
dotenv gem で環境変数をスマートに管理

認証準備

  • CONSUMER_KEY / CONSUMER_SECRET の取得

Trello にログイン後、下記 URL より CONSUMER_KEY / CONSUMER_SECRET を取得します。

https://trello.com/1/appKey/generate

  • アプリケーショントークンの取得

Trello にログイン後、下記 URL より OAUTH_TOKEN を取得します。

※ expiration を伸ばしたい場合は 1day を任意の設定に変更する。never にすれば期限なし。

trello_api1.png

  • Allow を選択すると以下のような出力を得ます。your_token... の部分にトークンが出力されているのでコピーします
You have granted access to your Trello information.

To complete the process, please give this token:

  your_token....

.env の設定

.env ファイルを作成し、前述の手順で取得した値を設定します

TRELLO_CONSUMER_KEY=your_trello_consumer_key
TRELLO_CONSUMER_SECRET=your_trello_consumer_secret
TRELLO_OAUTH_TOKEN=your_trello_oauth_token

特定のカードの情報を取得するサンプルコード

処理対象のカードは以下。

trello_api2.png

card から以下の情報を取得します

  • id
  • name
  • desc
  • checklist
    • name
    • size
    • items
      • name
      • state
require 'trello'
require 'dotenv'
require 'pp'

Dotenv.load
Trello.configure do |config|
  config.consumer_key = ENV['TRELLO_CONSUMER_KEY']
  config.consumer_secret = ENV['TRELLO_CONSUMER_SECRET']
  config.oauth_token = ENV['TRELLO_OAUTH_TOKEN']
end

card = Trello.client.find(:card, 'card_id')
card_part = {
    id: card.attributes[:id],
    name: card.attributes[:name],
    desc: card.attributes[:desc]
}

pp card_part

checklist = card.checklists.first
checklist_part = {
  name: checklist.attributes[:name],
  size: checklist.attributes[:check_items].size,
  items: checklist.attributes[:check_items].map { |item|
    {
      name: item['name'],
      state: item['state']
    }
  }
}

pp checklist_part

:runner: サンプルコードを実行します

{:id=>"card_id",
 :name=>"sample card",
 :desc=>"Sample Description"}
{:name=>"Sample Todos",
 :size=>3,
 :items=>
  [{:name=>"sample list1", :state=>"incomplete"},
   {:name=>"sample list2", :state=>"incomplete"},
   {:name=>"sample list3", :state=>"incomplete"}]}

:books: 外部資料

trello api document
ruby-trello GitHub

:books: 脚注

17
16
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
16