Ruby gems

  • 2,025 views
Uploaded on

Cool ruby gems to developing be easier and faster

Cool ruby gems to developing be easier and faster

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
2,025
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
6
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Ruby gemsMeetUP @ Balabit December 9, 2010 nucc@balabit.com
  • 2. Do Active what I Record meanMysql RMagick Ruby Jabber Git
  • 3. Do Active what I Record meanMysql RMagick Ruby Jabber Git
  • 4. First steps sudo apt-get install rubygems
  • 5. First steps sudo apt-get install rubygems gem install git
  • 6. First steps sudo apt-get install rubygems gem install git require “rubygems” require “git” repo = Git.open “/work/scb” repo.log.between ‘3.0.0’, ‘3.1.0’
  • 7. http://haml-lang.com
  • 8. HAML#title <div id=”title”> .left = @title <div class=”left”> <%= @title %>#content </div> .author.strong </div> %span{ :style => “float: left” } Nucc <div id=”content”> <div class=”author strong”> .body.mobile <span style=”float: left”> = @content.body Nucc </span> - if @content.footer? </div> .footer.mobile <div class=”body mobile”> = @content.footer <%= @content.body %> </div>- plain </div> <span>Copyright</span>
  • 9. http://sass-lang.com
  • 10. SASSHAML$blue: #3bbfce .content-navigation {$margin: 16px border-color: #3bbfce; color: #2b9eab;.content-navigation } border-color: $blue .border { color: darken($blue, 9%) padding: 8px; margin: 8px;.border border-color: #3bbfce; padding: $margin / 2 } margin: $margin / 2 border-color: $blue
  • 11. SASSHAML@mixin table-base #data { th float: left; text-align: center margin-left: 10px; font-weight: bold } td, th #data th { padding: 2px text-align: center; font-weight: bold;@mixin left($dist) } float: left #data td, #data th { margin-left: $dist padding: 2px; }#data @include left(10px) @include table-base
  • 12. HPricothttp://hpricot.com
  • 13. 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
  • 14. HPricot require ‘rubygems’ require ‘hpricot’ require ‘open-uri’ html = open("http://iosflashvideo.fw.hu/") { |f| Hpricot(f, :xhtml_strict => true) }xpath html.search("//a[@href=/donate]")css html.search("html > body > p img")css to xpath html.at("#header").xpath #=> "//div[@id=header]"xpath to css html.at("//span").css_path #=> "p > span:nth(0)"
  • 15. HpricotjQuery (html/"span.bold").set(:class => weight) (html/”span.bold”).removeLooping (html/:span).each do |span| span.attributes[‘class’] = “weight” end
  • 16. http://rspec.info
  • 17. RSPECBehaviour Driven Development
  • 18. RSPECBehaviour Driven DevelopmentDescribe 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.
  • 19. RSPEC describe Interface do it “should have a host address” do endBehaviour Driven Development it “should have a network address” do end it “should have a gateway address” do endDescribe a network interface! it “gateway and host address...” doIt should have a host address. endIt should have a network address. endIt should have a gateway address.The gateway and the host address must be in the same network.
  • 20. RSPEC describe 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
  • 21. RSPEC describe 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 end end
  • 22. 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"
  • 23. http://cukes.info
  • 24. Cucumber Precondition Given Action When Postcondition Then
  • 25. 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
  • 26. 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 |
  • 27. Cucumber Given an Interface and its address is <IP> and its network is <NETWORK> Given /^an Interface$/ do @interface = Interface.new end Given /address is (.*)$/ do |value| @interface.address = value end Given /network is (.*)$/ do |value| @interface.network = value end
  • 28. Cucumber When the gateway is <GATEWAY> Then its <RESULT> When /^the gateway is (.*)$/ do |gw| @interface.gateway = gw end Then /its (.*)$/ do |result| @interface.validate.should == (result == “VALID”) end
  • 29. 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
  • 30. CucumberBefore do  @calc = Calculator.newendHa /^beütök a számológépbe egy (d+)-(?:es|as|ös|ás)t$/ do |n|  @calc.push n.to_iendMajd /^megnyomom az (w+) gombot$/ do |op|  @result = @calc.send opendAkkor /^eredményül (.*)-(?:e|a|ö|á|)t kell kapnom$/ do |result|  @result.should == result.to_fend
  • 31. 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 KTHXBAIICANHAZ /^IN TEH BEGINNIN (d+) CUCUMBRZ$/ do |n|  @basket = Basket.new(n.to_i)endWEN /^I EAT (d+) CUCUMBRZ$/ do |n|  @belly = Belly.new  @belly.eat(@basket.take(n.to_i))end
  • 32. OmniAuthhttp://github.com/intridea/omniauth
  • 33. OmniAuth Facebook Twitter Google LinkedIn Foursquare Meetup OpenID
  • 34. OmniAuth/config/initializers/omniauth.rbRails.application.config.middleware.use OmniAuth::Builder do provider :twitter, CONSUMER_KEY, CONSUMER_SECRETend
  • 35. OmniAuth/config/initializers/omniauth.rbRails.application.config.middleware.use OmniAuth::Builder do provider :twitter, CONSUMER_KEY, CONSUMER_SECRETend get /auth/twitter redirect User valid failed /auth/twitter/callback /auth/failed
  • 36. OmniAuth auth_controller.rb 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 ...
  • 37. Ruby C APIhttp://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html
  • 38. class Test  def initialize    @array = Array.new  end  def add(anObject)    @array.push(anObject)  endend
  • 39. Ruby Cmain.c#include "ruby.h" class Teststatic VALUE t_init(VALUE self)   def initialize{     @array = Array.new VALUE array;   end array = rb_ary_new();   def add(anObject) rb_iv_set(self, "@array", array);     @array.push(anObject) return self;   end} endstatic VALUE t_add(VALUE self, VALUE anObject){ VALUE array; array = rb_iv_get(self, "@array"); rb_ary_push(array, anObject); return array;}...
  • 40. Ruby Cmain.c/* from the previous slide */ class Teststatic VALUE t_init(VALUE self);static VALUE t_add(VALUE self, VALUE anObject);   def initialize/* end */     @array = Array.new   endVALUE cTest;   def add(anObject)     @array.push(anObject)void Init_Test()   end{ cTest = rb_define_class("Test", rb_cObject); end rb_define_method(cTest, "initialize", t_init, 0); rb_define_method(cTest, "add", t_add, 1);}
  • 41. Ruby C extconf.rb require mkmf create_makefile("Test") nucc@rubybox ~ $ ruby extconf.rb nucc@rubybox ~ $ make nucc@rubybox ~ $ make install
  • 42. Ruby C my_test.rb require “Test” test = Test.new test.add("Balabit Meetup") p test => #<Test:0x100156548 @array=["Balabit Meetup"]>
  • 43. require ftplib ?ftp = FTP.open(ftp.netlab.co.jp)ftp.loginftp.chdir(pub/lang/ruby)puts ftp.dirftp.quit
  • 44. on yth ubyPR require ftplib ? ftp = FTP.open(ftp.netlab.co.jp) ftp.login ftp.chdir(pub/lang/ruby) puts ftp.dir ftp.quit
  • 45. Questions?
  • 46. Thank you!