0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ruby のライブラリ Faraday を使って kintone の REST API を叩いてみる

Last updated at Posted at 2019-11-21

RubyのHTTPクライアントライブラリFaradayを使って、kintoneのAPIを叩いてレコードを取得したりしてみたときのメモです。

はじめに

少し前に、Qiitaにnet/http を使って kintone の REST API を叩く記事を書いたのですが、Ruby Gemsの HTTPクライアントライブラリFaradayがよく使われているらしいので、Faradayでもkintone REST API を叩いてみたいと思います。

動作環境

  • mac OS10.14.6
  • Ruby 2.6.3
  • Homebrew 2.1.16
  • rbenv 1.1.2
  • Pry version 0.12.2 on Ruby 2.6.3
  • faraday 0.17.0

macな方のRubyの環境構築はこちらを参考にしてください。

faraday のインストール

Gemfileに追記 gem 'faraday'
bundle install

faradayのテスト

$ pry
[1] pry(main)> require 'faraday'
=> true
[2] pry(main)> response = Faraday.get 'https://api.openbd.jp/v1/get?isbn=978-4-87311-686-0&pretty'
[5] pry(main)> require 'json'
=> false
[14] pry(main)> JSON.parse(response.body)                                               => [{"onix"=>
   {"RecordReference"=>"9784873116860",
    "NotificationType"=>"03",
    "ProductIdentifier"=>{"ProductIDType"=>"15", "IDValue"=>"9784873116860"},
    "DescriptiveDetail"=>
     {"ProductComposition"=>"00",
      "ProductForm"=>"BZ",
      "Measure"=>
       [{"MeasureType"=>"01", "Measurement"=>"240", "MeasureUnitCode"=>"mm"},
        {"MeasureType"=>"02", "Measurement"=>"0", "MeasureUnitCode"=>"mm"}],
      "TitleDetail"=>
       {"TitleType"=>"01",
        "TitleElement"=>
         {"TitleElementLevel"=>"01",
          "TitleText"=>
           {"collationkey"=>"ウェブ エーピーアイ ザ グッド パーツ",
            "content"=>"Web API:The Good Parts"}}},
(省略)

faraday でkintone API を叩くテスト

[API 一覧の取得]APIを叩いてみます。
https://developer.cybozu.io/hc/ja/articles/201941934

  • HTTP メソッド: GET
  • URI: https://(サブドメイン名).cybozu.com/k/v1/apis.json

コード

apis.rb
require 'rubygems'
require 'bundler/setup'
require 'dotenv/load'
require 'faraday'
require 'pp'
require 'json'

url = "https://#{ENV['SUBDOMAIN']}.cybozu.com"
connection = Faraday.new(url: url)
response = connection.get '/k/v1/apis.json'
puts JSON.pretty_generate(JSON.parse(response.body))
puts response.status
puts response.success?

出力

$ ruby -w get_apps.rb
{
  "baseUrl": "https://{ドメイン名}.cybozu.com/k/v1/",
  "apis": {
    "app/acl/get": {
      "link": "apis/app/acl/get.json"
    },
    "app/acl/put": {
      "link": "apis/app/acl/put.json"
    },
    "app/customize/get": {
      "link": "apis/app/customize/get.json"
    },
    "app/form/fields/get": {
      "link": "apis/app/form/fields/get.json"
    },
    "app/form/layout/get": {
      "link": "apis/app/form/layout/get.json"
    },
    "app/get": {
      "link": "apis/app/get.json"
    },
    "app/settings/get": {
      "link": "apis/app/settings/get.json"
    },
(省略)
200
true

Basic認証

コード

apis_basic_auth.rb
require 'rubygems'
require 'bundler/setup'
require 'dotenv/load'
require 'faraday'
require 'pp'
require 'json'

url = "https://#{ENV['SUBDOMAIN']}.cybozu.com"
connection = Faraday.new(url: url)
connection.basic_auth(ENV['BASICAUTHID'], ENV['BASICAUTHPASS'])
response = connection.get '/k/v1/apis.json'
puts JSON.pretty_generate(JSON.parse(response.body))
puts response.status
puts response.success?

追記していきます。

参考リンク

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?