あいつの日誌β

働きながら旅しています。

Ruby, Heroku and Groonga tutorial 2014

Ruby, Heroku and Groonga tutorial 2014

enviroments

% sw_vers
ProductName:    Mac OS X
ProductVersion: 10.9.4
BuildVersion:   13E28

Ruby

install ruby

% rbenv install 2.1.2
% rbenv global 2.1.2

use plugin

% cd $RBENV_ROOT/plugins
% git clone git://github.com/ianheggie/rbenv-binstubs.git

install bundler

% gem install bundler travis travis-lint
% rbenv rehash 

create project

% mkdir ~/ruby-project && cd $_
% bundle init
% bundle install --path vendor/bundle --binstubs=vendor/bundle/bin

you can see .bundle and Gemfile.lock.

install gems

create Gemfile like this.

% cat Gemfile
ruby '2.1.2'
source "https://rubygems.org"
gem "sinatra"
# gem "rroonga"

group :development, :test do
  gem 'rspec'
  gem 'rack-test'
  gem 'coveralls'  
end
% bundle install

check it.

% bundle show sinatra
~/ruby-project/vendor/bundle/ruby/2.1.0/gems/sinatra-1.4.5

create and run app.rb

create app.rb like this.

require 'sinatra';

get '/' do
  'Hello, World!'
end

run it.

% bundle exec ruby app.rb 
[2014-08-23 11:10:38] INFO  WEBrick 1.3.1
[2014-08-23 11:10:38] INFO  ruby 2.1.2 (2014-05-08) [x86_64-darwin13.0]
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from WEBrick
[2014-08-23 11:10:38] INFO  WEBrick::HTTPServer#start: pid=68339 port=4567

open it.

% open http://localhost:4567

Git

create .gitignore

% cat .gitignore
.bundle
vendor/

first commit

% git init
% git add ./
% git commit -m 'first commit'

Heroku

install heroku-toolbelt

download from here https://toolbelt.heroku.com/

and check it.

% heroku --version
heroku-toolbelt/3.10.1 (x86_64-darwin13.0) ruby/2.1.2

create config.ru

% cat config.ru
require './app'
run Sinatra::Application

check it.

% bundle exec rackup config.ru

create .buildpacks like this

% cat .buildpacks
https://github.com/heroku/heroku-buildpack-ruby

create Procfile like this.

% cat Procfile
web: bundle exec rackup config.ru -p $PORT

commit it.

% git add config.ru .buildpacks Procfile
% git commit -m 'added files for Heroku'

deploy

% heroku create
% heroku config:add BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git
% git push heroku master
% heroku open

RSpec

create .rspec

% echo '--color -f d' > .rspec

create spec/app_spec.rb like this

% cat spec/app_spec.rb
require 'coveralls'
Coveralls.wear!

ENV['RACK_ENV'] = 'test'

require './app'
require 'rspec'
require 'rack/test'

describe 'The HelloWorld App' do
  include Rack::Test::Methods

  def app
    Sinatra::Application
  end

  it "says hello" do
    get '/'
    expect(last_response).to be_ok
    expect(last_response.body).to eq('Hello, World!')
  end
end

test it.

% bundle exec rspec spec

The HelloWorld App
  says hello

Finished in 0.01777 seconds (files took 0.13907 seconds to load)
1 example, 0 failures

commit it

% git add .rspec spec/app_spec.rb
% git commit -m 'added test case.'

Travis CI & Coveralls

sinup Travis CI COVERALLS and add repository.

after that create .travis.yml like this.

% cat .travis.yml
language: ruby
rvm:
  - 2.1.2
script: bundle exec rspec spec
before_install:
  - curl --silent --location https://github.com/groonga/groonga/raw/master/data/travis/setup.sh | sh

create .coveralls.myl

% cat .coveralls.yml
service_name: travis-ci

create README.md like this

[![Build Status](https://travis-ci.org/yourname/ruby-project.svg?branch=master)](https://travis-ci.org/yourname/ruby-project)
[![Coverage Status](https://coveralls.io/repos/yourname/ruby-project/badge.png?branch=master)](https://coveralls.io/r/yourname/ruby-project)

commit it.

% git add .travis.yml .coveralls.yml README.md
% git commit -m 'use travis ci'

push github

% git push origin master 

you can see build passing and coverage 100% badges!!

Groonga

install Groonga

% brew install groonga

modify Gemfile and bundle install

% git diff Gemfile
 ruby '2.1.2'
 source "https://rubygems.org"
 gem "sinatra"
+gem "rroonga"
 
 group :development, :test do
   gem 'rspec'
% bundle install

modify .gitignore like this

% cat .gitignore
.bundle
vendor/
groonga/database*

create groonga dir

% mkdir groonga 
% touch groonga/init.rb

modify .buildpacks like this

% cat .buildpacks
https://github.com/groonga/heroku-buildpack-groonga
https://github.com/heroku/heroku-buildpack-ruby

commit it.

% git add Gemfile Gemfile.lock .gitignore .buildpacks groonga/init.rb
% git commit -m 'use groonga'

modify app.rb like this.

require 'sinatra';
require 'groonga'
require 'pathname'

database_file = Pathname('groonga/database')
database_directory = database_file.dirname
database_directory.mkpath unless database_directory.exist?

if database_file.exist?
  Groonga::Database.open(database_file.to_s)
else
  Groonga::Database.create(:path => database_file.to_s)
end

if not Groonga["Items"]
  Groonga::Schema.create_table("Items", :type => :hash)
  items = Groonga["Items"]
  items.add("hello")

  Groonga::Schema.change_table("Items") do |table|
    table.text("title")
  end
  items["hello"].title = "Hello, World!"
end
  
get '/' do
  Groonga["Items"]["hello"].title
  #'Hello, World!'
end

test it.

% bundle exec rspec spec

commit it.

% git add app.rb
% git commit -m 'replace hello'

deploy heroku and open it

% git push heroku master
% heroku open

Congratulations!! You've accomplished this tutorial!!

Sample Code

https://github.com/okamuuu/sample-ruby-project