Ubuntu, Rails3.2の時刻関連メモ
忘れやすいのでメモしておきます。
Rails
config/application.rb
# 追加 config.time_zone = 'Tokyo' config.active_record.default_timezone = :local
RubyでWebスクレイピングするためのサンプル
サンプルで使ったライブラリ
gem install nokogiri gem install mechanize
- Nokogiri
- HTMLを操作するためのライブラリ
- tenderlove/nokogiri · GitHub
- Mechanize
- Webサイトへ自動でアクセスするためのライブラリ
- tenderlove/mechanize · GitHub
HTMLを操作するサンプル
- 型番で検索して、価格コムの最安値を出力するサンプル
# coding: utf-8 require 'rubygems' require 'nokogiri' require 'open-uri' model_number = 'L32-V09' page = open("http://kakaku.com/search_results/#{model_number}/") html = Nokogiri::HTML(page.read, nil, 'Shift_JIS') html_item = html.search('//div[@class="item clearfix"]').first min_price = html_item.search('//div[@class="price clearfix"]//span[@class="yen"]//a').first.content puts "#{model_number}: 最安値 #{min_price}"
- 実行結果
L32-V09: 最安値 \39,339 〜
Webサイトへの自動アクセスサンプル
- Googleで「Ruby」を検索して、結果のタイトルを出力する
require 'rubygems' require 'mechanize' a = Mechanize.new { |agent| agent.user_agent_alias = 'Mac Safari' } a.get('http://google.com/') do |page| search_result = page.form_with(:name => 'gbqf') do |search| search.q = 'Ruby' end.submit search_result.search(".//a[@class='l']").each_with_index.map do |link, i| puts "#{i} #{link.content}" end end
- 実行結果
0 オブジェクト指向スクリプト言語 Ruby 1 Ruby Programming Language 2 Ruby - Wikipedia 3 Ruby入門 4 Amazon.co.jp: プログラミングRuby―達人プログラマーガイド: デビット ... 5 Rubyとは - はてなキーワード 6 日本Rubyの会 公式Wiki - FrontPage 7 Rubyを最大63%高速化した中学生は超多忙! − @IT自分戦略研究所 8 日本で生まれ世界が育てた言語 Ruby:ITpro 9 逆引きRuby - namaraii.com 10 ついに軽量Rubyの「mruby」のソースコードが公開!
バリデーションエラー時にdiv.field_with_errorsを出力しないようにする
config/application.rb
config.action_view.field_error_proc = Proc.new { |html_tag, instance| %Q(#{html_tag}).html_safe }
Source:
Ruby on Rails Guides: Configuring Rails Applications
Rails3.2.1でMySQLを使う。
UbuntuにMySQLサーバーをインストール
sudo apt-get install mysql-server sudo apt-get install libmysqlclient16-dev
vi /etc/mysql/my.cnf
[mysqld] ... default-character-set=utf8 skip-character-set-client-handshake
Railsプロジェクト作成
rails new sandbox -d mysql
vi config/database.yml
development: adapter: mysql2 encoding: utf8 reconnect: false database: sandbox_development pool: 5 username: root password: socket: /var/run/mysqld/mysqld.sock
rake db:create rake db:migrate rake db:fixtures:load
VMのCentOS5.5-x86_64に Rails3.1環境を作成する
sshインストール
yum install openssh-server
adminユーザーがsudoコマンドを使用できるようにする
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
admin ALL=(ALL) ALL <-- 1行追加
sambaインストール
yum install samba
共有ディレクトリ作成
cd ~
cd mkdir/samba
chmod 777 samba
sambaアクセスユーザ作成
pdbedit -a admin
tdbsam_open: Converting version 0 database to version 3.
new password:
retype new password:
iptables無効化
sudo /etc/init.d/iptables stop
sudo chkconfig iptables off
/etc/samba/smb.conf
[homes]
comment = Home Directories
browseable = no
writable = yes
path = /home/admin/samba
sudo /etc/init.d/smb restart
sudo chkconfig smb on
Ruby, Rails環境
# ファイルの取得
$ cd /usr/local/src
$ sudo wget http://dag.wieers.com/packages/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
$ sudo wget http://dag.wieers.com/packages/RPM-GPG-KEY.dag.txt
# rpmパッケージのインストール
$ sudo rpm -ivh rpmforge-release-0.3.6-1.el5.rf.i386.rpm
$ sudo vi /etc/yum.repos.d/rpmforge.repo
#enabled = 1
enabled = 0
RVMインストール
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
sudo yum install gcc-c++.x86_64 patch.x86_64 readline.x86_64 readline-devel.x86_64 zlib.x86_64 zlib-devel.x86_64 libyaml-devel.x86_64 libffi-devel.x86_64 openssl-devel.x86_64 make.x86_64 bzip2.x86_64 iconv-devel.x86_64 sqlite-devel.x86_64
source .bashrc
rvm install 1.9.2
rvm use 1.9.2
Using /home/admin/.rvm/gems/ruby-1.9.2-p290
rvm gemset create rails31
rvm gemset use rails31
sqlite-rubyのインストール
cd /tmp
wget http://www.sqlite.org/sqlite-autoconf-3070400.tar.gz
tar zxvf sqlite-autoconf-3070400.tar.gz
cd sqlite-autoconf-3070400
./configure --prefix=/usr/local
make
make install
Railsイントール
gem install rails
rails new test
cd test
GemFile
gem 'execjs'
gem 'therubyracer'group :production do
gem 'therubyracer-heroku', '0.8.1.pre3' # you will need this too
gem 'pg'
end
bundle install
.rvmrc
rvm use 1.9.2@rails31
rails s
ImageMagickインストール
sudo yum install ImageMagick-devel
herokuの設定
gem install heroku taps
cd ~
ssh-keygen -t rsa -C "sample@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/admin/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/admin/.ssh/id_rsa.
Your public key has been saved in /home/admin/.ssh/id_rsa.pub.
The key fingerprint is:
e9:0d:8d:e6:cb:01:19:40:9d:4f:02:02:37:1c:77:ad takao.taira@gmail.com
heroku keys:add
cd samba/sample
heroku create sample
git init
git add .
git commit -m 'init'
git push heroku master
postgresql
sudo yum install postgresql-devel
graphviz
cd /etc/yum.repos.d
wget http://graphviz.org/graphviz-rhel.repo
Ubuntu11.04にSilexをインストール
Silexをインストールした際のメモです。
mkdir silex cd silex/ wget http://silex.sensiolabs.org/get/silex.phar nano index.php
<?php require_once __DIR__.'/silex.phar'; $app = new Silex\Application(); $app->get('/hello/{name}', function($name) use($app) { return 'Hello '.$app->escape($name); }); $app->run();
cd /var/www/ sudo ln -s /home/user/silex/
http://(サーバーアドレス)/silex/index.php/hello/silex
[:Source]
Silex - The PHP micro-framework based on Symfony2 Components
TOPページ - Silex ユーザーガイド