Category Archives: Code

Use AngularJS on Rails form_for


I used AngularJS on Rails form_for. The most annoying thing is form_for generates form tag with action attribute.

Environment

  • Rails 4.1.8
  • Ruby 2.2.2
  • AngularJS 1.4.7
  • Ubuntu 15.04

Circumstance

Rails form_for generates form tag with some attributes, including action. Of course, we can add other attributes. But, AngularJS conflicts with this.

In the following code, problem occurs.

After clicking the button, AngularJS process will be executed and original form process will be executed.

AngularJS prevent original process when form doesn’t have action attribute, but rails add action attributes automatically. I couldn’t find the way to prevent rails from adding action attribute.

Solution

Add onsubmit="return false;" to the form tag.

また AngularJS: How to prevent form submission after click on button? に記載されているような方法もありますが、 javascript として 他のところで使えないので、 上に書いたやり方の方がきれいに見えます。


Perl: The way to Send Mail


I’ll introduce the way to send mail in Perl. 私はこの仕組みを、サーバの状態を必要な時にメールで通知するスクリプトなどで利用しています。 (ref.: Server Monitoring Script made by me, a Perl beginner)

Environment

Computer which runs perl

I tried in 2 environments.

Case 1

  • Ubuntu 15.04
  • Perl 5.20.2

Case 2

  • EC2 Amazon Linux 2015.03
  • Perl 5.16.3

SMTP Server

  • EC2 Amazon Linux 2015.03
  • Postfix
    • Authenticate with password in CRAM-MD5 way

Code

Here’s the most simple sample code.

If you don’t require password authentication, there’s no need to use Digest::MD5 or Authen::SASL, or the line written auth. But supposing CRAM-MD5 authentication, they are necessary. auth method of Net::SMTP requires Authen::SASL, which requires Digest::MD5.

Authen::SASL が入ってないと auth を実行しても認証がおこなわれません。 Digest::MD5 が入っていないと、 Error: No SASL mechanism found と表示されます。

パスワードの認証方式は 自動で判別されています。 認証サーバで Digest-MD5 が有効であれば CRAM-MD5 よりも Digest-MD5 を優先的に使います。

そして、認証をするにはインストールしなければならないパッケージがあります。

  • For Ubuntu
    • libdigest-hmac-perl
    • libauthen-sasl-perl
  • For Amazon Linux
    • perl-Authen-SASL

Amazon Linux の場合、 MD5 のモジュールが入っているパッケージ perl-Digest-MD5 はあらかじめインストールされています。

In many case, perl is installed in advance and Net::SMTP module is available to use, but authentication requires additional module installation.


Drive Rails on Nginx and Passenger, Install Instructions


I setup rails on Passenger and Nginx. But I didn’t install from package, but compiled the source to hide some http response header. I wrote the instruction to install them, and what I did.

Environment

  • Ruby 2.2.2
  • Nginx 1.8
  • Passenger 5.0.18
  • EC2 Amazon Linux 2015.03

Overview

I suppose you installed ruby.

  1. Install Passenger
  2. Install Nginx
    • Download Nginx and required module
    • Compile and Install
  3. nginx configuration
  4. (Re)Boot nginx

Install Passenger

Install Passenger, as gem install passenger.

Install Nginx

If you execute passenger-install-nginx-module as root, nginx will be installed. But in the case, Nginx version and Passenger version should be presented in http response header.

For removing such no use headers, you should compile the source of nginx with nginx module.

Download Nginx and required module

Execute sudo su -, and switch user to root. And execute cd /usr/local/src.

Download Nginx

Download Nginx (tar.gz format). I downloaded Nginx 1.8.0, which was the latest stable nginx. You can find it at Nginx Official Download Page.

And extract it.

Download headers-more-nginx-module

Plain Nginx and Passenger respond http header including versions of Nginx and Passenger, and execution time, etc. Not to expose server’s internal information, use headers-more-nginx-module with Nginx.

I got the module (.tar.gz format) of latest version from github, which is on github.

Release list page : Releases · openresty/headers-more-nginx-module

After downloading, Extract it.

After extraction, change the .tar.gz file name to understandable name. Tab helps you to change the name, using extracted directory name.

Compile and Install

The installer require the following components. If there’s any deficit, installation will be stopped, so don’t worry.

  • C compiler (/usr/bin/cc)
  • C++ compiler (/usr/bin/c++)
  • wget or curl
  • curl development header with ssl support
  • Openssl development header
  • Zlib development header
  • rake, rack, ruby development header, rubygems

If you have installed nginx into the same place in the same way, the name old nginx binary will be changed to nginx.old.

Swap

If the RAM is less than 1 GB, like EC2 micro instance, swap memory is required. Execute the following commands as root.

Don’t worry, even if you start installation with small amount of memory, instruction is shown in installation process.

Execute passenger-install-nginx-module, with no option.

Now, the installation will start.

まずは 処理の説明が表示され、 Press Enter to continue, or Ctrl-C to abort. と表示されます。 Enter を押して続行します。 途中で()めたくなったら いつでも Ctrl-C を押せば止められます。

続いて Which languages are you interested in? と表示されるので 必要なものを画面の指示にしたがって選択し、 Enter を押します。

インストールされているソフトウェアのチェックが終わると、Do you want this installer to download, compile and install Nginx for you? と、 インストール方法を聞かれます。 ソースからコンパイルを行うので 2. No: I want to customize my Nginx installation. (for advanced users) を選びます。

And in my case, not installed component PCRE was automatically downloaded and installed.

Next, you should input source file location and compile option. I inputted the following.

Where is your Nginx source code located?
/usr/local/src/nginx-1.8.0
Where do you want to install Nginx to?
Pressed Enter, without any character.
Extra Nginx configure options
--add-module=/usr/local/src/headers-more-nginx-module-0.261
To use headers-more-nginx-module, add --add-module. Options set as default is printed on console.

At last, confirmation message will be shown, then Enter if there is no mistake. And compiling and installing will be started.

nginx configuration

I copied /opt/nginx/conf/nginx.conf.default to /opt/nginx/conf/nginx.conf.

For running Rails application, the following configuration helps. I show a part of the configuration. I set client_max_body_size 10 MB for uploading images. (It’s 1M as default.) Other configuration setting is explained on Phusion Passenger users guide, Nginx version or so.

And add code to modify response header.

Check the configuration file with the following command. It’s ok if no error is shown.

(Re)Boot Nginx

I usually use /opt/nginx/sbin/nginx -s reload for rails reloading, but it wasn’t effective in this case.

Stop Nginx with the command /opt/nginx/sbin/nginx -s stop, and boot it by execution of /opt/nginx/sbin/nginx.

If you launch nginx first time, only execute /opt/nginx/sbin/nginx.


Google Apps Script : Process only on business day


Have you ever planed to execute Google Apps Script only on business day? Send mail only on business day, or process on the weekend except holiday, etc. I thought script to make it.

First, write core script and set trigger to work on weekdays.

Process except Holiday

It means to process only on business day. Get holiday information from Google Calendar.

Add the following code to the first of the process.

It stop the process on Sunday and Saturday, and also on holiday in Japanese holiday calendar, prepared by Google. You can configure the day on which the script works, by setting trigger, too.

It is good to create function that returns bool value.

If you have other special holiday, create special holiday calendar and add some code to check it. Calendar ID which should be passed to getCalendarById is saw in the page of its calendar.

Continue reading Google Apps Script : Process only on business day

Post Weather Forecast to Slack Regularly


I created a script to post weather forecast to Slack everyday, with Google Apps Script. It can be configured to post only specific weekdays.

Direction

  • Get forecast information from Open Weather Map.
  • Write script with CoffeeScript.
  • Post to Slack using Incoming WebHooks.
  • Post automatically using Google Apps Script.

Preparation

Get url of Incoming Webhooks in Slack.

Continue reading Post Weather Forecast to Slack Regularly