Friday, February 22, 2013

Using Phusion Passenger as a Rails Server on Mac OS X 10.7.5

Software used : RVM Passenger 3.0 on Mac OS X 10.7.5

1. rvm use 1.9.3
2. gem install passenger
3. rvm get head
4. rvm reload
5. rvm repair all
6. passenger-install-apache2-module

Thursday, February 21, 2013

Could not find gem 'activerecord-sqlite3-adapter (>= 0) ruby' in the gems available on this machine.

Instead of
gem install activerecord-sqlite3-adapter
run
gem install sqlite3

Archimedes and the Internet

If Archimedes was born in this era, he would say:

Give me a single email account, and a website, and I will move the Internet.

Saturday, February 16, 2013

Playing with URL Helpers in Rails 3.2 Console


 :001 > Rails.application.routes
 => # 
 :002 > Rails.application.routes.url_helpers
 => # 
 :003 > Rails.application.routes.url_helpers.permissions_get_access_token_url
ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
 :004 > Rails.application.routes.url_helpers.permissions_get_access_token_path
 => "/permissions/get_access_token"
 :005 > Rails.application.routes.url_helpers.permissions_get_access_token_path(:host => 'http://localhost')
 => "/permissions/get_access_token"
 :006 > Rails.application.routes.url_helpers.permissions_get_access_token_url(:host => 'http://localhost')
 => "http://http://localhost/permissions/get_access_token"
 :007 > Rails.application.routes.url_helpers.permissions_get_access_token_url(:host => 'localhost')
 => "http://localhost/permissions/get_access_token"
 :008 > Rails.application.routes.url_helpers.permissions_get_access_token_url(:host => 'localhost:3000')
 => "http://localhost:3000/permissions/get_access_token"

 or

  :001 > include ActionDispatch::Routing
  => Object
  :002 > include Rails.application.routes.url_helpers
  => Object
  :003 > permissions_path
  => "/permissions"
  :004 > permissions_url
 ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
  from /Users/bparanj/.r

Reference:
Recognizing URL Helpers

Friday, February 15, 2013

protocol version mismatch (client 7, server 6) tmux

ps aux | grep tmux

Look at the second column for the process id. Then run : kill process-id-for-tmux

Sunday, February 10, 2013

SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

1. Install GCC Installer https://github.com/kennethreitz/osx-gcc-installer
2.

rvm remove 1.9.3
brew install openssl
rvm install 1.9.3 --with-openssl-dir=`brew --prefix openssl`


# Install openssl
echo "Install openssl..."
brew install openssl
brew link openssl
# download cert.pem file for openssl
cd /usr/local/etc/openssl/certs/
sudo curl -O http://curl.haxx.se/ca/cacert.pem
sudo mv cacert.pem cert.pem
cd -
echo "
# cert.pem file for openssl 
export SSL_CERT_FILE=/usr/local/etc/openssl/certs/cert.pem" >> ~/.bash_profile
source ~/.bash_profile

Shell script to turn your laptop to a development machine.

Saturday, February 09, 2013

PayPal Application id for Sand Box

Use APP-80W284485P519543T as the app_id

This is the same for all sandbox users and any PayPal API (Adaptive Payments, Express Checkout etc).

Sunday, February 03, 2013

Convert HAML to ERB


1. rails g install haml2erb
2. Gemfile :
gem 'mixology'
3. bundle
4. Rails console : Haml2Erb.convert('.foo')

This plugin is not perfect. I had to refer HAML documentation to convert some of the lines.

no such file to load -- less Twitter Bootstrap, Rails 3.2.11

1. In Gemfile, for group :assets block, add:
        gem "less-rails"
2. Add to Gemfile (not in any block), add:
         gem 'therubyracer', :require => 'v8'
3. bundle

It will now work.

Thursday, January 31, 2013

rake aborted! can't convert nil into Hash


Solution Source:
Rails 3.1, rake 0.9.2.2, add this code on config/boot.rb
require 'yaml'
YAML::ENGINE.yamler = 'syck'

Monday, January 28, 2013

Useful Tools for Testing Web API


1. RSpec HTTP : https://github.com/c42/rspec-http

response.should be_http_ok
response.should be_http_created
response.should be_http_unprocessable_entity
response.should be_http_im_a_teapot
response.should have_header('Content-Type')
response.should have_header('Content-Type' => 'application/json')
response.should have_header('Content-Type' => /json/)

2. JSON Spec : https://github.com/collectiveidea/json_spec

json_spec defines five new RSpec matchers:

be_json_eql
include_json
have_json_path
have_json_type
have_json_size

3. VCR : https://github.com/myronmarston/vcr

Screencast on VCR : http://avdi.org/devblog/2011/04/11/screencast-taping-api-interactions-with-vcr/

Friday, January 25, 2013

'too many connection resets (due to HTTP session not yet started - IOError) after

This happens when you use VCR with Webmock. There is an open bug on Webmock that is causing this problem. The fix is to either downgrade the Webmock to 1.0 version or use Fakeweb.

Thursday, January 24, 2013

Using pry in test environment


Is it possible to use pry when I am running my tests? Sometimes when I am running specs for legacy code, I need a way to experiment with the existing code.


Answering my own question. Yes, it is possible. Just add binding.pry in the production code, run the specs, pry will stop at the line where you have the binding in the terminal window where you have the test running. You can also use pry-nav to use the ruby debug familiar commands step, next and continue while you debug the code.

Installing Ruby 2.0 on Mac OS 10.8.2 using RVM

1. Make sure you have the latest RVM
     rvm get head
     rvm reload


$ brew install autoconf
$ brew install automake
$ brew install libyaml

$ rvm install ruby-head

Stolen from the blog post instructions How to install Ruby 2.0 (ruby-head) with RVM

Creating a New File in a Folder in Textmate 2

Textmate 2 does not have the quick shortcut to create a file from a selected folder. Here is a workaround:

1. Select the folder where you want the new file.
2. Hit Option+Command+N keys to create a new file.
3. Add the content and save the file. It will get saved in the selected folder.

Tuesday, January 22, 2013

To open a bundled gem, set $EDITOR or $BUNDLER_EDITOR


1. bundle open rails
      gives that error.
2. Add :
      export BUNDLER_EDITOR=mate
    to ~/.bash_profile

Thursday, January 17, 2013

How to push branch to remote

To check-in your new branch you created locally : 1. git checkout -b your_branch 2. git push -u origin your_branch

Friday, January 11, 2013

Wednesday, January 09, 2013

Configuring Moonshine to use Apache XSendFile for Rails 3.2


1. rails plugin install git://github.com/railsmachine/moonshine_xsendfile.git
2. In the Moonshine manifest file:
    configure :xsendfile => {:x_send_file_path => '/absolute/path/to/download/url'}
    recipe :xsendfile
   
    This will work even if you have subdirectories under the /are directory.
3. In production.rb, uncomment the line:
    config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache

4. In your controller:
      send_file '/absolute/path/to/download/url/file.pdf', type: 'application/pdf'

Thursday, January 03, 2013

Rails 3.2, Factory Girl


1. Include :
 
    group :development, :test do
 gem 'factory_girl_rails'
end

in Gemfile

2. bundle install
3. create factories.rb under spec folder
4. Add the factory definitions in it :

FactoryGirl.define do
 factory :user do
   email 'elinora@zepho.com'
   password 'welcome'
   primary_paypal_email 'elinora.price@zepho.com'
 end
 # product factory with a belongs_to association for the user
 factory :product do
   name 'TDD Workbook'
   price 49.99
   user
   thanks_page 'www.zepho.com/thanks.html'
   cancel_page 'www.zepho.com/cancel.html'
   sales_page 'www.zepho.com/sales_page.html'
   download_page 'www.zepho.com/download.html'  
 end
end

In this definition I have factory for user has_many :products relationship.
5. Create a support directory and include controller_macros.rb in it:
module ControllerMacros
 def login_user
   @request.env["devise.mapping"] = Devise.mappings[:user]
   user = FactoryGirl.create(:user)
   # user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
   sign_in user
 end
end
6. In spec_helper.rb add the devise helpers:
RSpec.configure do |config|
 ...
 config.include Devise::TestHelpers, :type => :controller
 config.include ControllerMacros, :type => :controller
 ...
end
7. In your controller spec, now you can do this:
 
   context 'User is logged in' do
    before do
      login_user
    end

    it 'should render index page' do
      get :index

      response.should be_success
    end
  end