Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Open Flash Chart II Plugin for Ruby on Rails on Heroku

on 2011/10/31 - 0 Comments -

Open Flash Chart II Plugin for Ruby on Rails

By using this plugin, you can generate beautiful charts easily on Ruby on Rails, even if it's on Heroku.

Setup:

# cd someproject
# rails plugin install git://github.com/pullmonkey/open_flash_chart.git
# cp vendor/plugins/open_flash_chart/assets/javascripts/swfobject.js public/javascripts/
# cp vendor/plugins/open_flash_chart/assets/open-flash-chart.swf public/



Wirte chart generation codes in some controller:

class TestItController < ApplicationController
  def index
    @graph = open_flash_chart_object(600,300,"/test_it/graph_code")
  end

  def graph_code
    title = Title.new("MY TITLE")
    bar = BarGlass.new
    bar.set_values([1,2,3,4,5,6,7,8,9])
    chart = OpenFlashChart.new
    chart.set_title(title)
    chart.add_element(bar)
    render :text => chart.to_s
  end
end



Write chart display codes in some view:

<script type="text/javascript" src="/javascripts/swfobject.js"></script>
<%= @graph %>



Write route setting in config/routes.rb:

match "/test_it/graph_code" => "test_it#graph_code"



Restart Server:

# rails s



Push to Heroku if it works:

# git push heroku master



All done.

How to generate a specific Date on Ruby

on 2011/10/29 - 0 Comments -


somocode.rb
require 'date'

# Today
Date.today

# 1st of this month
Date.new(Time.now.year, Time.now.month, 1)

# 1st of next month
Date.new(Time.now.year, Time.now.month, 1) >> 1

# 1st of last month
Date.new(Time.now.year, Time.now.month, 1) << 1

# 1 week later
Date.today + 7

# 1 week before
Date.today - 7



And you can use these Date valuables in Model 'where' phrase on Rails.


somocode.rb
models = SomeModel.where("date <= ?", Date.today)


no such file to load -- net/https

on 2011/10/15 - 0 Comments -

When I started using heroku, I got some errors and solved them by installing ext modules.

Installing libopenssl-ruby package by apt-get was one of the solutions, but I couldn't use that package because there was no available package for my ruby, version 1.9.

The error is:


# gem install heroku
# ...(installing)
# heroku
/usr/local/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient.rb:9:in `rescue in ': no such file to load -- net/https. Try running apt-get install libopenssl-ruby (LoadError)
 from /usr/local/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient.rb:5:in `'
 from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
 from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
 from /usr/local/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/rest_client.rb:2:in `'
 from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
 from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
 from /usr/local/lib/ruby/gems/1.9.1/gems/heroku-2.9.0/lib/heroku/client.rb:2:in `'
 from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
 from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
 from /usr/local/lib/ruby/gems/1.9.1/gems/heroku-2.9.0/lib/heroku.rb:6:in `'
 from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
 from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
 from /usr/local/lib/ruby/gems/1.9.1/gems/heroku-2.9.0/bin/heroku:6:in `'
 from /usr/local/bin/heroku:19:in `load'
 from /usr/local/bin/heroku:19:in `
' # (omg)



How I solved:

I installed two ext modules to my ruby - openssl and readline.
# I also installed libssl-dev before installing openssl because ssl.h was needed to install openssl.


# apt-get install libssl-dev
# cd /usr/local/src/ruby-1.9.2-p290/ext/openssl/
# ruby extconf.rb 
# make
# make install
# cd ../readline
# ruby extconf.rb 
# make 
# make install



All done.

VirtualBox + Ubuntu + Ruby + Rails + PostgreSQL

on 2011/10/12 - 0 Comments -

Ruby + Rails + PostgreSQL @ Ubuntu @ VirtualBox @ MacOS の環境構築のメモ。

1. VirtualBox のインストール

4.1.4 をダウンロードしてきて適当にインストール。

2. Ubuntu のインストール

11.04 をダウンロードしてきて VirtualBox に VM 新規追加・インストール。メモリとか HDD とかそのへんはデフォルトで。

3. もろもろインストール

ssh, emacs, git などなどを apt-get でインストール。

4. Ubuntu のネットワーク設定

一旦 VM をシャットダウンして、VirtualBox の設定画面から VM の NIC を増やす。Adapter 1 は NAT のままで、Adapter 2 を Host-only Adapter にする。設定したら VM を起動し、eth1 が up していることを確認。たぶん 192.168.XXX.XXX あたりが振られていて、ホスト側から疎通するはず。

5. Ruby のインストール

ruby-1.9.2-p290.tar.gz をダウンロードしてきて何も考えずいつもの3コマンドでインストール。

6. RubyGems のインストール

rubygems-1.8.10.tgz をダウンロードしてきて ruby setup.rb でインストール。

7. Rails のインストール

ちょっとつまずいたのでメモメモ。zlibc、rake をインストールしてから rails をインストールする。


# apt-get install zlibc zlib1g-dev

# cd /usr/local/src/ruby-1.9.2-p290/ext/zlib
# ruby extconf.rb
# make
# make install

# gem install rack
# gem install rails


なんか rails のインストールでメモリが swap out したので割り当てを増やして(1024MB)やり直したという。。

8. PostgreSQL のインストール

postgresql-9.0.4.tar.gz をダウンロードしてきて解凍後 INSTALL に書かれている以下の手順でインストール。途中 ./configure で readline が見つからず怒られたので readline6-dev をインストールしておきました。


# apt-get install libreadline6-dev

# ./configure
# make
# make install
# adduser postgres
# mkdir /usr/local/pgsql/data
# chown postgres /usr/local/pgsql/data
# su - postgres
# /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
# /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1 &
# /usr/local/pgsql/bin/createdb test
# /usr/local/pgsql/bin/psql test


9. PostgreSQL 用アダプタのインストール

gem でインストール。簡単。


# gem install postgres-pr



これで Ruby on Rails + PostgreSQL の環境が作れたはず。おしまい。

追記 on 2011/10/15:

以下も必要っぽいです。

no such file to load -- net/https

と


# gem install pg -- --with-pg-dir=/usr/local/pgsql/


incompatible character encodings: ASCII-8BIT and UTF-8

on 2011/09/25 - 1 Comments -

Rails の View での表示の際に incompatible character encodings: ASCII-8BIT and UTF-8 でエラーする件について。

エンコーディングが違う文字列を連結しようとすると発生するそうで、layout を入れ子にしていたりすると発生するとか。解決策としては文字列のエンコーディングをすべて utf-8 にそろえればよいとのことで、以下の通り実装。

ちなみに環境は Ruby 1.9.2 と Rails 3.0.9 です。この組み合わせはクサいらしい。


lib/encoding_patch.rb
module ActiveSupport
  class SafeBuffer < String
    def concat(value)
      if value.html_safe?
        super(value.force_encoding('utf-8'))
      else
        super(ERB::Util.h(value.force_encoding('utf-8')))
      end
    end
    alias << concat
  end
end




config/environment.rb
require 'encoding_patch'



確かにこれでエラーは出なくなりました。

参考:
Ruby1.9+Rails3で"incompatible character encodings: UTF-8 and ASCII-8BIT"の対策
# application.rb じゃなくて environment.rb に書くと思った

Ruby on Rails on Heroku + Facebook

on 2011/09/19 - 0 Comments -

Facebook が Heroku と正式に連携したので使ってみたメモ。

アナウンスおよびチュートリアル:
Facebook and Heroku: an even easier way to get started

Heroku では Rails が動くので、Rails で手軽に Facebook App が作れるようになったということですね。

1.Facebook から Heroku にアプリを追加

上のチュートリアルを読めば分かるはず。ポチポチとボタンを押すだけ。

2.Heroku で Rails でアプリを作成する

1で作られるアプリは Pure Ruby なので、自分で作った Rails アプリに入れ替える。とりあえず Facebook のことは考えず、以下の手順をもとに Heroku で Rails のアプリが動くところまで進める。

Getting Started with Heroku
Getting started with Rails 3.0 on Heroku/Cedar

* Heroku では PostgreSQL を使うので開発環境にも必要かも
* バージョンは Ruby が 1.9.2 、Rails が 3.0.9 でした

3.Facebook と連携

Rails が動くようになったら、Facebook が自動で作ってくれたものと入れ替え。
以下あたりを参考に。

Building a Facebook Application With Ruby
Getting Started with Your Facebook App on Heroku
Herokuで作るFacebookアプリ