ダン・クァン・ミン Blog

はじめまして

Vim Config

.vimrc

set number
set ruler
set showmatch
set hlsearch

"--- tab ----
set tabstop=4
set shiftwidth=4

"--- indent ----
" 'autoindent' depends on a above line.
"set autoindent

" 'smartndent' analies several lines.
set smartindent

" 'cndent' is greatest for C-launguage.
"set cindent

"--- status ---
set laststatus=2

"--- ctags ---
set tags=.tags

"--- list ----
set list
set listchars=tab:>-

"--- decoration ----
highlight SpecialKey ctermfg=grey
highlight ZenkakuSpace cterm=underline ctermfg=darkgrey

"--- xml sort ---
command XmlSort %s/></>\r</g | setf xml | normal gg=G

"--- open binary ----
augroup BinaryXXD
    autocmd!
    autocmd BufReadPre  *.bin let &binary =1
    autocmd BufReadPost * if &binary | silent %!xxd -g 1
    autocmd BufReadPost * set ft=xxd | endif
    autocmd BufWritePre * if &binary | %!xxd -r | endif
    autocmd BufWritePost * if &binary | silent %!xxd -g 1
    autocmd BufWritePost * set nomod | endif
augroup END

Setup Apache, Mysql and Rails

Install apache2

$ sudo yum install httpd
$ sudo service httpd start

Install mysql

$ sudo yum install mysql-server
$ sudo service mysqld start
$ mysql_secure_installation

$ sudo yum install mlocate tree sysstat iftop dstat lsof tcpdump wget
$ sudo yum install libcurl-devel httpd-devel apr-devel apr-util-devel
$ sudo yum install mysql-devel

Set up rails

$ sudo gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
$ curl -sSL https://get.rvm.io | bash
$ source ~/.profile
$ rvm install 2.2.2
$ rvm use 2.2.2 --default
$ gem install rails

Vagrant Step by Step

Download VirtualBox for Linux Hosts

Add the following line to your /etc/apt/sources.list:

deb http://download.virtualbox.org/virtualbox/debian trusty contrib

Run command:

$ wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install virtualbox-5.0
$ sudo apt-get install dkms

Rails Websocket Deploy on Apache2 With Passenger

Đầu tiên là tạo dự án rails đã.

$ rails new sample-chat -d mysql

Sau đó config database trong file config/database.yml

Bước tiếp theo đó là cài đặt gem websocket-railstrong Gemfile

gem "websocket-rails"

Chạy lệnh bundle install

Chạy lệnh rails g websocket_rails:install

WebsocketRailsコントローラの作成. Tạo file app/controllers/websocket_chat_controller.rb

# WebsocketRails::BaseControllerを継承
class WebsocketChatController < WebsocketRails::BaseController
  def message_recieve
    # クライアントからのメッセージを取得
    recieve_message = message()
    # websocket_chatイベントで接続しているクライアントにブロードキャスト
    broadcast_message(:websocket_chat, recieve_message)
  end
end

Vim Tips

Move left, up, down, right

Move left : [h]
Move up   : [j]
Move down : [k]
Move right: [l]

Move word at a time

[w]  moves to the start of next word;
[e]  moves to the end of next word;
[b]  moves to beginning of the word;

Rails Tutorial - Step by Step

Init project

Create project

$ rails new project-name -d mysql
$ cd project-name
$ bundle install
$ bundle update

Init bitbucket and heroku

$ git init
$ git remote add origin git@bitbucket.org:<username>/<project-name>

$ heroku create
$ heroku apps:rename <new-name>

Android - Retrofit, OkHttp and GSON

Overview

Retrofit is a type-safe REST client for Android built by Square. The library provides a powerful framework for authenticating and interacting with APIs and sending network requests with OkHttp.

This library makes downloading JSON or XML data from a web API fairly straightforward. Once the data is downloaded then it is parsed into a Plain Old Java Object (POJO) which must be defined for each “resource” in the response.

Setup

Make sure to require Internet permissions in your AndroidManifest.xml file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    <uses-permission android:name="android.permission.INTERNET" />
    .
    .
    .
</manifest>

Add the following to your app/build.gradle file:

dependencies {
  compile 'com.squareup.okhttp:okhttp:2.4.0'
  compile 'com.squareup.retrofit:retrofit:1.9.0'
  compile 'com.google.code.gson:gson:2.3'
}

Note Something When Work About Rails and Gmail

To enable smtp for gmail account via rails

Cần phải bật chế độ less secure

https://www.google.com/settings/u/1/security/lesssecureapps

List files to config

/config/environments/development.rb
#Chú ý line này, cần phải chính xác
config.action_mailer.default_url_options = { :host => '192.168.1.214:3000' }

/config/secrets.yml
# Chú ý line này, có lẽ phải giống với tên đăng ký gmail
admin_name: "Link works Japan"

Edit Gemfile

Thêm gem pg vào production khi deploy lên heroku

Note Something About Android Studio

Create project with Google Plus login

Cần phải sửa lại phiên bản của play-services trong file build.gradle

compile 'com.google.android.gms:play-services:6.1.71'

Thao tác để enable Google Plus API

  1. Go to the Google Developers Console

  2. Click Create Project:

    • In the Project name field, type in a name for your project, such as Quickstart App.
    • In the Project ID field, optionally type in a project ID for your project or use the one that the console has created for you. This ID must be unique world-wide.
  3. Click the Create button and wait for the project to be created. Note: There may be short delay of up to 30 seconds before the project is created. Once the project is created, the name you gave it appears at the top of the left sidebar.

    • In the left sidebar, select APIs & auth (the APIs sub-item is automatically selected).
    • Find the Google+ API and set its status to ON—notice that this action moves Google+ API to the top of the list; you can scroll up to see it.
    • In the sidebar under “APIs & auth”, select Consent screen.
    • Choose an Email Address and specify a Product Name.
    • In the left sidebar under “APIs & auth”, select Credentials.
    • Click Create a new Client ID—the Create Client ID dialog box appears, as shown further below.
    • Select Installed application for the application type.
    • Select Android as the installed application type.
    • Copy and paste the following package name for this quick-start app into the Package name field: com.google.android.gms.plus.sample.quickstart
    • In a terminal, run the the Keytool utility to get the SHA-1 fingerprint of the certificate. For the debug.keystore, the password is android. keytool -exportcert -alias androiddebugkey -keystore -list -v