特定のサイトを簡単に監視したい
概要
指定したURL先のステータスコードを監視したい。
今回はステータスが200になったらLINEに通知を飛ばす。
使用したもの
- AWS EC2
- shell
- LINE Notify
つくりかた
1. AWSのEC2を立てる
2. Line Notifyのトークンをゲットする
3. shell scriptsを以下のかんじで書いてテキトウな場所に配置
#!/bin/sh url=http:{{監視したいURL}} ACCESS_TOKEN={{2で取得したトークン}} status=`/usr/bin/curl -LI $url -o /dev/null -w "%{http_code}" -s` if [ $status = 200 ]; then curl -X POST -H "Authorization: Bearer $ACCESS_TOKEN" -F "message=$status" https://notify-api.line.me/api/notify curl -X POST -H "Authorization: Bearer $ACCESS_TOKEN" -F "message=メッセージ" https://notify-api.line.me/api/notify else echo $status fi
4. cronに設定
*/5 * * * * source /home/ec2-user/urlwatch.sh
補足
LINE NotifyのAPIドキュメントはこちら。
Ruby|配列の計算ふりかえり
そういえばRubyは配列同士で計算ができるんだった!
ということをうっかりしていたのでもう一度整理してみる。
# 配列を2つ用意 a = [1, 2, 2] b = [1, 3, 4, 5] # それぞれ計算してみる p a + b #concatと同じ p a.concat(b) #配列の結合 p a | b #和集合 p a - b #差集合 p a & b #積集合 # 出力結果 [1, 2, 2, 1, 3, 4, 5] [1, 2, 2, 1, 3, 4, 5] [1, 2, 3, 4, 5] [2, 2] [1, 3, 4, 5]
ううむ、すてき。
Rails|Deviseのログアウトでルーティングエラー
事象
ユーザー認証にDeviseを使用しているのですが、サインアウトをしようとした際、以下のようにエラーになってしまいます。 ログインまわりのソースコード↓
<% if user_signed_in? %> <!-- current_user は現在ログインしているUserオブジェクトを返すdeviseのHelperメソッド --> <!-- *_path はUserモデルを作成したときに、 deviseにより自動で作成されてますので、rake routesで確認できます --> Logged in as <strong><%= current_user.email %></strong>. <%= link_to 'プロフィール変更', edit_user_registration_path %> | <%= link_to "ログアウト", destroy_user_session_path, method: :delete %> <% else %> <%= link_to "サインイン", new_user_registration_path %> | <%= link_to "ログイン", new_user_session_path %> <% end %>
対応方法
DeviseはデフォルトでDELETEメソッドによりサインアウトさせていますが、今回はこれをGETに変更します。
① hmtl上で送信する際のメソッドを変更
② /config/initializers/devise.rb の config.sign_out_via のメソッドをGETに変更
特定のページのみ特定のCSSを読み込ませる
やりたいこと
特定のページ(今回はサイトのTOPのWelcomeページ)のみ特定のCSSを読み込ませたい。
HOWTO
① 特定のページ用のlayoutを作成する
/app/views/layouts/welcome_layout.html.erb
② コントローラーに使用するlayoutを明記する
class WelcomeController < ApplicationController layout "welcome_layout" # ->これ def index end end
③ /config/initializers/assets.rb に以下を記述
Rails.application.config.assets.precompile += %w( welcome/creative.css ) Rails.application.config.assets.precompile += %w( welcome/creative.min.css ) Rails.application.config.assets.precompile += %w( welcome/style.css )
④ welcome_layout.html.erbに以下を記述
<%= stylesheet_link_tag 'welcome/style', media: 'all' %> <%= stylesheet_link_tag 'welcome/creative', media: 'all' %> <%= stylesheet_link_tag 'welcome/creative.min', media: 'all' %>
※ <%= stylesheet_link_tag ‘application’, media: 'all' %>
は記述しないこと
⑤ サーバー再起動
Vagrant開発環境でメールサーバを立てる
やりたいこと
普段Vagrant上でRuby on Railsのアプリを開発しています。
ユーザー認証にDeviseを使っていてサインアップの際、開発環境でアプリからメールを送りたい場面があったので、今回MailCatcherを使ってメールサーバをVagrant上に立てました。
※ 今回VagrantのIPアドレスは192.168.33.60
としています。
HOWTO
1.Gemをインストールする
※ アプリで使う他のGemと衝突する恐れがあるため、Gemfileには書かない。
$ gem install mailcatcher
2.以下をenvironment/development.rbに記述
config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 } config.action_mailer.default_url_options = { host: '192.168.33.60', port: 3000 }
3.MailCatcherを起動する
$ mailcatcher --http-ip 192.168.33.60 Starting MailCatcher ==> smtp://127.0.0.1:1025 ==> http://192.168.33.60:1080 *** MailCatcher runs as a daemon by default. Go to the web interface to quit.
4.ブラウザからアクセスする
5.ルート画面からサインインし、メールが送られると、コンソールに以下の文言が表示される
Sent mail to programming_girl@hoge.com (10.1ms) Date: Wed, 05 Oct 2016 09:56:34 +0900 From: please-change-me-at-config-initializers-devise@example.com Reply-To: please-change-me-at-config-initializers-devise@example.com To: programming_girl@hoge.com Message-ID: <57f44fc21f0f5_1cfc3f81bcda37ac325df@develop.mail> Subject: Confirmation instructions Mime-Version: 1.0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit <p>Welcome programming_girl@hoge.com!</p> <p>You can confirm your account email through the link below:</p> <p><a href="http://192.168.33.60:3000/users/confirmation?confirmation_token=WPTMz1JQJm3gx4YxCfSt">Confirm my account</a></p> Redirected to http://192.168.33.60:3000/ Completed 302 Found in 200ms (ActiveRecord: 12.4ms)
6.ブラウザ見るとこんなかんじ
本文に確認URLがあるので、Pushする。
あるいは5の文言中のconfirmリンクをブラウザで叩く
http://192.168.33.60:1080:3000//users/confirmation?confirmation_token=79y3We4KFXSMzExiEqsx
7.ログイン画面に遷移するので、ログインする
git configの値の設定
はじめに
いつもいつも値のユーザー名やメアドの設定方法をググっている気がするので、備忘録(´・ω・`)
コミット時のユーザーを設定するよ。
設定の確認
$ git config --global --list user.name=programming_girl user.email=programming_girl@hoge.com core.excludesfile=~/.gitignore core.pager=less -R core.editor=vim color.ui=auto pull.rebase=true push.default=current include.path=~/.gitconfig.local
値の設定
$ git config --global user.name programming_girl $ git config --global user.email programming_girl@hoge.com