2011年5月24日 #ruby #sinatra

Sinatra使って二日目ですが、サーバーを再起動しないと修正したコードが反映されないことに気づきました。これは不便なので、改善する方法を紹介します。

shotgunというgemを使えばOKです。インストールし、ruby -rubygemsではなくshotgunでサーバを起動します。

$ sudo gem install shotgun
$ shotgun myapp.rb
== Shotgun/WEBrick on http://127.0.0.1:9393/
...

これでサーバを再起動せずにホットデプロイができるようになります。

他にもSinatra::ReloaderSinatra::Reloaderが公式サイトで紹介されていますが、shotgunは一番簡単だそうです。

参考サイト:
http://stackoverflow.com/questions/5684266/can-i-do-sinatra-program-without-restart-server
What happened to reloading in Sinatra

2011年5月23日 #mac #ruby #環境構築 #sinatra

Sinatraを試したところこのようなエラーになりました。

ruby -rubygems myapp.rb
no such file to load -- ubygems (LoadError)

ググッてみたらやはりPATHが間違ったっぽいです。
Macportでrubygemsをインストールすることで解決できるそうです。
その後はgemを再度インストールする必要があるらしいです。
操作の流れ:

#パスの確認
which gem
/usr/bin/gem

sudo port install rb-rubygems
...
# Terminal再起動
# パスの確認
which gem
/opt/local/bin/gem

sudo gem install sinatra

一旦はこれで解決です。

参考になったサイト:
http://d.hatena.ne.jp/holypp/20110319/1300556202
http://d.hatena.ne.jp/Kuna/20090315/1237121262

2011年5月20日 #php #facebook #oauth

Our automated systems have detected that you may be inadvertently allowing authentication data to be passed to 3rd parties.

ここ最近Facebookから”認証情報をサードパーティに渡している可能性がある、48時間内に修正しろ”みたいなメールが届いた人が多いと思います。
メール本文です:

Dear Developer of xxxxxx {your app name} xxxxx, Our automated systems have detected that you may be inadvertently allowing authentication data to be passed to 3rd parties. Allowing user ids and access tokens to be passed to 3rd parties, even inadvertently, could allow these 3rd parties to access the data the user made available to your site. This violates our policies and undermines user trust in your site and Facebook Platform. In every case that we have examined, this information is passed via the HTTP Referer Header by the user’s browser. This can happen when using our legacy authentication system and including <iframe>, <img> or <script> content from 3rd parties in the page that receives authentication data from Facebook. Our legacy mechanism passes authentication information in the URL query string which, if handled incorrectly, can be passed to 3rd parties by the browser. Our current OAuth 2.0 authentication system, released over a year ago, passes this information in the URL fragment, which is not passed to 3rd parties by the browser. Please ensure that you are not allowing this data to be passed immediately. Accessing your site as a test user while running a HTTP proxy/monitor like Charles or Fiddler is the best way to determine if you are allowing this information to be passed. If you discover the issue, you can do one of two things: 1. Migrate your site to use our OAuth 2.0 authentication system. We are requiring all apps and sites to update to this mechanism by Sept. 1, 2011. Migrating now will address this issue and ensure that you are one of the first to meet the deadline. For more details, please see our Authentication Guide. 2. Create and use an interstitial page to remove the authentication data before redirecting to your page with 3rd party content. This approach is used by many of our largest developers today (although they are all migrating to OAuth 2.0 shortly). This is a simple and straightforwardchange that should have minimal impact on your site. For more details on this approach, see our Legacy Connect Auth doc. Because of the importance of ensuring user trust and privacy, we are asking you to complete one of the above steps in the next 48 hours. If you fail to do so, your site may be subject to one of the enforcement actions outlined in our policies. If you have any questions or believe you have received this message in error, please contact us. Facebook Developer Relations

多分認証システムが古い(OAuth 2.0を使ってない)かつ、該当のページに<iframe>などのタグがあるのを条件としてFacebook側が検知してたと思います、別にサードパーティにどうこうした訳ではなく。

ソースの確認

まずユーザ認証のところのソースを確認しましょう。
古い認証システムを使う場合これに似てるはずです。
ソースはFacebookのhttp://developers.facebook.com/docs/authentication/connect_auth/より。

 $api_key = "YOUR_API_KEY";
 $interstitial_page = "YOUR_SECURE_URL"; //URL with no 3rd party apps

 $url='http://www.facebook.com/login.php?api_key=' . $app_id
   . '&session_version=3&next=' . urlencode($interstitial_page)
   . '&v=1.0&return_session=1&fbconnect=1'
   . '&cancel_url=' . urlencode($interstitial_page);

 echo "Welcome to the Old Auth flow";
 echo "<p>";

 echo("<a href='" . $url . "'>"
   . "<img src='http://static.ak.facebook.com/images/"
   . "devsite/facebook_login.gif'></a>");

この場合nextパラメータで指定したページに行く時、URLパラメータでuidとかaccess_tokenが渡されますので、危ないと。

OAuth 2.0を使ったログインURLはこうです。

https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=email,read_stream

改修

メールで書かれた通り一つはOAuth2.0に移行する、一つは「中間ページ」挟んでユーザ情報をセッションに保持して隠蔽する。

OAuthについてはhttps://developers.facebook.com/docs/authentication/で詳しく説明しています。

後者の場合はhttp://developers.facebook.com/docs/authentication/connect_auth/の最後でサンプルコードがあるので参考できます。

2011年5月19日 #ruby #rails #rails3.1

Rails3.1.0 beta1をインストールするときにあったトラブルです。

インストール

// 3.1.0 betaに更新
sudo gem install rails --pre

// バージョン確認
rails -v

// ダミープロジェクト作成
rails new railsfoo

// 必要なGemをインストール
cd railsfoo
bundle install

// サーバ起動
rails s

トラブル

ここまで順調でしたが、http://127.0.0.1:3000にアクセスしたときにundefined method `context’ for #Sprockets::Environment:のトラブルに会いました。

Sprockets trouble

どうやらこのSprocketsというのがポイントのようです。
ぐぐってみたらgithubでこのようなコメントがありました。

Just to save someone else the 2 minutes waiting for a bundle update sprockets that will fail, beta.2 is required by rails 3.1.0beta1. You want gem ‘sprockets’, ‘2.0.0.beta.2’ in your Gemfile.
github https://github.com/rails/rails/issues/453

のようでGemfileに下記を追記し再度bundle update。

gem 'sprockets', '2.0.0.beta.2'

解決〜

rails server running

2011年5月17日 #mac #app

とてもシンプルで使いやすいタイマーアプリTimeRingFocus
を発見しましたので紹介させていただきます。
Mac専用になります。

時間の設定にはマウスクリックするか、キーボードのTabキーでhh:mm:ss間で切り替えられます。
そして時間を入力してreturnキー(Enterキー)を押せば設定完了です。とてもシンプルです。

TimeRingFocus

例えば15分と設定したい場合の操作手順は:
+option + space(MacアプリAlfredのホットキー)
+“ti"を入力(よく使うのでtiだけで起動できる)し、returnキー
+tabキーを一回、"15"と入力、returnキー
慣れたら5秒以内で設定できます〜

Setupボタンで設定画面に行きますが、こちらのsoundを「Glass」に設定しています。
全部試しましたが、これが一番いいかと思います。

TimeRingFocus Setting

普段の仕事では結構こまめに8分とか15分とかを設定して、
その間は集中することに決めてSkypeなども見ないようにしています。
結構効率アップにつながると思います。

注意点

command + Wで閉じたらアプリは終了
command + Mで最小化した場合は、Bring to Frontが無効となる
command + Hで隠すのは正常動作

もともと勉強の目的もあって当サイトのFacebookのLikeボタンやTwitterボタンを全部手書きで用意したのですが、
ブログのRSS登録者を50%増やすシンプルな6つの方法 | Last Day. jpを読んでmixiやグリーなども一応設置したほうがいいかもと思いまして実践しました。
FacebookとTwitter、はてなはもちろん、グリー、EvernoteやDeliciousなどまで対応してて便利ですよ。

WordPressにソーシャルブックマークボタンを楽々設置:WP Social Bookmarking Light | ninxit.blog

設定

設定方法はとても簡単です。WPの設定画面でWP Social Bookmarking Lightを選択肢
下記のように必要なサービスを左にドラッグ&ドロップすればOKです。

WP Social Bookmarking Light Settings

General Settingタブ

「Singular」がDisabledにすれば記事の一覧ページにもボタンが反映するようになりますが、そうすると画面の読み込みが結構重くなるようでデフォルトのEnableのままにしています。

Facebookタブ

LikeボタンのVersionはデフォルトは「XFBML」となっていますが、XFBMLはもう廃止予定なのでiFrameに変更します。

WP Social Bookmarking Light Facebook Setting

2011年5月15日 #php #php #cakephp #xml

CakephpでXMLを配列に変換するのはとても簡単です。
下記のファンクションを使えば一発でできます。

uses('Xml');
$xml = new Xml("/path/to/xml");
$xml_array = Set::reverse($xml);
2011年4月22日 #php #php

こちらを参照してください。

  • Mcrypt Requirements
  • Mcrypt Installation PHPを--with-mcrypt*=DIR*再コンパイルする必要があるそうです。またlibmcryptも2.5.6以上のバージョンを求められます。

一般的にもしfunction is undefinedになったら、何かライブラリをコンパイルする必要があるということになります。

# for ubuntu
sudo apt-get install php5-mcrypt

# for mac
sudo port install php5-mcrypt
2011年4月22日 #diary #evernote #blog #twitter

Evernote使い始めたのは結構前ですが、ちゃんとノートブックを整理してライフログ始めたのはまだこの二ヶ月の話です。
それからなかなかブログ更新するきっかけが少なくなりましたね。ただ教訓としてメモするのは基本Evernoteに蓄積してるし、普段思いついたものやストレス、情報共有とかは随時Twitterにつぶやくようになりました。

まあ、Evernote書いたメモって共有する価値はあるといえばありますが、何だかんだ重複作業になるような気がして抑えてはいます。またEvernoteって、基本自分向けなので気軽に書けるメリットもありますし、仕事が忙しくなったら本当にEvernoteに書くのも精一杯になりますからね。
と言ってもブログ書かないのは、それなりのインプットがないことと思いますよ。この業界で働いているなら、絶対これだけたみんなに伝えたいくらいの出来事はあるでしょうと思います。自分と同じような誤りや遠回しは避けて欲しいぐらい。
そういう意味ではちゃんと反省しないといけないと思いますよ、ここ最近の仕事とプライベートを。ただ疲れたを言い訳にしてないか、あるいは勉強不足でインプットが足りないかって。

酒飲んで調子に乗っていろいろ書いたけど、本当反省&改善したい。

最後に、
この記事もiPhone FastEverって書いてます。いや〜本当に便利!チェックなしで発信!