2015年3月20日金曜日

Googelの検索結果内に検索フォームを表示させる方法

Googelの検索結果内に検索フォームを表示させる方法

Google とかで検索した際に、大きいサイトだと企業名のしたによく検索フォームが表示されているのをよく見かけると思います。

zozo.jpの場合

zozoの検索フォーム

大きな主要サイトだと、Googleが良きに計らってくれて表示させてくれているようですが、

小さなサイトだとなかなかやってくれないようなので、
自分で”もし良かったら、こんな感じでフォーム表示してもらってもいいですか?”的に設定が可能です。

下記より実装方法になります。

実装方法

今回の実装サンプルでは、Ruby on Rails で chema.org と JSON+LDを使っています。

common/_semantic_web.html.erb

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "WebSite",
  "url": "https://www.example.com/",
  "potentialAction": {
    "@type": "SearchAction",
    "target": "https://www.example.com/search?keyword={keyword}",
    "query-input": "required name=keyword"
  }
}
</script>
* https://www.example.com/の部分はご自分のサイトURLに変更してください

* https://www.example.com/search?keyword={keyword}は自分のサイトの検索用URLを指定してください。

* "query-input": "required name=keyword"は検索フォームに入力されてワードを**keyword**という変数名定義します。そして、**required** をつけることで必須ですよと設定しています。必須でなくても問題ないようであれば、requiredは削除してください。

あとは、共通レイアウト等でrenderして読み込んでください。

layouts/application.html.erb

  (略)

  <%= render('common/semantic_web') %>

  </body>
</html>

まとめ

比較的簡単に設定ができて、SEOにも効果があると思うので、設定してもよいのではないでしょうか。
しかも、検索結果に表示された時にインパクトもありますしね。

参考サイト

今回の実装にあたり参考にしたサイトです。

Goolge 公式

Google ウェブマスター向けブログ

https://developers.google.com/structured-data/slsb-overview

schema.org

http://schema.org/WebSite

http://schema.org/SearchAction

http://schema.org/docs/actions.html

2015年3月13日金曜日

AWS CLIを使って OpsworksのSTACK_IDとAPP_IDを取得する

OpsWorksのSTACK_IDとAPP_IDの取得方法について下記します。
今回、値の取得にaws cliを使用しています。
aws cliの設定については、MacにAWS CLIをインストールする方法 をご参考ください。

Stack id 取得

aws cliから取得する場合は、describe-stacksを以下のように実行してください。

aws opsworks --region us-east-1 --profile myprj describe-stacks

–regionが東京(ap-northeast-1) かと思って頑張っていたけど、us-east-1を指定しないとダメらしい。

AWS - describe-stacks

※ は、OpsWorks IDと同じです。

app id 取得

aws cliから取得する場合は、describe-appsを以下のように実行してください。
には、前項で取得したSTACK_IDを指定してください。

aws opsworks --region us-east-1 describe-apps --stack-id <STACK_ID>

AWS - describe-apps

MacにAWS CLIをインストールする方法

pipのインストール

easy_installでpipをインストールします。

pipはphysonのパッケージ管理ソフト

以下を実行します。パスワードが求められるの合わせて入力します。

sudo easy_install pip
Password:

pipのインストールの確認

以下を実行して

pip -h

以下のようにUsageが出力されれば成功です。

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  zip                         DEPRECATED. Zip individual packages.
  unzip                       DEPRECATED. Unzip individual packages.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output.
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup.
  --trusted-host <hostname>   Mark this host as trusted, even though it does not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check

AWS CLIのインストール

上記でpipのインストールが完了したら、下記を実行します。
また、パスワードも聞かれるの再度入力してください。

sudo pip install awscli
Password:

AWS CLIのインストールの確認

以下を実行してみて

aws -h

以下のように usageが出力されればインストール完了です。

usage: aws [options] <command> <subcommand> [parameters]

設定の追加

以下を実行すると

aws configure

下記をきかれるの、順次入力していきます。

AWS Access Key ID [None]: XXXXXXXXXXXXXXXXXXXXXXXXXXX
AWS Secret Access Key [None]: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Default region name [None]: ap-northeast-1
Default output format [None]: json

AWS Access Key IDやAWS Secret Access Keyは各自に環境に合わせて入力してください。

動作確認

以下を実行して、結果がかえってくることを確認してください。

aws ec2 describe-instances

インスタンスの情報を取得

複数アカウント運用する場合

.aws/configを以下のように設定します。

[default]
output = json
region = ap-northeast-1

[profile myprj]
aws_access_key_id = XXXXXXXXXXXXXXXXXXXXXXXXXXX
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
region = ap-northeast-1

[profile myprj]以下を追加するアカウント情報を追記する。

動作確認

以下を実行して、結果がかえってくることを確認できれば成功です。

aws ec2 describe-instances --profile myprj 

2015年3月11日水曜日

複数Bitbucketアカウントをsshで接続する方法

会社とプライベートで別々の複数のBitbucketアカウントを持っている時に、
同一端末から、sshでアクセスする方法です。

今回は仮に、bitbucket1とbitbucket2の2個を使用する事を想定しています。

SSHキーの作成

まず、SSHキーを作成します。
もし、既に自前のSSHがあるようであれば、自前の物を使ってください。

cd ~/.ssh

mkdir bitbucket1
ssh-keygen -t rsa -C ore-tec@example.com -f bitbucket1/id_rsa

mkdir bitbucket2
ssh-keygen -t rsa -C ore-tec@example.com -f bitbucket2/id_rsa

SSHキーの権限変更

sshきーの権限を変更します。

chmod 600 bitbucket1/id_rsa
chmod 600 bitbucket2/id_rsa

.ssh/configの設定

Host bitbucket.org1
  HostName bitbucket.org
  IdentityFile ~/.ssh/bitbucket1/id_rsa
  User bitbucket1
Host bitbucket.org2
  HostName bitbucket.org
  IdentityFile ~/.ssh/bitbucket2/id_rsa
  User bitbucket2

Bitbucketへのキーの追加

[アカウントの管理]画面 > SECURITY [SSH キー] をクリック
[鍵を追加]ボタンをクリック

bitbucket add ssh

上記のkeyの部分に以下でコピーしたモノをペーストします。

cat ~/.ssh/bitbucket1/id_rsa.pub | pbcopy

Labelも自分で認識しやすいモノを設定する。

同様にbitbucket2も別bitbuckeアカウントに鍵を追加します。

.git/configの設定

プロジェクトのルートに移動して.git/configを下記のように設定します。

my_project1の場合

[remote "origin"]
         url = ssh://git@bitbucket.org1/bitbucket1/my_project1.git
         fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master

my_project2の場合

[remote "origin"]
         url = ssh://git@bitbucket.org2/bitbucket2/my_project2.git
         fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master

重要なところは url = の部分で以下のように指定します。

ssh://git@[ssh/config のHOSTで設定したラベル]/[Bitbucketのユーザー名]/[リポジトリ名].git

ディレクトリ構成を維持したままコピーするコマンド

ディレクトリ構成を維持したままコピーするコマンドの使い方です。

シンタックス

cp -r [fromdir] [to-dir]

サンプル

Desktopのtestディレクトリ配下を一式、test_backにコピーするイメージです。

cp -r ~/Desktop/test Desktop/test_back

2015年3月3日火曜日

saveで ActiveRecord::ReadOnlyRecordの対処方法

RailsのActiveRecordでsaveしようとしたら、ActiveRecord::ReadOnlyRecordが発生しました。
私の原因として、joinsメソッドを使って取得したレコードに対して、saveしようとしたのがダメなようです。

発生事象

ダメ例を簡単に書くと下記のようになります。

user_items = UserItems.joins(:user)

user_items.each do |user_item|
  user_item.name = 'aaaaa'
  user_item.save ## ここでエラー
end

対処方法

対象方法としては、readonly(false) メソッドを呼び出します。

user_items = UserItems.joins(:user).readonly(false) ## <= 追加

user_items.each do |user_item|
  user_item.name = 'aaaaa'
  user_item.save
end

とやると、saveに成功します。

statistics