Contenu connexe
Similaire à Rails and twitter #twtr_hack (20)
Rails and twitter #twtr_hack
- 2. Agenda
1. ruby / rails に関して
2.Twitter連携アプリ作成の実例
2.1.下準備
2.2.Sign in with Twitter
2.3.Tweet
2.4.Deploy
- 10. アプリ登録
Write権限も取れるように
dev.twitter.com
Read and Write に変更
dev.twitter.com
Consumer
key /
Cosumer secretを
控えておく
- 12. rails準備
rails new コマンドにより、アプリケーションの枠組みが作成される
rails new <アプリ名>
生成されるファイル群
-rw-r--r-- 1 i7a16k staff 765 7 29 14:02 Gemfile
-rw-r--r-- 1 i7a16k staff 2463 7 29 14:02 Gemfile.lock
-rw-r--r-- 1 i7a16k staff 9208 7 29 14:02 README.rdoc
-rw-r--r-- 1 i7a16k staff 274 7 29 14:02 Rakefile
drwxr-xr-x 8 i7a16k staff 272 7 29 14:02 app/
drwxr-xr-x 10 i7a16k staff 340 7 29 14:02 config/
-rw-r--r-- 1 i7a16k staff 159 7 29 14:02 config.ru
drwxr-xr-x 3 i7a16k staff 102 7 29 14:02 db/
drwxr-xr-x 3 i7a16k staff 102 7 29 14:02 doc/
drwxr-xr-x 4 i7a16k staff 136 7 29 14:02 lib/
drwxr-xr-x 3 i7a16k staff 102 7 29 14:02 log/
drwxr-xr-x 8 i7a16k staff 272 7 29 14:02 public/
drwxr-xr-x 3 i7a16k staff 102 7 29 14:02 script/
drwxr-xr-x 8 i7a16k staff 272 7 29 14:02 test/
drwxr-xr-x 3 i7a16k staff 102 7 29 14:02 tmp/
drwxr-xr-x 4 i7a16k staff 136 7 29 14:02 vendor/
- 15. TOPページ表示(1)
controllerを追加し、TOPページを作成する
rails g controller home index
生成されるファイル群
create app/controllers/home_controller.rb
route get "home/index"
invoke erb
create app/views/home
create app/views/home/index.html.erb
invoke test_unit
create test/functional/home_controller_test.rb
invoke helper
create app/helpers/home_helper.rb
invoke test_unit
create test/unit/helpers/home_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/home.js.coffee
invoke scss
create app/assets/stylesheets/home.css.scss
- 19. Twitter ログイン
gem設定をGemfileに追加
Gemfile
gem ‘omniauth-twitter’
bundle install
omniauth用の設定ファイルを追加
config/initializers/omniauth.rb
TWITTER_CONSUMER_KEY = ‘xxxxx’
TWITTER_CONSUMER_SECRET = ‘yyyy’
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET
end
※dev.twitter.comで取得したconsumer keyとconsumer secretを使う
- 23. callbackの制御
ログイン用のコントローラーを作成
rails g controller sessions
routes.rbに設定を追加
routes.rb
match ‘/auth/twitter/callback’ => ‘sessions#create’
sessions_controller.rb に、createメソッド追加
sessions_controller.rb
def create
raise request.env[“omniauth.auth”].to_yaml
end
ユーザ情
報がこの中に入っているので
確認してみる
- 25. 取得された情報
oauth後取得されている情報
provider: twitter
uid: '118282075'
info:
nickname: i7a16k
name: i7a
location: Spikers Camp
image: http://a0.twimg.com/profile_images/1602452666/
i7aicon_normal.jpg
description: i7aで「いな」と読ませたいらしいです。16kはspikeoutというゲーム
より。エンジニアとして http://giftee.co/
の中の人をやってます。ちなみにIME and DiMENSiONの中の人。男なんですけど、キ
ティが好きです。 (キティの中の人含め)
urls:
Website: http://and-d.i7a16k.net/
Twitter: http://twitter.com/i7a16k
credentials:
token: 118282075-xxxxxxxxxxxxxxxxxxxxxxxxx
secret: yyyyyyyyyyyyyyyyyyyyyyyy
- 26. モデルを作ろう
userモデルを作成
rails g model user provider:string uid:string
name:string token:string secret:string
rake db:migrate
カラム名 型 意味
id integer 自動生成
provider string 連携サービス名
uid string 連携サービスのユーザid
name string 連携サービスでの名前
token string アクセストークン
secret string シークレットトークン
- 28. ログインロジック
sessions#createにロジック追加
sessions_controller.rb
def create
auth = request.env[“omniauth.auth”]
user = User.find_by_provider_and_uid(auth[“provider”],auth[“uid”])
|| User.create_with_omniauth(auth)
session[:user_id] = user.id
redirect_to root_url
end
user.rb にユーザ作成処理追加
user.rb
self.create_with_omniauth(auth)
create! do |user|
user.provider = auth[“provider”]
user.uid = auth[“uid”]
user.name = auth[“info”][“name”]
user.token = auth[“credentials”][“token”]
user.secret = auth[“credentials”][“secret”]
end
end
- 30. ログイン後
application_controller.rb に current_user メソッド追加
application_controller.rb
helper_method :current_user
private
def current_user
@current_user ||= User.where(:id => session[:user_id]) if session[:user_id]
end
index.html.erbにログイン状態に応じた制御を追加
index.html.erb
<% if current_user %>
こんにちは <%=current_user.name%> さん
<% else %>
<%= link_to ‘Sign in with Twitter’, ‘/auth/twitter’ %>
<% end %>
- 32. ログアウト実装
index.html.erb に ログアウトのリンク追加
index.html.erb
<% if current_user %>
こんにちは <%=current_user.name%> さん
<%= link_to ‘Sign Out’, signout_path %>
<% else %>
<%= link_to ‘Sign in with Twitter’, ‘/auth/twitter’ %>
<% end %>
routes.rb にログアウトの設定追加
index.html.erb
match ‘/signout’ => ‘sessions#destroy’, :as => :signout
sessions_controller にdestroyメソッド追加
sessions_controller.rb
def destroy
session[:user_id] = nil
redirect_to root_url
end
Notes de l'éditeur
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n