LoginSignup
1
1

More than 3 years have passed since last update.

Airtable API ruby クライアント airtable を使う

Last updated at Posted at 2019-12-09

概要

  • Airtableは、excel様の表計算の使用感で、入力型の指定、添付ファイル、1対多のデータを取り扱えるサービスです。
  • AirtableはデータにアクセスできるAPIを用意しています。
  • 今回は、APIをrubyで使いやすくデータを読み込み書き込みできるAPIクライアントgem airtableを使ってみました。

インストール

railsであれば、下記の一文を足して bundle install です。

gem 'airtable'

railsでない場合は、activesupportが必要なので下記のようにしてください。

gem install activesupport
gem install airtable
# 使いたいところでrequireする
require 'active_support/all' # https://github.com/Airtable/airtable-ruby/issues/5
require 'airtable'

使い方

下記のようなデータ(Airtableではbaseと表現されるの以下baseと表現します。)があると仮定します。

  • eventsテーブル。 image.png

eventsテーブルとpalcesが1対1。eventsテーブルとtagsテーブルが1対多の関係にあります。

前準備

baseにアクセスするには、アカウント自身のapi keyと base自身のapi idが必要です。下記からコピーしてください。

基本的な操作

  1. アカウント自身のapi key を使ってAirtableにアクセスするclientを作る
  2. clientをに対して、 base自身のapi idtable名 を使ってtableを指定
  3. tableに対してデータ読み書き操作を実行する

例えば、FESTIVAL baseの eventsテーブルの全データを取得する場合は以下の通りです。

tableを指定

require 'active_support/all' # airtableがActiveSupportをつかっているので必要
require 'airtable'
api_key = 'アカウント自身のapi key' # アカウント毎に1つ
app_token = 'base自身のapi id' # FESTIVAL base

client = Airtable::Client.new(api_key)
table = client.table(api_token, 'events') # events tableを指定する

table.all で全データを読み取る

records = table.all # events tableを全部呼び出す
p records
[
#<Airtable::Record :id=>"rec4jTsyTBJ2GEG9D">,
 #<Airtable::Record :place=>["recUZQa12QxnpYlD4"], :start=>"2020-02-19", :tags=>["recynBCi1UsxNakr6"], :attachments=>[{"id"=>"attOUbz2csayVdwgf", "url"=>"https://dl.airtable.com/.attachments/534ea5f6b38dbd51cbd168e71dce36b6/2ec2b44f/thumbnail_book_smile.jpg", "filename"=>"thumbnail_book_smile.jpg", "size"=>13155, "type"=>"image/jpeg", "thumbnails"=>{"small"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/f0a901d1f23fe8150e67f8d6e897e641/86ac3878", "width"=>36, "height"=>36}, "large"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/a2efaf458b5779af1edc3afe5eb5c4ae/f2bb971a", "width"=>180, "height"=>180}, "full"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/f9899e2b7b132282ac7028f363c8956c/5861b6c7", "width"=>3000, "height"=>3000}}}], :notes=>"夏目漱石のこころを朗読するよ\n", :name=>"朗読フェスティバル", :id=>"recLaCiRU8AAGm0PF">,
 #<Airtable::Record :place=>["recUZQa12QxnpYlD4"], :start=>"2019-10-22", :tags=>["recpnIdd9uMlegCLZ"], :attachments=>[{"id"=>"attCLeFeSWWViub0n", "url"=>"https://dl.airtable.com/.attachments/ed89fb1a36074958b3e97b8288b4eb06/13ddd3b9/rapper.png", "filename"=>"rapper.png", "size"=>31094, "type"=>"image/png", "thumbnails"=>{"small"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/22b1e4ab19464c76bd14d2714d9c62e6/e88cc241", "width"=>36, "height"=>36}, "large"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/7added43e6cd8c130a4674c4dd7f597d/1a7682f1", "width"=>180, "height"=>180}, "full"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/9d13f0b73a6b82a6339c81d06f0afa96/23df6c2a", "width"=>3000, "height"=>3000}}}], :notes=>"エリザベートよ", :name=>"演劇フェスティバル", :id=>"recMrGMLpV8orHwK2">,
 #<Airtable::Record :place=>["recrbd8K65GT5bFJT"], :start=>"2019-10-15", :tags=>["recx6N45MwnImnIqi", "recynBCi1UsxNakr6"], :attachments=>[{"id"=>"attNoJgJSivCZxZ6b", "url"=>"https://dl.airtable.com/.attachments/eba3a91c1e8484e604d8709b6a39e610/7dc48cb0/engeki.png", "filename"=>"engeki.png", "size"=>287149, "type"=>"image/png", "thumbnails"=>{"small"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/ee3d568f09b1bdf1116a2a58cb8c614e/3d722f18", "width"=>44, "height"=>36}, "large"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/0d4fb6ce5e22dc5a885327099c378347/70ee925c", "width"=>500, "height"=>408}, "full"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/5f99a30d260d640b6b31c69dabeea3da/e517f559", "width"=>3000, "height"=>3000}}}], :notes=>"吹奏楽の演奏があるよ", :name=>"音楽フェスティバル", :id=>"recbAn2S44M4IAzEP">
]

tableに新規レコードを書き込む

record = Airtable::Record.new(name: 'hoge festival', notes: "note\nmemo")
table.create(event)

新規レコードが書き込まれました。

Image from Gyazo

関連レコードを参照する

Airtableも魅力のひとつは関連レコードを容易に書き込めることです。
一手間かかりますが、書き込まれた関連レコードもAPIをつかうことで参照することができます。

事例は、eventのタグ付けように用意したtagsテーブルのデータを読み込む方法です。

  • eventsテーブル。
    image.png

  • tagsテーブル
    image.png

まず、eventのデータを引っ張ります。

event = table.select(formula: 'name = "音楽フェスティバル"').first

こちらのデータのtagsデータ見てみます。

event.tags
=> ["recx6N45MwnImnIqi", "recynBCi1UsxNakr6"]

tagsデータにはidが入っています。
このidを使って、こちらを使って関連データ(tagsのデータ)を引っ張ることができます。
idを元にしてデータを持ってくるには find methodを使います。(なんだかrailsっぽくてわかりやすいですね!)

irb(main):039:0> table.find('recx6N45MwnImnIqi')
=> #<Airtable::Record :name=>"食べ物可", :events=>["recbAn2S44M4IAzEP"], :id=>"recx6N45MwnImnIqi">
irb(main):040:0> table.find('recynBCi1UsxNakr6')
=> #<Airtable::Record :name=>"ペット可", :events=>["recbAn2S44M4IAzEP", "recLaCiRU8AAGm0PF"], :id=>"recynBCi1UsxNakr6">
irb(main):041:0> 

添付ファイルにアクセスする

添付ファイルにも簡単にアクセスできます。
eventsテーブルは attachementsとう名称で添付ファイル(画像とか)を管理しています。
こちらも下記のようにアクセスできます。

event.attachements

=> [{"id"=>"attOUbz2csayVdwgf", "url"=>"https://dl.airtable.com/.attachments/534ea5f6b38dbd51cbd168e71dce36b6/2ec2b44f/thumbnail_book_smile.jpg", "filename"=>"thumbnail_book_smile.jpg", "size"=>13155, "type"=>"image/jpeg", "thumbnails"=>{"small"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/f0a901d1f23fe8150e67f8d6e897e641/86ac3878", "width"=>36, "height"=>36}, "large"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/a2efaf458b5779af1edc3afe5eb5c4ae/f2bb971a", "width"=>180, "height"=>180}, "full"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/f9899e2b7b132282ac7028f363c8956c/5861b6c7", "width"=>3000, "height"=>3000}}}]

上記配列、ハッシュの中身を見てもらえればわかるかと思います。
意外に便利だなーと思ったのは、thumbnailアップロードするだけで大中小のサムネイル画像も作ってもらえるので、そのまま流用すれば、サムネイル作る手間が省けます!!

"thumbnails"=>
{"small"=>{"url"=>"https://dl.airtable.com/.attachmentThumbnails/f0a901d1f23fe8150e67f8d6e897e641/86ac3878", 
"width"=>36, 
"height"=>36}, 
# 以下large, fullが続く。

以上です。
Airtableもっと流行るといいなー。

1
1
0

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
1
1