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 is_expected.to

it { is_expected.to 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_expected.to 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 { is_expected.to 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
  end

  def converted_body
    # do something
  end
end
class MyModelSerializer < ActiveModelSerializers::Serializer
  attributes :converted_body
end

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
  end

  def 

  def related_models
    related_models.map do |related_model|
      RelatedModel.new(related_model: related_model)
    end
  end
end
class MyModelSerializer < ActiveModelSerializers::Serializer
  has_many :related_models
end

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

Reference

Find more explainations in https://github.com/rails-api/active_model_serializers/tree/0-10-stable#what-does-a-serializable-resource-look-like


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.

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.

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.

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

# 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(XXX@XXX.mailgun.org) 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 => "smtp.mailgun.org",
	:port => 587,
	:domain => mydomain.club',
	:user_name => "postmaster@mydomain.club",
	: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.


如何在ubuntu16通过终端设置shadowsocks实现科学上网

给三年前买的旧笔记本重装了Ubuntu桌面版,第一件要做的事就是科学上网啦。

┗(・o・)┛ナハ┗(・o・)┛ナハ

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. 设置配置文件

{
"server":"XX.XX.XX.XX",
"server_port":XX,
"local_port":1080,
"password":"your password",
"timeout":600,
"method":"aes-256-cfb"
}


3. 启动shadowsocks

sslocal -c /your/path/shadowsocks.json

如果启动成功了可以看到如下信息(/home/momoko/shadowsocks/ss-config.json是我存放配置文件的路径)

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


4. 设置浏览器

接下来需要设置浏览器,代理到shadowsocks指定端口(默认情况是1080)

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

https://github.com/FelisCatus/SwitchyOmega/releases/

还有配合GFWList的使用方法:

https://github.com/FelisCatus/SwitchyOmega/wiki/GFWList

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

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


5. Attention

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

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


自从学会科学上网,看youtube视频根本停不下来。

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

最后推荐一个颜文字网站:

http://kaomojiya.com/


   Tags
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!