ぽんこつメモ

https://github.com/kanorimon

railsアプリ開発(twitter投稿)

gemインストール

# gem install twitter

require

controllerに以下のrequireを追加

require "twitter"

ソース

Twitter.configure do |config|

  config.consumer_key       = ENV['CONSUMER_KEY']

  config.consumer_secret    = ENV['CONSUMER_SECRET']

  config.oauth_token        = current_user.token

  config.oauth_token_secret = current_user.secret

end

twitter_client = Twitter::Client.new

twitter_client.update("テスト")

railsアプリ開発(twitter認証)

gem

# vi Gemfile

gem 'omniauth'を追加

gem 'omniauth-twitter'を追加

developer登録

https://dev.twitter.com/apps/

設定ファイル

#vi config/initializers/omniauth.rb

設定内容(秘密情報は環境変数に設定する)

 Rails.application.config.middleware.use OmniAuth::Builder do

  provider :twitter, ENV['CONSUMER_KEY'],ENV['CONSUMER_SECRET']

end

controller/model/viewはここを参考にしました

http://d.hatena.ne.jp/katryo/20120708

http://npb.somewhatgood.com/blog/archives/715

authで取ってこられるデータ

https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema

 

railsアプリ開発(github連携)

キーを作成

# ssh-keygen

id_rsa.pubをGithubアカウントとのSSHKeyに登録

ローカルレポジトリをコミット

# git init

# git add .

# git commit -m "first commit"

リモートレポジトリを設定

# git remote add origin git@github.com:ユーザ名/レポジトリ名.git

リモートにpush

# git push -u origin master

railsアプリ開発(準備)

アプリケーション作成

# rails new memoapp

Gemfile編集

# vi Gemfile

therubyracerのコメントアウトをはずす

unicornのコメントアウトをはずす

# bundle install

データベース準備

# mysql -u root -p

mysql> create database memoapp_db;

database.yml設定

# vi config/database.yml

設定内容

mysql: &mysql

  adapter: mysql2

  encoding: utf8

  reconnect: false

  pool: 5

  database: "<%= ENV["MYSQL_DB"] %>"

  username: "<%= ENV["MYSQL_USER"] %>"

  password: "<%= ENV["MYSQL_PASSWORD"] %>"

  socket: /var/lib/mysql/mysql.sock

development:

  <<: *mysql

production

  <<: *mysql 

unicorn.rb設定

# vi config/unicorn.rb

設定内容

http://unicorn.bogomips.org/examples/unicorn.conf.rbを参考に以下を書き換え

working directoryをコメントアウト

pid File.expand_path('log/unicorn.pid', ENV['RAILS_ROOT'])

stderr_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])

stdout_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])

環境変数設定

export RAILS_ROOT=railsのルートディレクトリ

export MYSQL_DB=データベース名

export MYSQL_USER=mysqlユーザ名

export MYSQL_PASSWORD=mysqlパスワード

unicorn起動(開発環境)

# unicorn_rails -c config/unicorn.rb -E development