Use `raise_error` matcher in Rspec

Yesterday I came accross a tricky useage of raise_error matcher in rspec, first I try to use the one line syntax
rb it { raise_error(SomeError) }

but I got an eror which stops the test before the test is finished.

It is confusing in the first time, because is shortcut for expect(subject).

Then I found this excellent explanation in Github isses.

So expect(subject) is not a block, subject is executed instantly, so rspec will stop before evaluating raise_error matcher.

In order to make it work, we can make subject itself a block
subject { -> { raise SomeError } }

it { raise_error(SomeError) }

But this syntax is creepy, instead it’s better to use
subject { raise SomeError }

expect { subject }.to raise_error(SomeError)

Serialize Non-ActiveRecord Model Using ActiveModel::Serializer in Rails

Recently I’ve been working in backend which connects to another API, you may need to send a specific format of JSON data which is not the same as database resource.

Create A Serializable Resource

Currently I’m using active_model_serializers 0.10.0. From the documentation we know ActiveModel::Serializer wraps a serializable resource, to create a serializable resource we need to create a plain old Ruby object inherits ActiveModelSerializers::Model, then create a corresponding serializer.

For example:
class MyModel < ActiveModelSerializers::Model
def initialize(title:, body:)
@title = title
@body = body

def converted_body
# do something

class MyModelSerializer < ActiveModelSerializers::Serializer
  attributes :converted_body

Handling Nested JSON

You can use has_many, has_one method in ActiveModel::Serializer to handle nested json, although it doesn’t mean the same with Rails model relations.

For example:
class MyModel < ActiveModelSerializers::Model
def initialize(object: object)
@title = title
@body = body


def related_models do |related_model| related_model)

class MyModelSerializer < ActiveModelSerializers::Serializer
  has_many :related_models

ActiveModelSerializers::Serializer will automatically look for a serializer named RelatedModelSerializer.


Find more explainations in

Plot Financial Data Using Matplotlib (super basic)

import libraries

In order to plot the data, we need to import libraries for dataframe and plot, also set plot inline if you’re using a notebook.
python import numpy as np import pandas as pd import matplotlib.pyplot as plt %matplotlib inline

plot stock price

Code is super simple, you just need to set the index of the dataframe to timestamp. If you find the figure to small, use pylab to set the figure size.
python from pylab import rcParams rcParams['figure.figsize'] = 50,13 apple_data = market_train_df[market_train_df['assetCode'] == 'AAPL.O'] apple_data.set_index('time')['close'].plot(grid = True)

compare multiple plots

Then I’m trying to sanitize the data by looking at the figure it self, I want to plot all suspicious assets.
python for code in suspicious_asset_code: apple_data = market_train_df[market_train_df['assetCode'] == code] apple_data.set_index('time')['close'].plot(grid = True)

This only gives me one figure with all assets, it’s ugly. I want to plot them one by one.

Subplot will give you figures put up together, use
python # treat the whole figure as a grid, specify how many rows and columns you wanna plot and the index of the current sub plot fig.add_subplot(row, columns, index)
Then you’ll get graphs all together to detect abnormal data.

Configure Mailgun settings in Rails app

Recently I’m learning to create a Rails 5 api only application with an android mobile application. I want to send a activation email after user registered from the android app, and I found there is a free Mailgun add-on on Heroku. It’s perfect choice to start to play with because you can send up to 100 messages/hour without being charged. The documentation is good enough but I do found several steps confusing.

Firstly, you need to create a Mailgun account and add your credit card information, otherwise you can’t send to recipents other than authorized ones(which means test only).

Then, use the test domain( to ensure Mailgun not being blocked by your test mail address(In my case I use qq mail at first, it actually blocks all traffic from mailgun). After that, you need to add a custom domain. Go to your domain name provider and configure DNS settings as following the steps of Mailgun instructions. In my case I use Namecheap and it took me a while(a cup of coffee) for Mailgun to verify my domain.

After your custom domain is active, configure your development environment and make sure it works. In my case I use smtp instead of api call, the settings are for example:

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
	:address => "",
	:port => 587,
	:domain =>',
	:user_name => "",
	:password => XXXX

Then start your rails server and see if the mail was delivered. I spent quite a few hours here because at first my emails weren’t delivered. I refered to the log and showed Mailgun accepted the mail address, and one minute later a ESP throttling message showed up. The reason is Mailgun being blocked by certain ISP(like qq mail, in this case I have to use an extra mail hosting service), so I couldn’t see any mail sent inbox.

If OK in development environemnt, change your production settings as well, it should work as well.




shadowsocks的安装有两种方法,一是安装shadowsocks-qt5 GUI版,二是通过终端运行shadowsocks。我试过了第一种GUI安装,但是一直连不上服务器,发现输出的json配置文件格式是错误的,于是选择了第二种, 具体步骤如下:

1. 下载shadowsocks

sudo apt-get update
sudo apt-get install python-pip
sudo apt-get install python-setuptools m2crypto
pip install shadowsocks

2. 设置配置文件

"password":"your password",

3. 启动shadowsocks

sslocal -c /your/path/shadowsocks.json


INFO: loading config from /home/momoko/shadowsocks/ss-config.json
2016-12-06 19:31:54 INFO     loading libcrypto from
2016-12-06 19:31:54 INFO     starting local at

4. 设置浏览器


我使用的浏览器是Ubuntu下的Chromium,chrome插件本来可以在chrome商店里下载到,成功翻墙之前是进不去的。这里推荐从github上下载SwitchyOmega,里面有中文下载说明, 网址如下:


下载结束之后需要设置代理选项,情景模式里选socks 5, 端口号1080,其余设置参考上述链接。

打开youtube或是facebook测试一下,有点小惊喜呢。┌ ≧∇≦

5. Attention

值得注意的是,虽然浏览器可以翻墙了,但是用终端访问比如curl 还是没有响应,这里需要安装额外的插件,我使用的是proxychains4,具体安装方法请自行搜索。如果你运行的是例如polipo全局代理的插件,在终端运行某些命令会失败(比如说git clone),这时候暂时关掉代理插件即可。

什么?不会安装?浏览器都能翻墙了还不谷歌一下。 ( ̄▽ ̄人)) ((人 ̄▽ ̄)


へ( ̄ ̄へ)(ノ ̄ ̄)ノ パラパラ♪


life ( 4 )
music ( 1 )
programming ( 14 )
spirituality ( 1 )
中文 ( 2 )
language-learning ( 1 )

About Me

I'm a university student in the last year, also a web developer. I love exporing cultures, learning languages and making BGM music!