Code Galaxy

A blogging framework for hackers.

Rpush Generating Certificate for Ios

To Create .pem file for apple push notification using rpush .

Steps:

1.Create a CSR Using Key Chain Access 2.Create a P12 Using Key Chain Access using private key. 3.APNS App ID and certificate.

  • This gives you three files:
    • The CSR
    • The private key as a p12 file (pushkey_production.p12)
    • The SSL certificate, aps_development.cer or the .pem file(pushkey_production.pem)

Convert the .cer file into a .pem file:

 openssl x509 -in aps_development.cer -inform der -out pushkey_production.pem

Convert the private key’s .p12 file into a .pem file:

 openssl pkcs12 -nocerts -out private_keyy.p12 -in pushkey_private_production.p12

Enter Import Password:

 MAC verified OK
 Enter PEM pass phrase: 
 Verifying - Enter PEM pass phrase:

You first need to enter the passphrase for the .p12 file so that openssl can read it. Then you need to enter a new passphrase that will be used to encrypt the PEM file. Again for this tutorial I used “pushchat” as the PEM passphrase. You should choose something more secure. Note: if you don’t enter a PEM passphrase, openssl will not give an error message but the generated .pem file will not have the private key in it.

Finally, combine the certificate and key into a single .pem file:

 cat pushkey_production.pem pushkey_private_production.pem >rpush_prod.pem

CORS on Rails and Nginx

When using rails api for web applications like angular or node js the cors has to be handled on the both nginx and api server. Please refer the following code snippet to get this right.

Handle Cors in Nginx

Add the following lines to nginx conf file sudo vim /etc/nginx/sites-available/default

  location / {
    proxy_pass http://app;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
  if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    add_header 'Content-Length' 0;
    return 204;
  }
  if ($request_method = 'POST') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-    Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

 }
 if ($request_method = 'GET') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
 }
 }

Handle cors in Rails api

Add the following preflight check on the base controller.

 class Api::BaseController < ApplicationController

   before_filter :cors_preflight_check
   after_filter :cors_set_access_control_headers

   def cors_set_access_control_headers
     #headers['Access-Control-Allow-Origin'] = '*'
     headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PATCH, DELETE'
     headers['Access-Control-Request-Method'] = '*'
     headers['Access-Control-Max-Age'] = "1728000"
   end

  def cors_preflight_check
    if request.method.to_s.downcase == "options"
     #headers['Access-Control-Allow-Origin'] = '*'
     headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PATCH, DELETE'
     headers['Access-Control-Request-Method'] = '*'
     headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version, Content-Type'
     headers['Access-Control-Max-Age'] = '1728000'
     render :json => {}
   end
 end

Vundle the Bundler of Vim

I use vim like text editor since after my college. I allways appreciate vim and I really don’t want change to another one.

How I manage my plugin around the time

Vim is really powerfull with this plugins. You need really quick use it. It’s really important to manage it. All around the time, I change how I manage it with new technique inside vim or develop by other developper.

Handmade management

In start of my vim usage, I manage my plugin by copying file in different directory. I put in versionning my .vimrc and .vim. But this technique can be hard to maintain in time. The installation is not really simple and know if you need update or not is hard too. So the evolution of plugins become rare.

Vimball management

In vim evolution, the vimball management system was implemented. With this vimball, you can more easily install plugin and know which version you use. So you can improve your plugins management. But to know if a new version is available, you need go to vimscript.org and check each of your plugins. The update become rare too.

Git-submodule management

With github, all plugins become a git repository. We can check all new commit on our favorite plugin. I start to link all plugin in a new submodule and link file in good directory. The update was really simple and my plugins can be up to date. But the installation become really hard.

Janus management

Yehuda Katz and Carl Lerche start a new project call janus. This project’s goal is an easy plugin management by rake task. In this Rakefile, you can see the list of your plugin and with some rake task you can install it and update it. This project was really great. It start with a bunch of plugins pre-configure and good if you start using vim and you are not aware with what plugin can be good to you. But in time, the management can be a little tricky with version of janus. So it’s a really good project to start vim.

Pathogen management, the first vim plugin to manage you plugins

To closed of janus’s released, Tim Pope ( a very important vim plugin developper ) release Pathogen. This plugin help to manage your vim plugin directly in your vimrc file. I don’t really use it. But a lot of people say it’s a really good plugin. There are a lot of fork of janus trying to use pathogen directly.

Vundle management

Now, someone told me about a very good plugin to manage his vim’s plugin, Vundle. As Pathogen, the plugin management is do directly in your vimrc. His usage is really simple and well designed. If you are a ruby guy and use already Bundler, you can easily understand how works Vundle and how use it. Vundle is directly inspired of this project.

Using Vundle

Install Vundle

To install vundle, you just need clone the project’s repository in your .vim directory and add 2 lignes in your vimrc file

mkdir ~/.vim/bundle
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle

Vundle makes it very easy to manage Vim plugins. You just need to specify a few rows along the lines of Bundle “user/plugin” in your .vimrc and then issue :BundleInstall to start the installation.

You can also run :BundleUpdate and :BundleClean

New .vimrc

Both NERDTree and Syntastic are quite slow. I could probably manage without them but what would be the fun in that?

set nocompatible
filetype off

set rtp+=~/.vim/bundle/vundle/
call vundle#rc()

" Let Vundle manage Vundle
Bundle 'gmarik/vundle'

" My Bundles
Bundle 'tpope/vim-sensible'
Bundle 'tpope/vim-surround'
Bundle 'tpope/vim-fugitive'
Bundle 'tpope/vim-rails'
Bundle 'tpope/vim-rake'
Bundle 'nanotech/jellybeans.vim'
Bundle 'Lokaltog/vim-powerline'
Bundle 'scrooloose/syntastic'
Bundle 'scrooloose/nerdtree'
Bundle 'kien/ctrlp.vim'
Bundle 'rking/ag.vim'
Bundle 'kana/vim-textobj-user'
Bundle 'nelstrom/vim-textobj-rubyblock'
Bundle 'slim-template/vim-slim'

filetype plugin indent on

let mapleader=","

color jellybeans

set cursorline
set expandtab
set modelines=0
set shiftwidth=2
set clipboard=unnamed
set synmaxcol=128
set ttyscroll=10
set encoding=utf-8
set tabstop=2
set nowrap
set number
set expandtab
set nowritebackup
set noswapfile
set nobackup
set hlsearch
set ignorecase
set smartcase

" Automatic formatting
autocmd BufWritePre *.rb :%s/\s\+$//e
autocmd BufWritePre *.go :%s/\s\+$//e
autocmd BufWritePre *.haml :%s/\s\+$//e
autocmd BufWritePre *.html :%s/\s\+$//e
autocmd BufWritePre *.scss :%s/\s\+$//e
autocmd BufWritePre *.slim :%s/\s\+$//e

au BufNewFile * set noeol
au BufRead,BufNewFile *.go set filetype=go

" No show command
autocmd VimEnter * set nosc

" Quick ESC
imap jj <ESC>

" Jump to the next row on long lines
map <Down> gj
map <Up>   gk
nnoremap j gj
nnoremap k gk

" format the entire file
nmap <leader>fef ggVG=

" Open new buffers
nmap <leader>s<left>   :leftabove  vnew<cr>
nmap <leader>s<right>  :rightbelow vnew<cr>
nmap <leader>s<up>     :leftabove  new<cr>
nmap <leader>s<down>   :rightbelow new<cr>

" Tab between buffers
noremap <tab> <c-w><c-w>

" Switch between last two buffers
nnoremap <leader><leader> <C-^>

" Resize buffers
if bufwinnr(1)
  nmap Ä <C-W><<C-W><
  nmap Ö <C-W>><C-W>>
  nmap ö <C-W>-<C-W>-
  nmap ä <C-W>+<C-W>+
endif

" NERDTree
nmap <leader>n :NERDTreeToggle<CR>
let NERDTreeHighlightCursorline=1
let NERDTreeIgnore = ['tmp', '.yardoc', 'pkg']

" Syntastic
let g:syntastic_mode_map = { 'mode': 'passive' }
let g:syntastic_ruby_exec = '~/.rvm/rubies/ruby-2.0.0-p0/bin/ruby'

" CtrlP
nnoremap <silent> t :CtrlP<cr>
let g:ctrlp_working_path_mode = 2
let g:ctrlp_by_filename = 1
let g:ctrlp_max_files = 600
let g:ctrlp_max_depth = 5

" Go programming
set rtp+=/usr/local/Cellar/go/1.0.3/misc/vim

" Quit with :Q
command -nargs=0 Quit :qa!

Ubuntu fix

In ubuntu you may not get the proper color pallet on the screen . You may have to export the terminal color please refer this link: (http://unix.stackexchange.com/questions/1045/getting-256-colors-to-work-in-tmux)

And a small .gvimrc

I like to keep the main part of the config in .vimrc in order to have as few differences between the GUI and CLI versions of MacVim.

  " MacVim GUI mode
if has("gui_macvim")
  set guifont=Monaco:h13
  set guioptions=aAce
  set fuoptions=maxvert,maxhorz
  set noballooneval

  " resize current buffer by +/- 5
  nnoremap <M-Right> :vertical resize +5<CR>
  nnoremap <M-Left>  :vertical resize -5<CR>
  nnoremap <M-Up>    :resize -5<CR>
  nnoremap <M-Down>  :resize +5<CR>

  " Command+Option+Right for next
  map <D-M-Right> :tabn<CR>
  " Command+Option+Left for previous
  map <D-M-Left> :tabp<CR>

  " Automatically resize splits
  " when resizing MacVim window
  autocmd VimResized * wincmd =
endif

My vimrc

If you want see my own vimrc your can see it on one of my github repository. With the Vundle help, it’s really simple to understand what plugin I really use. All is in only one file.

Installing Ruby on Rails in Mac/ubuntu Os

To get your Ruby on Rails development environment set up get working on this right away using the guide i have provided below, broken down according to operating system. Depending on your operating system and other system specific factors, additional steps may be required. If issues occur during the installation process, please refer to the following link, which may contain more up-to-date information:

If you encounter any problems, don’t give up, ask questions in the discussion forum, search the web for answers, etc. If you can get your development environment set up and functioning properly, you will have taken the first big step towards becoming a web application developer!

Installing Ruby and Rails on Linux

Install Dependencies

Install build essentials by opening up a terminal window and typing the following command:

  sudo apt-get install build-essential

Next, install curl. In the terminal window type:

  sudo apt-get install curl

Then, install Git. In the terminal window type:

  sudo apt-get install git-core

Installing RVM

RVM stands for Ruby Version Manager and allows you to install multiple versions of Ruby and switch between them easily. Even if you are only going to use one version, it’s probably the simplest way to install Ruby.

To install RMV, in your terminal window run the command:

  curl -L https://get.rvm.io | bash -s stable --ruby

In the terminal window, type the following in order to make RVM known in your bash sessions:

  echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc

Finally, restart your bash session by closing and then reopening your terminal window

Using RVM

You can get a list of Ruby versions by running the following command in your terminal window:

  rvm list known

The version of Ruby we will use is 2.1.0 and you can install it by using the following command in your terminal window:

  rvm install ruby-2.1.0

Set this version as the default using the following command in your terminal window:

  rvm use 2.1.0

You can then make sure Ruby is installed by running the following command in your terminal window:

  ruby -v

Installing Rails

The latest version of Rails will be installed by issuing the following command.  From your terminal window type:

  gem install rails

you can verify that it was installed correctly by running the following command in your terminal window:

      rails -v

(This will check the current installed version of Rails, which should be 4.0 or later)

Installing Ruby and Rails on OSX

First you must install Xcode with command line utilities

In order to do this, you must locate Xcode in the Apple app store and install it from there.

Afterward, open Xcode, go to Preferences and click on the Downloads tab.

Click install next to Command Line Tools.

To install Git, go to http://git-scm.com and download the Mac OSX installer.

Installing RVM

RVM stands for Ruby Version Manager and allows you to install multiple versions of Ruby and switch between them easily. Even if you are only going to use one version it’s probably the simplest way to install Ruby.

Install RMV by running the following command in your terminal window:

  curl -L https://get.rvm.io | bash -s stable --ruby

Next, cd to your home directory (using: cd ~) and open the .bashrc file in a text editor using the command:

  pico .bashrc

Copy the following into this file and save it:

  PATH=$PATH:$HOME/.rvm/bin

Finally, restart your bash session by closing and reopening your terminal window.

Using RVM

You can get a list of ruby versions by running the following command in your terminal window:

  rvm list known

The version of Ruby we will use is 2.1.0 and you can install it by running the following command in your terminal window:

  rvm install ruby-2.1.0

Set this version as the default by running the following command in your terminal window:

  rvm use 2.1.0

Then, make sure Ruby is installed by running the following command in your terminal window:

  ruby -v

Installing Rails

The latest version of Rails will be installed by issuing the following command. From your terminal window type:

  gem install rails -v 4.0

You can verify that it was installed correctly by running the following command in your terminal window:

  rails -v

(This will check the current installed version of Rails, which should be 4.0 or later)

Installing Ruby and Rails on Windows

Installing Ruby

Ruby can be installed simply by downloading the RubyInstaller executable: Ruby 2.0.0-p247

Installing Rails

Rails can be installed by typing the following into your terminal window:

  gem install rails

You can verify that it was installed correctly by running the following command in your terminal window:

  rails -v

(This will check the current installed version of Rails, which should be 4.0 or later)