Ruby gems

48
Ruby gems December 9, 2010 [email protected] MeetUP @ Balabit

description

Cool ruby gems to developing be easier and faster

Transcript of Ruby gems

Page 1: Ruby gems

Ruby gems

December 9, [email protected]

MeetUP @ Balabit

Page 3: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

ActiveRecord

Mysql

Ruby Git

RMagick

Do what I mean

Jabber

Page 4: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

ActiveRecord

Mysql

Ruby Git

RMagick

Do what I mean

Jabber

Page 5: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

First steps

sudo apt-get install rubygems

Page 6: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

First steps

sudo apt-get install rubygems

gem install git

Page 7: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

First steps

sudo apt-get install rubygems

gem install gitrequire “rubygems”require “git”

repo = Git.open “/work/scb”

repo.log.between ‘3.0.0’, ‘3.1.0’

Page 9: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

#title .left = @title

#content .author.strong %span{ :style => “float: left” } Nucc

.body.mobile = @content.body

- if @content.footer? .footer.mobile = @content.footer

- plain <span>Copyright</span>

<div id=”title”> <div class=”left”> <%= @title %> </div></div>

<div id=”content”> <div class=”author strong”> <span style=”float: left”> Nucc </span> </div> <div class=”body mobile”> <%= @content.body %> </div></div>

HAML

Page 11: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

$blue: #3bbfce$margin: 16px

.content-navigation border-color: $blue color: darken($blue, 9%)

.border padding: $margin / 2 margin: $margin / 2 border-color: $blue

.content-navigation { border-color: #3bbfce; color: #2b9eab;}

.border { padding: 8px; margin: 8px; border-color: #3bbfce;}

HAMLSASS

Page 12: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

@mixin table-base th text-align: center font-weight: bold td, th padding: 2px

@mixin left($dist) float: left margin-left: $dist

#data @include left(10px) @include table-base

#data { float: left; margin-left: 10px;}#data th { text-align: center; font-weight: bold;}#data td, #data th { padding: 2px;}

HAMLSASS

Page 13: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

HPricot

http://hpricot.com

Page 14: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

HPricotrequire ‘rubygems’require ‘hpricot’

html = Hpricot("<p id=’test_id’>A simple <span class=’bold’>test</span> string.</p>")

(html/”p”).inner_html=> "A simple <span class=\"bold\">test string.</b>"

(html/:p/:span).first.inner_html

=> “test”

(html/”#test_id”).inner_html

(html/”span.bold”).remove

Page 15: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

HPricotrequire ‘rubygems’require ‘hpricot’require ‘open-uri’

html = open("http://iosflashvideo.fw.hu/") { |f| Hpricot(f, :xhtml_strict => true) }

html.search("//a[@href='/donate']")

html.search("html > body > p img")

xpath

css

html.at("#header").xpath #=> "//div[@id='header']"

css to xpath

html.at("//span").css_path #=> "p > span:nth(0)"

xpath to css

Page 16: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

Hpricot

(html/"span.bold").set(:class => 'weight')

(html/”span.bold”).remove

jQuery

(html/:span).each do |span| span.attributes[‘class’] = “weight”end

Looping

Page 18: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

RSPEC

Behaviour Driven Development

Page 19: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

RSPEC

Behaviour Driven Development

Describe a network interface!

It should have a host address.

It should have a network address.

It should have a gateway address.

The gateway and the host address must be in the same network.

Page 20: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

RSPEC

Behaviour Driven Development

Describe a network interface!

It should have a host address.

It should have a network address.

It should have a gateway address.

The gateway and the host address must be in the same network.

describe Interface do

it “should have a host address” do end

it “should have a network address” do end

it “should have a gateway address” do end

it “gateway and host address...” do end

end

Page 21: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

RSPECdescribe Interface do

before :all do @interface = Interface.new end

before :each do @interface.network = “10.30.0.0” end

it “should have a host address” do @interface.should respond_to :host end

it “should be valid” do # Rails model validation @interface.should be_valid end

after :each do #do something end

end

Page 22: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

RSPECdescribe Interface do

describe :network

subject { @interface.network }

context “when netmask is 255.255.0.0” do before { @interface.netmask = “255.255.0.0” } it { should =~ /0.0$/} end

context “when netmask is 255.0.0.0” do before { @interface.netmask = “255.0.0.0” } it { should =~ /0.0.0$/} end

context “when gateway and host are not in the same network” do # network address is 10.30.0.0/255.255.0.0 currently before { @interface.gateway = “10.100.255.254” }

it { should =~ /^10.100/ } specify { @interface.should not.be_valid } end endend

Page 23: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

RSPEC

Interface network when netmask is 255.255.0.0 should =~ /0.0$/ when netmask is 255.0.0.0 should =~ /0.0.0$/ when gateway and host are not in the same network should =~ /^10.100/ should not be valid

expected: /0.0$/,         got: "10.30.0.1" (using =~)    Diff:    @@ -1,2 +1,2 @@    -/0.0$/    +"10.30.0.1"

Page 25: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

Cucumber

Precondition Given

Action When

Postcondition Then

Page 26: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

Cucumber

Feature: Network Connection Managing network connections on intraweb

Scenario: Interface settings

Given an Interface and its address is 10.30.0.34 and its network is 10.30.0.0

When the gateway is 10.100.255.254

Then its not valid

Page 27: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

Cucumber

Feature: Network Connection Managing network connections on intraweb

Scenario: Interface settings

Given an Interface and its address is <IP> and its network is <NETWORK>

When the gateway is <GATEWAY>

Then its <RESULT>

Examples: | IP | NETWORK | GATEWAY | RESULT | | 10.30.0.34 | 10.30.0.0 | 10.30.255.254 | VALID | | 10.100.30.1 | 10.100.0.0 | 10.30.255.254 | NOT VALID |

Page 28: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

Cucumber

Given an Interface and its address is <IP> and its network is <NETWORK>

Given /^an Interface$/ do @interface = Interface.newend

Given /address is (.*)$/ do |value| @interface.address = valueend

Given /network is (.*)$/ do |value| @interface.network = valueend

Page 29: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

Cucumber

When the gateway is <GATEWAY>

Then its <RESULT>

When /^the gateway is (.*)$/ do |gw| @interface.gateway = gwend

Then /its (.*)$/ do |result| @interface.validate.should == (result == “VALID”)end

Page 30: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

Cucumber

Jellemző: Összeadás Azért, hogy elkerüljem a buta hibákat amit diszkalkúliásként elkövethetek, két szám összegét szeretném kiszámoltatni.

Forgatókönyv vázlat: Két szám összeadása Amennyiben beütök a számológépbe egy <be_1>-est És beütök a számológépbe egy <be_2>-est Majd megnyomom az <gomb> gombot Akkor eredményül <ki>-t kell kapnom

Példák: | be_1 | be_2 | gomb | ki | | 20 | 30 | add | 50 | | 2 | 5 | add | 7 | | 0 | 40 | add | 40 |

by gbence

Page 31: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

Cucumber

Before do  @calc = Calculator.newend

Ha /^beütök a számológépbe egy (\d+)\-(?:es|as|ös|ás)t$/ do |n|  @calc.push n.to_iend

Majd /^megnyomom az (\w+) gombot$/ do |op|  @result = @calc.send opend

Akkor /^eredményül (.*)\-(?:e|a|ö|á|)t kell kapnom$/ do |result|  @result.should == result.to_fend

Page 32: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

CucumberOH HAI: STUFFING

MISHUN: CUCUMBR I CAN HAZ IN TEH BEGINNIN 3 CUCUMBRZ WEN I EAT 2 CUCUMBRZ DEN I HAS 2 CUCUMBERZ IN MAH BELLY AN IN TEH END 1 CUCUMBRZ KTHXBAI

ICANHAZ /^IN TEH BEGINNIN (\d+) CUCUMBRZ$/ do |n|  @basket = Basket.new(n.to_i)end

WEN /^I EAT (\d+) CUCUMBRZ$/ do |n|  @belly = Belly.new  @belly.eat(@basket.take(n.to_i))end

Page 34: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

OmniAuth

Facebook

Twitter

Google

LinkedIn

Foursquare

Meetup

OpenID

Page 35: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

OmniAuth

Rails.application.config.middleware.use OmniAuth::Builder do provider :twitter, 'CONSUMER_KEY', 'CONSUMER_SECRET'end

/config/initializers/omniauth.rb

Page 36: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

OmniAuth

Rails.application.config.middleware.use OmniAuth::Builder do provider :twitter, 'CONSUMER_KEY', 'CONSUMER_SECRET'end

/config/initializers/omniauth.rb

/auth/twitter

User

get redirect

/auth/failed/auth/twitter/callback

valid failed

Page 37: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

OmniAuth

class AuthController < Application

def callback

auth_hash = request.env['omniauth.auth']

# auth_hash: # { # ‘uid’ => “12345”

# ‘provider’ => “twitter” # ‘user_info’ => {

# ‘name’ => “User name” # ‘nickname’ => “nick”, # ...

# } # }

end

...

auth_controller.rb

Page 39: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

class Test

  def initialize    @array = Array.new  end

  def add(anObject)    @array.push(anObject)  end

end

Page 40: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

Ruby C

#include "ruby.h"

static VALUE t_init(VALUE self){ VALUE array;

array = rb_ary_new(); rb_iv_set(self, "@array", array); return self;}

static VALUE t_add(VALUE self, VALUE anObject){ VALUE array;

array = rb_iv_get(self, "@array"); rb_ary_push(array, anObject); return array;}...

main.cclass Test

  def initialize    @array = Array.new  end

  def add(anObject)    @array.push(anObject)  end

end

Page 41: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

Ruby C

/* from the previous slide */static VALUE t_init(VALUE self);static VALUE t_add(VALUE self, VALUE anObject);/* end */

VALUE cTest;

void Init_Test() { cTest = rb_define_class("Test", rb_cObject); rb_define_method(cTest, "initialize", t_init, 0); rb_define_method(cTest, "add", t_add, 1);}

main.cclass Test

  def initialize    @array = Array.new  end

  def add(anObject)    @array.push(anObject)  end

end

Page 42: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

Ruby C

require 'mkmf'create_makefile("Test")

extconf.rb

nucc@rubybox ~ $ ruby extconf.rbnucc@rubybox ~ $ makenucc@rubybox ~ $ make install

Page 43: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

Ruby C

require “Test”

test = Test.newtest.add("Balabit Meetup")p test

=> #<Test:0x100156548 @array=["Balabit Meetup"]>

my_test.rb

Page 45: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

require 'ftplib'

ftp = FTP.open('ftp.netlab.co.jp')ftp.loginftp.chdir('pub/lang/ruby')puts ftp.dirftp.quit

?

Page 46: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

require 'ftplib'

ftp = FTP.open('ftp.netlab.co.jp')ftp.loginftp.chdir('pub/lang/ruby')puts ftp.dirftp.quit

?RubyPyth

on

Page 47: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

Questions?

Page 48: Ruby gems

Balabit Meetup - December 9, 2010 [email protected]

Thank you!