月
初めて月を撮影した。Pentax K-x、標準の望遠レンズキット300mm、ISO 200、F8、1/30秒。 三脚がないので、ベランダの手すり + 適当な厚みの本で固定し斜め上に向けて撮影。 輪郭がぼやけているのはブレだろうか。
Yosemite で libv8 と therubyracer をインストールする
Mac OS X 10.9 Mavericks 以降だと、libv8 と therubyracer の gem がインストールできないじゃないですか。毎回遭遇するけど、毎回苦しんでいます。
キモは、libv8、therubyracer、OS のバージョンの整合性。あとは定番のオプションを付けること。
http://inoao.hatenablog.com/entry/2014/03/27/183523 に、超便利な表がありました。が、Yosemite が載っていないので、最新版はこうなります。Yosemite = darwin-14 の値は、自分で試してうまくいった組み合わせです。
| CPU | OS | OS Version | libv8 | therubyracer |
|---|---|---|---|---|
| amd64 | freebsd | 8 | 3.3.10.4 | 0.10.2 |
| 9 | ||||
| x86 | darwin | 10 | ||
| 11 | ||||
| linux | ||||
| freebsd | 9 | 3.11.8.3 | 0.11.0 | |
| x86_64 | darwin | 10 | ||
| 11 | 3.3.10.4 | 0.10.2 | ||
| 12 | 3.11.8.3 | 0.11.0 | ||
| 13 | 3.16.14.3 | 0.12.1 | ||
| 14 | 3.16.14.7 | |||
| linux | 3.11.8.3 | 0.11.0 | ||
手順としては、まず、自分の OS のバージョンを調べます。
$ ruby -rubygems -e 'puts Gem::Platform.new(RUBY_PLATFORM)'
x86_64-darwin-14
OS X 10.10 Yosemite の場合、darwin-14 になりました。普通に bundle install すると、以下のように失敗します。
$ bundle install --path vendor/bundle
…(略)…
Installing kaminari 0.16.1
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/Users/takah/.rbenv/versions/2.0.0-p353/bin/ruby extconf.rb --without-system-v8
…(略)…
An error occurred while installing libv8 (3.16.14.3), and Bundler cannot continue. Make sure that `gem install libv8 -v '3.16.14.3'` succeeds before bundling. </code>
なので、上記の表を見て、Gemfile と Gemfile.lock のバージョンが合うように修正します。
さらに、ビルドオプションも付けてやります。gem コマンドでインストールするだけなら、以下のように – で区切ってオプションを渡すことができます。
$ gem install libv8 -v '3.16.14.7' -- --with-system-v8
bundle install でインストールする場合、コマンドラインで直接オプションを指定できないので bundle config コマンドを使います。
$ bundle config build.libv8 --with-system-v8
$ bundle config build.therubyracer --with-v8-dir
このコンフィグオプションは build_info に保存されます。
$ cat vendor/bundle/ruby/2.0.0/build_info/libv8-3.16.14.3.info
--with-system-v8
さて、これで bundle install してみましょう。
$ bundle install
一旦 bundle install –path vendor/bundle すると、オプションが .bundle/config に保存されて、次回以降は –path オプション無しでも同じ動作をしてくれます。
うまく行きました! I’m so happy!
AWS の Route53 でドメインを作成するスクリプト
R53 でドメインを作成するコマンドラインを Ruby で書いた。
使い方
$ ruby create_record -d ドメイン名 [-h ホスト名]
ex) ruby create_record -d google.com -h rarara
-d ドメイン名、だけを指定すると、新規に hosted zone を作成し、
Aレコードと MXレコードを作成する。
-h ホスト名、も付けると、そのドメインの下に Aレコードと
MXレコードを作成する。
コード
require 'rubygems'
require 'aws-sdk'
require 'optparse'
AWS.config( access_key_id: ‘xxxxxxxxxxxxxxxxxxxxx’, secret_access_key: ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’ ) IP=’xxx.xxx.xxx.xxx’
def create_record domain, host = nil
r53 = AWS::Route53.new zone = r53.hosted_zones.select { |z| z.name == “#{domain}.” }.first
host1 = host ? “#{host}.#{domain}.” : “#{domain}.” host2 = host ? “www.#{host}.#{domain}.” : “www.#{domain}”
r53.client.change_resource_record_sets( hosted_zone_id: zone.id, change_batch: { changes: [ { action: ‘UPSERT’, resource_record_set: { name: host1, type: ‘A’, ttl: 300, resource_records: [ value: IP ] } }, { action: ‘UPSERT’, resource_record_set: { name: host2, type: ‘A’, ttl: 300, resource_records: [ value: IP ] } }, { action: ‘UPSERT’, resource_record_set: { name: host1, type: ‘MX’, ttl: 300, resource_records: [ value: “10 #{host1}” ] } }, ] } ) end
def create_hosted_zone domain r53 = AWS::Route53.new resp = r53.client.create_hosted_zone({ name: “#{domain}.”, caller_reference: domain }) puts “Name servers:” for ns in resp[:delegation_set][:name_servers] puts ns end create_record domain end
options = {}
OptionParser.new do |opts| opts.banner = ‘Usage: create_record [options]’ opts.on(‘-h [HOST]’, ‘–host [HOST]’, ‘hostname’) do |v| options[:host] = v end opts.on(‘-d [DOMAIN]’, ‘–domain [DOMAIN]’, ‘domain’) do |v| options[:domain] = v end end.parse!
if options[:host] create_record options[:domain], options[:host] else create_hosted_zone options[:domain] end </code>
Rails を utf8mb4 で動かす
絵文字などを格納できるようにするため、MySQL の encoding を utf8mb4 にすると、1文字が最大 4バイトになる。すると、primary key と unique key として使うカラムが、InnoDB の最大長 767バイトを超えるために、
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
というエラーが発生する。
それを乗り越えるための手順は以下の通り。
1) まず、my.cnf に以下を追加して restart させる。
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
innodb_file_per_table = 1 innodb_file_format = Barracuda innodb_large_prefix </code>
innodb_large_prefix が、767バイトを超えるキーを使うための設定。
collation として utf8mb4_unicode_ci と utf8mb4_general_ci の 2つが考えられるが、ここでは unicode_ci を選んだ。文字列の比較処理が拡張されていて、半角全角を同一とみなしてくれるなどの利点がある。その一方で、濁点などを区別しないという問題もある。
2) 次に、テーブルの ROW_FORMAT を変更する。Dynamic か Compressed にしないと、 innodb_large_prefix が効かない。
現状の ROW_FORMAT を確認すると、全部デフォルトの Compact になっている。
mysql> select row_format from information_schema.tables where table_schema='データベース名';
そこで、ROW_FORMAT を Dynamic に変更する。
mysql> ALTER TABLE テーブル名 ROW_FORMAT=DYNAMIC;
3) 新規に migration で作成されるテーブルの ROW_FORMAT を Dynamic にするため、
以下のコードを追加する(http://qiita.com/kamipo/items/101aaf8159cf1470d823)。
config/initializers/ar_innodb_row_format.rb
ActiveSupport.on_load :active_record do module ActiveRecord::ConnectionAdapters
class AbstractMysqlAdapter
def create_table_with_innodb_row_format(table_name, options = {})
table_options = options.reverse_merge(:options => 'ENGINE=InnoDB ROW_FORMAT=DYNAMIC')
create_table_without_innodb_row_format(table_name, table_options) do |td|
yield td if block_given?
end
end
alias_method_chain :create_table, :innodb_row_format
end
end end </code>
4) 既存のテーブルのエンコーディングを変換する
ALTER TABLE テーブル名 CONVERT TO CHARACTER SET utf8mb4;
参考 http://qiita.com/kamipo/items/101aaf8159cf1470d823 http://blog.kamipo.net/entry/2012/11/13/102024
リツイートしたユーザーの一覧を ruby で取得する
require 'rubygems'
require 'twitter'
client = Twitter::REST::Client.new do |config| config.consumer_key = ‘………’ config.consumer_secret = ‘………’ config.access_token = ‘………’ config.access_token_secret = ‘………’ end
#
指定された Tweet にリツイートしたユーザー一覧を得る
ただし、Twitter API の制限により、最大で100件まで
# tweet = client.status(489034564640063492) puts tweet.text retweets = client.retweets(tweet, count: 100) retweets.each do |retweet| puts retweet.user.screen_name end </code>