なんとなく日々徒然と

Signature expired

踏み台サーバー上でcapistranoで

1
`return_or_raise': Signature expired: 20130924T043045Z is now earlier than 20130924T043046Z (20130924T043546Z - 5 min.) (AWS::ELB::Errors::SignatureDoesNotMatch)

って出て、デプロイ出来ない。

結論。
サーバーの時刻が遅れてたからでした。
はまったので、みなさんも同じことにならないように。

えっ!

f:id:gogo  _sakura:20130719233028j:image

え・・・・
ええええええ!

f:id:gogo  _sakura:20130719233015j:image

こわい・・・

meat_sauce

thorとかanemoneとかselenium-driverとか使ってみたくて、ついでにgemにしてみました。
cliから一気に複数画面のスクリーンショット取ったり、リンク一覧取得したり、指定タグの中身を取得したりできるツールです(※要 ruby)

https://github.com/YoshitsuguFujii/meat_sauce

なんか勝手にブラウザとか立ち上がりますが。びびらないでください。
あと環境によっては動かないかもしれないです。ごめんなさい。
僕の環境ではFirefoxとsafariなら動きました。

インストール

1
$ gem install meat_sauce

使えるコマンドが見れます。

1
$ meat_sause

ページの全てのリンクを取得します

1
$ meat_sauce show_links --url http://www.yahoo.co.jp 

 -d 数字をオプション追加でさらにリンクを踏んだ先をどんどん表示します。どんどん。

指定したタグの中身を抜き出します

1
$ meat_sauce scraping --url http://www.yahoo.co.jp --tag h2

ソースを表示します

1
$ meat_sauce show_source --url http://www.yahoo.co.jp

画面のスクリーンショットを撮ります

1
$ meat_sauce capture --url http://www.yahoo.co.jp --file-path ~/yahoo.jpg

 -d 数字をオプション追加でリンクを踏んだ先も画像を撮ります。パシャパシャ。

個人的にはスクショとスクレイピングが便利だと思うのですが。どうでしょうか?
動けばですが・・・

bunlderのデフォルトオプション

1
bundle install --without production

とか毎回オプション設定がめんどくさい。
gemrcみたいなのないのかなーと思って探してみたら答えはここにあった。
http://bundler.io/bundle_config.html

bundle config –globalでglobalな設定を見れる。
bundle config –global キー 値で設定できる。

よって

1
bundle config --global without production

ってやったら、出来た。

確認してみる

1
2
3
4
5
bundle config 
Settings are listed in order of priority. The top value will be used.

without
Set for the current user (~/.bundle/config): "production"

ファイルも見てみる

1
2
3
vim ~/.bundle/config
---
BUNDLE_WITHOUT: production

できましたー。

N回injectしたいとき(数値型からinject)

2012-04-24 - なんとなく日々徒然との日記からの焼きまわし感半端ない

1
10.times.inject(0) {|sum, (i, j)| sum + i } # => 0から9までを足して、結果は45

上記を1から10足して55にしたい場合(つまり1始まりにしたい)

とりあえずこうしてみる

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
10.times.with_index(1).inject(0) {|sum, i| p i }

# =>
[0, 1]
[1, 2]
[2, 3]
[3, 4]
[4, 5]
[5, 6]
[6, 7]
[7, 8]
[8, 9]
[9, 10]
[
    [0] 9,
    [1] 10
]

ふむ。
展開させます

1
10.times.with_index(1).inject(0) {|sum, (times_count, index_count)| sum + index_count } # => 55

ransackを使ったpolymorphic関連のモデルの検索処理

polymorphic関連のモデルの検索処理をransack使って書いていて、どうやるんだ?
ってなった。答えはここにあった。
http://stackoverflow.com/questions/13077954/does-ransack-support-the-same-polymorhpic-belongs-to-associations-in-its-searche

polymorphicに関してのわかりやすい説明はこちらのサイトで。

http://shirusu-ni-tarazu.hatenablog.jp/entry/2012/11/04/173742

説明にもここの情報を使わせていただきます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Image < ActiveRecord::Base
  belongs_to :imageable, :polymorphic => true
end

class Company < ActiveRecord::Base
  has_many :images, :as => :imageable
end

class User < ActiveRecord::Base
  has_many :images, :as => :imageable
end

#Imageからみた検索

companyの名前のlike検索

f.text _field :imageable _of _Company _type _name _cont

#Imageからみた検索

userの名前のlike検索

f.text _field :imageable _of _User _type _name _cont

polymorphic名 of 検索したいクラス名 + type + 検索対象列 + 条件
で検索できるみたい。

padrinoでコントローラ名とアクション名

sinatraもよくわからないのに、padrino触ってます。

コントローラ名とアクション名はこうやったら取れんじゃないかと

コントローラ名
request.controller.to _s

アクション名
request.action.to _s

TODOの一覧を出力したい

コード中で

TODO ほにゃらら

って書いておけば、rake notesでTODO一覧を出力できる。

1
2
3
4
5
6
7
rake notes

app/controllers/news_controller.rb:
  * [  9] [TODO] お知らせ一覧データの有効期限の実装を行う

app/controllers/users_controller.rb:
  * [174] [TODO] デバッグ用、後で消す。

ファイアーウォールとかで守られている社内gitサーバーに置かれたgemを使いたい

弊社はgithub enterpriseで運営しています。
外部からは自由にアクセスできないように、ファイアーウォールで社内アクセスのみに接続を限定しています。

が、今やっている案件ではお客様が用意したサーバーにアクセスしなければならず。
社内gitサーバーにアクセスできない状態です。

そういった場合、capistranoでローカルにソースを先にダウンロードしてtarで固めてサーバーに送信するという指定ができます。

config/deploy.rb

1
2
set :deploy_via, :copy
set :copy_cache, true

ですが、Gemfileにか書かれているGemファイルはbundle installで入れるようなので、社内gitサーバーに置かれたgemを参照しようとしたら落ちました。

うーん。gemも一式ローカルに落としてくれんか?と探していたらありました。
rudionrails/capistrano-strategy-copy-bundled ? GitHub

Gemfileにgem ‘capistrano-strategy-copy-bundled’と書いてbundle install。

config/deploy.rb

1
set :deploy_via,    :copy_bundled

でもcap deploy:updateしたら、

1
2
3
4
5
6
7
8
executing locally: "cd /var/folders/dl/n1zf4q750msgc9jcd99hdq3c0000gn/T/20130422015014 && bundle package --all"
Error loading RubyGems plugin "/Users/gogosakrua/.rvm/gems/ruby-1.9.3-p385@global/gems/rubygems-bundler-1.1.0/lib/rubygems_plugin.rb": Could not find rubygems-bundler (>= 0) amongst [actionmailer-3.2.12, actionpack-3.2.12, active_decorato
r-0.3.4, activemodel-3.2.12, activerecord-3.2.12, activeresource-3.2.12, activesupport-3.2.12, arel-3.0.2, builder-3.0.4, cancan-1.6.9, chronic-0.9.1, chunky_png-1.2.7, coffee-rails-3.2.2, coffee-script-2.2.0, coffee-script-source-1.6.1,
compass-0.12.2, compass-colors-0.9.0, compass-rails-1.0.3, compass_twitter_bootstrap-2.2.2.2, country_select-1.1.3, erubis-2.7.0, execjs-1.4.0, fssm-0.2.10, gruff-0.3.7, haml-4.0.0, haml-rails-0.4, hike-1.2.1, i18n-0.6.4, jbuilder-1.0.2,
journey-1.0.4, jquery-rails-2.2.1, json-1.7.7, kaminari-0.14.1, libv8-3.11.8.13-x86_64-darwin-12, log4r-1.1.10, mail-2.4.4, mime-types-1.21, multi_json-1.6.1, pg-0.14.1, polyamorous-0.5.0, polyglot-0.3.3, rack-1.4.5, rack-cache-1.2, rack-
mini-profiler-0.1.23, rack-ssl-1.3.3, rack-test-0.6.2, rails-3.2.12, rails3_acts_as_paranoid-0.2.5, railties-3.2.12, rake-10.0.3, ransack-0.7.2, rdoc-3.12.2, ref-1.0.2, rjb-1.4.6, rmagick-2.13.2, rubyzip-0.9.9, sass-3.2.6, sass-rails-3.2.
6, seed-fu-2.2.0, settingslogic-2.0.9, simple_form-2.1.0, sprockets-2.2.2, therubyracer-0.11.4, thor-0.17.0, tilt-1.3.4, treetop-1.4.12, turbo-sprockets-rails3-0.3.6, tzinfo-0.3.36, uglifier-1.3.0, whenever-0.8.2] (Gem::LoadError)
    command finished in 3794ms

って怒られる。
うーん。gemsetがdefault使われてるなー。ってことで。
いい機会なので、rbenvにしてみました。
http://ksauzz.github.io/blog/2012/03/23/rbenv-ruby-build-rbenv-gemset/[?](http://d.hatena.ne.jp/keyword/http%3A//ksauzz%2Egithub%2Eio/blog/2012/03/23/rbenv%2Druby%2Dbuild%2Drbenv%2Dgemset/?mode=edit)

んで cap deploy:update

1
undefined method `with_clean_env' for Bundler:Module (NoMethodError)

また怒られる。なんでだ。ぐぐっても有益な情報なし。
あ、ひょっとしてbundle exec必要?

ってことでbundle exec cap deploy:update

んで行くはず。

はずって言うのも。あまりにGem一式のダウンロードが重くて、やってらんねぇってなったので。
急遽ファイアーウォールに穴開けてもらうことになったからです。