読者です 読者をやめる 読者になる 読者になる

Tbpgr Blog

Ruby プログラマ tbpgr(てぃーびー) のブログ

JSON API対応のRuby Clientである json_api_client gem を試す

f:id:tbpg:20160815221724p:plain

JSON API対応のRuby Clientである json_api_client gem を試します。

準備

JSON API 準拠のAPIサーバーをあらかじめ立てておきます。
今回は id:hkdnet さんの下記の記事をもとに Ruby on RailsAPI サーバーを作っておきます。

hkdnet.hatenablog.com

対象モデル

  • User
    • name:string
    • screen_name:string
    • password:string
    • has_many :tweets
  • Tweet
    • user_id :integer
    • text:text
    • belongs_to :user

サンプル

Gemfile

# 略
gem 'json_api_client', '~> 1.0'
# 略

Sample

require "json_api_client"

module ClientSample
  class Base < JsonApiClient::Resource
    self.site = "http://localhost:3000/"
  end

  class User < Base
    has_many :tweets
  end

  class Tweet < Base
  end
end

require "pp"

## Ref
puts "==Ref=="
pp ClientSample::User.all.take(2)
pp ClientSample::Tweet.all.take(2)

## Create
puts "==Create=="
puts "===User==="
pp "User.size = #{ClientSample::User.all.size}"
suzuki = ClientSample::User.create(name: "suzuki", screen_name: "suzuki86")
pp suzuki
pp "User.size = #{ClientSample::User.all.size}"
puts "===Tweet==="
pp "User.size = #{ClientSample::Tweet.all.size}"
suzuki_tweet1 = ClientSample::Tweet.create(text: "suzuki zutsuki", user_id: suzuki.id)
suzuki_tweet2 = ClientSample::Tweet.create(text: "suzuki satsuki", user_id: suzuki.id)
pp suzuki_tweet1
pp suzuki_tweet2
pp suzuki.tweets
pp "Tweet.size = #{ClientSample::Tweet.all.size}"

## update
puts "==Update=="
suzuki.name = "suzuki updated"
suzuki.save
pp suzuki
suzuki.name = "suzuki updated"
suzuki.save
suzuki_tweet1 = ClientSample::Tweet.find(id: suzuki.id).first
suzuki_tweet1.text = "#{suzuki_tweet1.text} updated"
suzuki_tweet1.save
pp suzuki_tweet1

実行結果

==Ref==
[#<ClientSample::User:@attributes={"id"=>"1", "type"=>"users", "name"=>"name0 update", "screen_name"=>"screen_name0"}>,
 #<ClientSample::User:@attributes={"id"=>"2", "type"=>"users", "name"=>"name1", "screen_name"=>"screen_name1"}>]
[#<ClientSample::Tweet:@attributes={"id"=>"1", "type"=>"tweets", "user_id"=>1, "text"=>"name0 < text0"}>,
 #<ClientSample::Tweet:@attributes={"id"=>"2", "type"=>"tweets", "user_id"=>1, "text"=>"name0 < text1"}>]
==Create==
===User===
"User.size = 23"
#<ClientSample::User:@attributes={"name"=>"suzuki", "screen_name"=>"suzuki86", "type"=>"users", "id"=>"24"}>
"User.size = 24"
===Tweet===
"User.size = 70"
#<ClientSample::Tweet:@attributes={"text"=>"suzuki zutsuki", "user_id"=>24, "type"=>"tweets", "id"=>"71"}>
#<ClientSample::Tweet:@attributes={"text"=>"suzuki satsuki", "user_id"=>24, "type"=>"tweets", "id"=>"72"}>
[#<ClientSample::Tweet:@attributes={"id"=>"71", "type"=>"tweets", "user_id"=>24, "text"=>"suzuki zutsuki"}>,
 #<ClientSample::Tweet:@attributes={"id"=>"72", "type"=>"tweets", "user_id"=>24, "text"=>"suzuki satsuki"}>]
"Tweet.size = 72"
==Update==
#<ClientSample::User:@attributes={"name"=>"suzuki updated", "screen_name"=>"suzuki86", "type"=>"users", "id"=>"24"}>
#<ClientSample::Tweet:@attributes={"id"=>"24", "type"=>"tweets", "user_id"=>3, "text"=>"name2 < text3 updated"}>

外部資料