Friday, March 29, 2013

Cameleon v0.1.1

Cameleon v0.1.1リリース

HTTPのスタブを作るためのフレームワークCameleonのv0.1.1をリリースしました。

Github : Cameleon

v0.1.1ではポストされたJSONをバリデーションする機能を追加しました。Cameleonはgemでインストールできます。

$ gem install cameleon

使い方の詳細は前の記事を参照してください。

JSONバリデーション

v0.1.1のJSONバリデーション機能では、POSTまたはGETのHTTPリクエストボディのJSONについて、データ構造の検証を行うことができます。バリデーションのエンジンにはjson-schema gemを利用し、JSON Schema Draft 3のスキーマ定義を利用します。このバリデーション処理をするには、まず設定ファイル(cameleon.yml)に以下の内容を記載します。

validation:
  type: json

その上で、レスポンスフォルダにrequest.schema.jsonというスキーマファイルをおいておくと、リクエストボディにそのスキーマに適合するJSONデータが含まれない場合にはステータス500のエラーが帰るようになります。

例えば次のようなJSONを期待するインターフェイスの場合

{"a": 1}

次のようなスキーマを定義します。

{
  "type" : "object",
  "properties" : {
    "a" : {
      "type" : "integer",
      "required" : true
    }
  }
}

Saturday, February 23, 2013

Handling remote bare Git repository

When you have Git repositories as below.

  • CENTER.git : Central repository you can not manage
  • MAIN.git : Main repository that your team push into
  • LOCAL : Local repository on developers local machine

Merge on bare repository

You can't merge on bare repository such as MAIN.git because bare repository has no working copy. There are no ways to resolve the conflict when it occurs. Therefore, you must checkout the target branch into your local repository, merge your commits, then push to bare repository(MAIN.git).

Merge from external repository

Use 'git fetch' when there are some changes on CENTER.git. But this command doesn't update HEAD position. So you should use 'git reset --soft'.

# on MAIN.git
$ git fetch <CENTER.git>
$ git reset --soft HEAD
check commit histories using 'git log'.

Push specified branch

You can push local branch 'dev-local' into 'dev-remote' on origin.

$ git push origin dev-local:dev-remote

Clone from remote branch

You can checkout 'dev-remote' branch on MAIN.git into 'dev-local' branch on local machine:

git clone <MAIN.git>
git checkout -b dev-local remotes/origin/dev-remote

Push tags into remote repository

You can push local tags into remote repository.

$ git push --tags

This article is translated from Japanese.

Friday, February 22, 2013

How to create new environment type (RAILS_ENV) for Rails3

You know there are development, test and production environments on Ruby on Rails. This article shows you how to create new environment for your system.

We are currently working with:

  • Ruby: 1.9.2
  • Ruby on Rails: 3.0.5

How to refer environment name

You can refer environment name through Rails.env.

p Rails.env #=> "development"

Adding new environment

For example, you can add your new 'staging' environment by these steps. Follow these steps to add your new 'staging' environment.

  1. copy RAILS_HOME/config/environments/production.rb as RAILS_HOME/config/environmentsstaging.rb
  2. add staging section into database.yml
If you have any other configuration file, edit the configuration too.

Then, create database, do migration, and start your staging server.

$ rake db:create RAILS_ENV=staging
$ rake db:migrate RAILS_ENV=staging
$ rails server -e staging
Yes! You now have new 'staging' environment for your project.

gem setting

You can use different gems for your each environment. This situation, write your Gemfile as below.

group :development, :test do
  gem 'sqlite3-ruby'
end
group :staging, :production do
  gem 'mysql2', '0.2.7'
end
You can reject unnecessary gems for specified environment.
$ bundle install --without test development
see: Bundler : Using Groups

This article is translated from Japanese.

Thursday, February 21, 2013

httpstatus コマンドで、HTTP のステータスコードをすばやくしらべる! in Ruby

なんだか流行っているようなので。

一般的な Web Programmer ならば、HTTP Status code はすべて暗記していると聞きました。しかし、僕は初心者なので、なかなか覚えきれていないので、HTTPのステータスコードをさがすのに便利なツールを用意しました。Rubyで書いてます。Githubに上げてあります。

$ httpstatus 40
400: Bad Request
401: Unauthorized
402: Payment Required
403: Forbidden
404: Not Found
405: Method Not Allowed
406: Not Acceptable
407: Proxy Authentication Required
408: Request Timeout
409: Conflict

$ httpstatus bad
400: Bad Request
502: Bad Gateway
インストールはgemで。
$ gem install httpstatus
元ネタなど。

Thursday, February 07, 2013

CentOS, Redmine, ImageMagick, RMagick

CentOSにRedmineをインストールするために、ImageMagickとRubyバインディングのRMagickをインストールしたけど、やたら嵌ったのでメモ。なんか前からImageMagickのインストールはすんなり行ったことがないような。

まずはyumでインストールしようとしたけど、バージョンが古いのでNG。本家からRPMでインストールしてもエラーが出てNG。多分ヘッダーとかのパスの問題っぽいが、ImageMagick-develみたいなものをRPMでどう認識させて良いかわからず断念。(後から考えると、このタイミングでPKG_CONFIG_PATHを設定すればうまく行ったのかも)

最終的には本家からソースを持ってきてインストールして、PKG_CONFIG_PATHを設定した後にRMagickをインストールするとうまく行った。

$ tar xzvf ImageMagick.tar.gz
$ cd ImageMagick-6.8.2-6/./configure
$ make
$ sudo make install
$ sudo /sbin/ldconfig /usr/local/lib
$ gem install rmagick -v '2.13.2'

Bundlerでライブラリが取得さえできれば、Redmineのインストールは特に問題なし。

Tuesday, January 08, 2013

RubyのTwitter gemでrate_limit_statusを取得

Rubygemsにあるtwitterバージョン4以降は、rate_limit_statusを取得するAPIがなくなった。これは、このAPIの利用方法がTwitterのREST APIのエンドポイントURIに依存していて、Twitter側の仕様変更があったとき、このAPIをサポートしているとライブラリの更新だけで(アプリの修正なしに)この変更に追随することができなくなるためらしい。

まあ設計思想としては理解できるが、とはいえこのライブラリを使ってやりたいことはTwitterのREST APIを叩くことだから、Twitter側の仕様変更に影響を受けることはある程度は仕方ないと思うんだが、どうだろうか。もちろんそこが隠蔽されていて、Twitter側のバージョンが変わってもアプリのコードに変更がない、ということが可能であれば美しいけど。

で、無理やりrate_limit_statusを取得するには以下のコード。

Twitter.get('/1.1/application/rate_limit_status.json')[:body]

要するにgetというメソッドを使えば、直接使いたいAPIを叩ける。このコードで結果がHashで返ってくる。

参考)https://github.com/sferik/twitter/issues/318

Friday, November 16, 2012

mmrd (MongoDB MapReduce Driver) リリースしました

MongoDBのMapReduceをNode.jsから実行/テストするためのモジュール、mmrdをリリースしました。

インストール

$ npm install mmrd

使い方

例えばMongoDBのワードカウントなどで、以下のようなMapReduceを書いていたとします。

// map.js
function map() {
  var words = this.text.split(' ');
  words.forEach(function(word) {
    emit(word, 1);
  });
}
//reduce.js
function reduce(key, values) {
  var result = 0;
  values.forEach(function(v) {
    result += v;
  });
  return result;
}

このとき、mmrdを使うと以下のような形でこのMapReduceをNode.jsから実行できます。

// runner.js
var mmrd = require('mmrd');

// map test
(function() {
  console.log("Word count / Map test");
  var doc = { text: "foo bar baz" };
  mmrd.loadMap('map.js');
  mmrd.map(doc, function(key, value) {
    console.log(key, ":", value);
  });
})();

// reduce test
(function() {
  console.log("Word count / Reduce test");
  mmrd.loadReduce('reduce.js');
  var key = "foo";
  var values = [1, 1];
  var reduced = mmrd.reduce(key, values);
  console.log(key, ":", reduced);
})();

// integration test
(function() {
  console.log("Word count / Integration test");
  var docs = [
    { text: "foo bar baz" },
    { text: "foo baz baz"}
  ];
  mmrd.loadMap('map.js');
  mmrd.loadReduce('reduce.js');
  var result = mmrd.mapReduce(docs);
  console.log(result);
})();
起動
$ node runner.js

もちろん、mochaやshouldを使ったテストも書けます。当たり前ですが。

お試しあれ。