Skip to content
This repository
Browse code

Initial support for Whois::Response and Whois parsers (references #281).

  • Loading branch information...
commit ce867f8fade8e00db0d8e185cf4c42520c6b7342 1 parent 9958aa6
Simone Carletti authored
27 lib/whois.rb
@@ -18,6 +18,7 @@
18 18 require 'whois/errors'
19 19 require 'whois/client'
20 20 require 'whois/server'
  21 +require 'whois/response'
21 22 require 'whois/whois'
22 23
23 24
@@ -26,8 +27,30 @@ module Whois
26 27 NAME = 'Whois'
27 28 GEM = 'whois'
28 29 AUTHORS = ['Simone Carletti <weppos@weppos.net>']
29   -
30   - def self.whois(qstring)
  30 +
  31 +
  32 + def self.query(qstring)
31 33 Client.new.query(qstring)
32 34 end
  35 +
  36 + def self.whois(qstring)
  37 + query(qstring)
  38 + end
  39 +
  40 + def self.available?(qstring)
  41 + query(qstring).available?
  42 + rescue ParserNotFound => e
  43 + $stderr.puts "This method is not available for this kind of object.\n" +
  44 + "Use Whois.query('#{qstring}') instead."
  45 + nil
  46 + end
  47 +
  48 + def self.registered?(qstring)
  49 + query(qstring).registered?
  50 + rescue ParserNotFound => e
  51 + $stderr.puts "This method is not available for this kind of object.\n" +
  52 + "Use Whois.query('#{qstring}') instead."
  53 + nil
  54 + end
  55 +
33 56 end
19 lib/whois/errors.rb
@@ -25,16 +25,14 @@ class Error < StandardError
25 25 class ServerError < Error
26 26 end
27 27
28   -
29   - # Definition not found
  28 + # Server Definition not found
30 29
31 30 # Raised when the class hasn't been able to select a valid server
32 31 # probably because definitions are outdated.
33 32 class ServerNotFound < ServerError
34 33 end
35 34
36   -
37   - # Definition found
  35 + # Server Definition found
38 36
39 37 class InterfaceNotSupported < ServerError
40 38 end
@@ -50,8 +48,7 @@ class NoInterfaceError < InterfaceNotSupported
50 48 class WebInterfaceError < InterfaceNotSupported
51 49 end
52 50
53   -
54   - # Known object, Definition unavailable
  51 + # Server Known object, Definition unavailable
55 52
56 53 # Raised when we know about a specific functionality
57 54 # but this functionality has not been implemented yet.
@@ -66,5 +63,15 @@ class ServerNotSupported < ServerError
66 63 # Raised when unknown AS numer of IP network. (\x06)
67 64 class AllocationUnknown < ServerError
68 65 end
  66 +
  67 +
  68 + # Generic Parser exception class.
  69 + class ParserError < Error
  70 + end
  71 +
  72 + # Raised when the class hasn't been able to load a valid parser
  73 + # according to current settings.
  74 + class ParserNotFound < ParserError
  75 + end
69 76
70 77 end
131 lib/whois/response.rb
... ... @@ -0,0 +1,131 @@
  1 +#
  2 +# = Ruby Whois
  3 +#
  4 +# An intelligent pure Ruby WHOIS client.
  5 +#
  6 +#
  7 +# Category:: Net
  8 +# Package:: Whois
  9 +# Author:: Simone Carletti <weppos@weppos.net>
  10 +# License:: MIT License
  11 +#
  12 +#--
  13 +#
  14 +#++
  15 +
  16 +
  17 +require 'whois/response/parsers/base'
  18 +
  19 +
  20 +module Whois
  21 +
  22 + class Response
  23 + attr_reader :server
  24 + attr_reader :content
  25 +
  26 + def initialize(content, server = nil)
  27 + @content = content
  28 + @server = server
  29 + end
  30 +
  31 + def to_s
  32 + @content.to_s
  33 + end
  34 +
  35 + def inspect
  36 + @content.inspect
  37 + end
  38 +
  39 + # Invokes <tt>match</tt> on response <tt>@content</tt>
  40 + # and returns the <tt>MatchData</tt> or <tt>nil</tt>.
  41 + def match(pattern)
  42 + @content.match(pattern)
  43 + end
  44 +
  45 + # Returns true if the <tt>object</tt> is the same object,
  46 + # or is a string and has the same content.
  47 + def ==(other)
  48 + (other.equal?(self)) ||
  49 + (other.instance_of?(String) && other == self.to_s)
  50 + end
  51 +
  52 + # Delegates to ==.
  53 + def eql?(other)
  54 + self == other
  55 + end
  56 +
  57 +
  58 + # Invokes <tt>match</tt> and returns <tt>true</tt> if <tt>pattern</tt>
  59 + # matches <tt>@content</tt>, <tt>false</tt> otherwise.
  60 + def match?(pattern)
  61 + !@content.match(pattern).nil?
  62 + end
  63 +
  64 + # Invokes <tt>match</tt> and returns the first useful match,
  65 + # nil otherwise.
  66 + #
  67 + # "My car is blue".i_m_feeling_lucky(/^My car is (\w+)$/)
  68 + # # => "blue"
  69 + # "My car is blue".i_m_feeling_lucky(/^My bike is (\w+)$/)
  70 + # # => nil
  71 + #
  72 + def i_m_feeling_lucky(pattern, &block)
  73 + if matches = match(pattern)
  74 + m = matches[1]
  75 + m = yield(m) if block_given?
  76 + m
  77 + end
  78 + end
  79 + alias :imfl :i_m_feeling_lucky
  80 +
  81 +
  82 + # Lazy-loads and returns current response parser.
  83 + def parser
  84 + @parser ||= self.class.parser_klass(@server).new(self)
  85 + end
  86 +
  87 + protected
  88 +
  89 + # Delegates all method calls to the internal parser.
  90 + def method_missing(method, *args, &block)
  91 + if Parsers::Base.allowed_methods.include?(method)
  92 + parser.send(method, *args, &block)
  93 + else
  94 + super
  95 + end
  96 + end
  97 +
  98 +
  99 + def self.parser_klass(server)
  100 + raise ParserError,
  101 + "Unable to select a parser. " +
  102 + "The server for this response is either nil or invalid." if server.nil? || server.host.nil?
  103 +
  104 + file = "whois/response/parsers/#{server.host}.rb"
  105 + require file
  106 +
  107 + name = host_to_parser(server)
  108 + Parsers.const_get(name)
  109 +
  110 + rescue LoadError
  111 + raise ParserNotFound,
  112 + "Unable to find a parser for the server `#{server.host}'"
  113 + end
  114 +
  115 + def self.host_to_parser(server)
  116 + server.host.to_s.
  117 + gsub(/\./, '_').
  118 + gsub(/(?:^|_)(.)/) { $1.upcase }
  119 + end
  120 +
  121 + # available?
  122 + # registered?
  123 + # status
  124 +
  125 + # created_on
  126 + # updated_on
  127 + # expires_on
  128 +
  129 + end
  130 +
  131 +end
58 lib/whois/response/parsers/base.rb
... ... @@ -0,0 +1,58 @@
  1 +#
  2 +# = Ruby Whois
  3 +#
  4 +# An intelligent pure Ruby WHOIS client.
  5 +#
  6 +#
  7 +# Category:: Net
  8 +# Package:: Whois
  9 +# Author:: Simone Carletti <weppos@weppos.net>
  10 +# License:: MIT License
  11 +#
  12 +#--
  13 +#
  14 +#++
  15 +
  16 +
  17 +require 'time'
  18 +
  19 +
  20 +module Whois
  21 + class Response
  22 + module Parsers
  23 +
  24 + #
  25 + # = Base Response Parser
  26 + #
  27 + # This class is intended to be the base abstract class for all
  28 + # server-specific parser implementations.
  29 + #
  30 + class Base
  31 +
  32 + @@allowed_methods = [
  33 + :registered?, :available?, :status,
  34 + :created_on, :updated_on, :expires_on,
  35 + ]
  36 +
  37 + def self.allowed_methods
  38 + @@allowed_methods
  39 + end
  40 +
  41 + attr_reader :response
  42 +
  43 +
  44 + def initialize(response)
  45 + @response = response
  46 + end
  47 +
  48 + allowed_methods.each do |method|
  49 + define_method(method) do
  50 + raise NotImplementedError, "You should overwrite this method."
  51 + end
  52 + end
  53 +
  54 + end
  55 +
  56 + end
  57 + end
  58 +end
58 lib/whois/response/parsers/whois.nic.it.rb
... ... @@ -0,0 +1,58 @@
  1 +#
  2 +# = Ruby Whois
  3 +#
  4 +# An intelligent pure Ruby WHOIS client.
  5 +#
  6 +#
  7 +# Category:: Net
  8 +# Package:: Whois
  9 +# Author:: Simone Carletti <weppos@weppos.net>
  10 +# License:: MIT License
  11 +#
  12 +#--
  13 +#
  14 +#++
  15 +
  16 +
  17 +require 'whois/response/parsers/base'
  18 +
  19 +
  20 +module Whois
  21 + class Response
  22 + module Parsers
  23 +
  24 + class WhoisNicIt < Base
  25 +
  26 + def status
  27 + response.i_m_feeling_lucky(/^Status:\s+(.+)$/) { |m| m.downcase.to_sym }
  28 + end
  29 +
  30 + def available?
  31 + response.match?(/^Status:\s+AVAILABLE$/)
  32 + end
  33 +
  34 + def registered?
  35 + !available?
  36 + end
  37 +
  38 +
  39 + def created_on
  40 + return unless registered?
  41 + response.i_m_feeling_lucky(/^Created:\s+(.+)$/) { |m| Time.parse(m) }
  42 + end
  43 +
  44 + def updated_on
  45 + return unless registered?
  46 + response.i_m_feeling_lucky(/^Last Update:\s+(.+)$/) { |m| Time.parse(m) }
  47 + end
  48 +
  49 + def expires_on
  50 + return unless registered?
  51 + response.i_m_feeling_lucky(/^Expire Date:\s+(.+)$/) { |m| Time.parse(m) }
  52 + end
  53 +
  54 + end
  55 +
  56 + end
  57 + end
  58 +end
3  lib/whois/server/adapters/base.rb
@@ -36,7 +36,8 @@ def initialize(type, allocation, host, options = {})
36 36 end
37 37
38 38 def query(qstring)
39   - request(qstring)
  39 + response = request(qstring)
  40 + Response.new(response, self)
40 41 end
41 42
42 43 def request(qstring)
4 test/adapters/afilias_test.rb
@@ -13,7 +13,7 @@ def test_query
13 13 response = "No match for DOMAIN.FOO."
14 14 expected = response
15 15 @server.expects(:ask_the_socket).with("domain.foo", "whois.afilias-grs.info", 43).returns(response)
16   - assert_equal expected, @server.query("domain.foo")
  16 + assert_equal expected, @server.query("domain.foo").to_s
17 17 end
18 18
19 19 def test_query_with_referral
@@ -22,7 +22,7 @@ def test_query_with_referral
22 22 expected = referral + "\n" + response
23 23 @server.expects(:ask_the_socket).with("domain.foo", "whois.afilias-grs.info", 43).returns(referral)
24 24 @server.expects(:ask_the_socket).with("domain.foo", "whois.belizenic.bz", 43).returns(response)
25   - assert_equal expected, @server.query("domain.foo")
  25 + assert_equal expected, @server.query("domain.foo").to_s
26 26 end
27 27
28 28 end
4 test/adapters/pir_test.rb
@@ -13,7 +13,7 @@ def test_query
13 13 response = "No match for DOMAIN.FOO."
14 14 expected = response
15 15 @server.expects(:ask_the_socket).with("FULL domain.foo", "whois.publicinterestregistry.net", 43).returns(response)
16   - assert_equal expected, @server.query("domain.foo")
  16 + assert_equal expected, @server.query("domain.foo").to_s
17 17 end
18 18
19 19 def test_query_with_referral
@@ -22,7 +22,7 @@ def test_query_with_referral
22 22 expected = referral + "\n" + response
23 23 @server.expects(:ask_the_socket).with("FULL domain.foo", "whois.publicinterestregistry.net", 43).returns(referral)
24 24 @server.expects(:ask_the_socket).with("domain.foo", "whois.iana.org", 43).returns(response)
25   - assert_equal expected, @server.query("domain.foo")
  25 + assert_equal expected, @server.query("domain.foo").to_s
26 26 end
27 27
28 28 end
10 test/adapters/standard_test.rb
@@ -10,14 +10,16 @@ def setup
10 10 end
11 11
12 12 def test_query
13   - @server.expects(:ask_the_socket).with("domain.foo", "whois.foo", 43)
14   - @server.query("domain.foo")
  13 + @server.expects(:ask_the_socket).with("domain.foo", "whois.foo", 43).returns("Whois Response")
  14 + response = @server.query("domain.foo")
  15 + assert_equal response, "Whois Response"
15 16 end
16 17
17 18 def test_query_with_port
18 19 @server = @klass.new(:tld, ".foo", "whois.foo", { :port => 20 })
19   - @server.expects(:ask_the_socket).with("domain.foo", "whois.foo", 20)
20   - @server.query("domain.foo")
  20 + @server.expects(:ask_the_socket).with("domain.foo", "whois.foo", 20).returns("Whois Response")
  21 + response = @server.query("domain.foo")
  22 + assert_equal response, "Whois Response"
21 23 end
22 24
23 25 end
4 test/adapters/verisign_test.rb
@@ -13,7 +13,7 @@ def test_query
13 13 response = "No match for DOMAIN.FOO."
14 14 expected = response
15 15 @server.expects(:ask_the_socket).with("=domain.foo", "whois.foo", 43).returns(response)
16   - assert_equal expected, @server.query("domain.foo")
  16 + assert_equal expected, @server.query("domain.foo").to_s
17 17 end
18 18
19 19 def test_query_with_referral
@@ -22,7 +22,7 @@ def test_query_with_referral
22 22 expected = referral + "\n" + response
23 23 @server.expects(:ask_the_socket).with("=domain.foo", "whois.foo", 43).returns(referral)
24 24 @server.expects(:ask_the_socket).with("domain.foo", "whois.tucows.com", 43).returns(response)
25   - assert_equal expected, @server.query("domain.foo")
  25 + assert_equal expected, @server.query("domain.foo").to_s
26 26 end
27 27
28 28 end
4 test/client_test.rb
@@ -77,8 +77,8 @@ def query(*args)
77 77
78 78 def test_query_with_domain
79 79 response = @client.query("weppos.it")
80   - assert_match /Domain:\s+weppos\.it/, response
81   - assert_match /Created:/, response
  80 + assert response.match?(/Domain:\s+weppos\.it/)
  81 + assert response.match?(/Created:/)
82 82 end
83 83
84 84 end
62 test/parsers/whois_nic_it_test.rb
... ... @@ -0,0 +1,62 @@
  1 +require 'test_helper'
  2 +require 'whois/response/parsers/whois.nic.it.rb'
  3 +
  4 +class WhoisNicItTest < Test::Unit::TestCase
  5 +
  6 + TESTCASE_PATH = File.expand_path(File.dirname(__FILE__) + '/../testcases/responses/it')
  7 +
  8 + def setup
  9 + @klass = Whois::Response::Parsers::WhoisNicIt
  10 + @server = Whois::Server.factory(:tld, ".it", "whois.nic.it")
  11 + @response = Whois::Response
  12 + end
  13 +
  14 +
  15 + def test_available?
  16 + assert @klass.new(load_response('/available.txt')).available?
  17 + assert !@klass.new(load_response('/registered.txt')).available?
  18 + end
  19 +
  20 + def test_registered?
  21 + assert @klass.new(load_response('/registered.txt')).registered?
  22 + assert !@klass.new(load_response('/available.txt')).registered?
  23 + end
  24 +
  25 + def test_status
  26 + assert_equal :active,
  27 + @klass.new(load_response('/status_active.txt')).status
  28 + assert_equal :available,
  29 + @klass.new(load_response('/status_available.txt')).status
  30 + end
  31 +
  32 +
  33 + def test_created_on
  34 + assert_equal Time.parse("1999-12-10 00:00:00"),
  35 + @klass.new(load_response('/registered.txt')).created_on
  36 + assert_equal nil,
  37 + @klass.new(load_response('/available.txt')).created_on
  38 + end
  39 +
  40 + def test_updated_on
  41 + assert_equal Time.parse("2008-11-27 16:47:22"),
  42 + @klass.new(load_response('/registered.txt')).updated_on
  43 + assert_equal nil,
  44 + @klass.new(load_response('/available.txt')).updated_on
  45 + end
  46 +
  47 + def test_expires_on
  48 + assert_equal Time.parse("2009-11-27"),
  49 + @klass.new(load_response('/registered.txt')).expires_on
  50 + assert_equal nil,
  51 + @klass.new(load_response('/available.txt')).expires_on
  52 + end
  53 +
  54 +
  55 +
  56 + protected
  57 +
  58 + def load_response(path)
  59 + @response.new(File.read(TESTCASE_PATH + path), @server)
  60 + end
  61 +
  62 +end
85 test/response_test.rb
... ... @@ -0,0 +1,85 @@
  1 +require 'test_helper'
  2 +
  3 +class ResponseTest < Test::Unit::TestCase
  4 +
  5 + def setup
  6 + @klass = Whois::Response
  7 + @server = Whois::Server.factory(:tld, ".foo", "whois.foo")
  8 + @content = "This is a response for domain.foo."
  9 + @response = @klass.new(@content, @server)
  10 + end
  11 +
  12 + def test_initialize
  13 + response = @klass.new(@content)
  14 + assert_instance_of @klass, response
  15 + assert_equal @content, response.content
  16 + assert_equal nil, response.server
  17 + end
  18 +
  19 + def test_initialize_with_server
  20 + response = @klass.new(@content, @server)
  21 + assert_instance_of @klass, response
  22 + assert_equal @content, response.content
  23 + assert_equal @server, response.server
  24 + end
  25 +
  26 + def test_to_s
  27 + assert_equal @content, @response.to_s
  28 + end
  29 +
  30 + def test_inspect
  31 + assert_equal @content.inspect, @response.inspect
  32 + end
  33 +
  34 + def test_match
  35 + # Force .to_a otherwise Match will be compared as Object instance
  36 + # and the test will fail because they actually are different instances.
  37 + assert_equal @content.match(/domain\.foo/).to_a, @response.match(/domain\.foo/).to_a
  38 + assert_equal @content.match(/google/), @response.match(/google/)
  39 + end
  40 +
  41 + def test_equality
  42 + assert_equal @response, @content
  43 + assert_not_equal @content, @response
  44 + assert @response.eql?(@content)
  45 + assert !@content.eql?(@response)
  46 + end
  47 +
  48 + def test_equality_with_self
  49 + assert_equal @response, @response
  50 + assert @response.eql?(@response)
  51 + end
  52 +
  53 +
  54 + def test_match?
  55 + assert @response.match?(/domain\.foo/)
  56 + assert !@response.match?(/google/)
  57 + end
  58 +
  59 + def test_i_m_feeling_lucky
  60 + assert_equal "domain.foo", @response.i_m_feeling_lucky(/for (.*)\.$/)
  61 + assert_equal nil, @response.i_m_feeling_lucky(/^invalid (.*)$/)
  62 + end
  63 +
  64 +
  65 + def test_parser
  66 + @server = Whois::Server.factory(:tld, ".it", "whois.nic.it")
  67 + @response = @klass.new("", @server)
  68 + assert_instance_of Whois::Response::Parsers::WhoisNicIt,
  69 + @response.parser
  70 + end
  71 +
  72 + def test_parser_should_raise_with_missing_parser
  73 + @server = Whois::Server.factory(:tld, ".it", "invalid.nic.it")
  74 + @response = @klass.new("", @server)
  75 + error = assert_raise(Whois::ParserNotFound) { @response.parser }
  76 + assert_match /Unable to find a parser/, error.message
  77 + end
  78 +
  79 + def test_parser_should_raise_with_missing_server
  80 + @response = @klass.new("")
  81 + error = assert_raise(Whois::ParserError) { @response.parser }
  82 + assert_match /Unable to select a parser/, error.message
  83 + end
  84 +
  85 +end
2  test/testcases/responses/it/available.txt
... ... @@ -0,0 +1,2 @@
  1 +Domain: foobarbaz.it
  2 +Status: AVAILABLE
1  test/testcases/responses/it.txt → test/testcases/responses/it/registered.txt
@@ -50,3 +50,4 @@ Nameservers
50 50 ns4.google.com
51 51 ns2.google.com
52 52 ns3.google.com
  53 +
53 test/testcases/responses/it/status_active.txt
... ... @@ -0,0 +1,53 @@
  1 +
  2 +
  3 +*********************************************************************
  4 +* Please note that the following result could be a subgroup of *
  5 +* the data contained in the database. *
  6 +* *
  7 +* Additional information can be visualized at: *
  8 +* http://www.nic.it/cgi-bin/Whois/whois.cgi *
  9 +*********************************************************************
  10 +
  11 +Domain: google.it
  12 +Status: ACTIVE
  13 +Created: 1999-12-10 00:00:00
  14 +Last Update: 2008-11-27 16:47:22
  15 +Expire Date: 2009-11-27
  16 +
  17 +Registrant
  18 + Name: Google Ireland Holdings
  19 + ContactID: GOOG175-ITNIC
  20 + Address: 30 Herbert Street
  21 + Dublin
  22 + 2
  23 + IE
  24 + IE
  25 + Created: 2008-11-27 16:47:22
  26 + Last Update: 2008-11-27 16:47:22
  27 +
  28 +Admin Contact
  29 + Name: Tsao Tu
  30 + ContactID: TT4277-ITNIC
  31 + Organization: Tu Tsao
  32 + Address: 30 Herbert Street
  33 + Dublin
  34 + 2
  35 + IE
  36 + IE
  37 + Created: 2008-11-27 16:47:22
  38 + Last Update: 2008-11-27 16:47:22
  39 +
  40 +Technical Contacts
  41 + Name: Technical Services
  42 + ContactID: TS7016-ITNIC
  43 +
  44 +Registrar
  45 + Organization: Register.it s.p.a.
  46 + Name: REGISTER-MNT
  47 +
  48 +Nameservers
  49 + ns1.google.com
  50 + ns4.google.com
  51 + ns2.google.com
  52 + ns3.google.com
  53 +
2  test/testcases/responses/it/status_available.txt
... ... @@ -0,0 +1,2 @@
  1 +Domain: foobarbaz.it
  2 +Status: AVAILABLE
24 test/whois_test.rb
@@ -2,9 +2,31 @@
2 2
3 3 class WhoisTest < Test::Unit::TestCase
4 4
  5 + def setup
  6 + @server = Whois::Server.factory(:tld, ".it", "whois.nic.it")
  7 + @response = Whois::Response.new("", @server)
  8 + end
  9 +
  10 + def test_query
  11 + Whois::Client.any_instance.expects(:query).with("foo.com")
  12 + Whois.query("foo.com")
  13 + end
  14 +
5 15 def test_whois
6 16 Whois::Client.any_instance.expects(:query).with("foo.com")
7 17 Whois.whois("foo.com")
8 18 end
9   -
  19 +
  20 + def test_available_question
  21 + @response.expects(:available?).returns(true)
  22 + Whois::Client.any_instance.expects(:query).with("foo.com").returns(@response)
  23 + assert Whois.available?("foo.com")
  24 + end
  25 +
  26 + def test_registered_question
  27 + @response.expects(:registered?).returns(true)
  28 + Whois::Client.any_instance.expects(:query).with("foo.com").returns(@response)
  29 + assert Whois.registered?("foo.com")
  30 + end
  31 +
10 32 end
150 utils/bm_delegation_vs_inheritance.rb
... ... @@ -0,0 +1,150 @@
  1 +$:.unshift(File.dirname(__FILE__) + '/../lib')
  2 +require 'benchmark'
  3 +require 'delegate'
  4 +
  5 +class Foo < DelegateClass(String)
  6 + attr_reader :optional
  7 + def initialize(mandatory, optional = nil)
  8 + super(String.new(mandatory))
  9 + @optional = optional
  10 + end
  11 +end
  12 +
  13 +class Bar < String
  14 + attr_reader :optional
  15 + def initialize(mandatory, optional = nil)
  16 + super(mandatory)
  17 + @optional = optional
  18 + end
  19 +end
  20 +
  21 +class Baz
  22 + attr_reader :optional
  23 + def initialize(mandatory, optional = nil)
  24 + @string = mandatory.to_s
  25 + @optional = optional
  26 + end
  27 +
  28 + def match(*args)
  29 + @string.match(*args)
  30 + end
  31 +
  32 + def gsub(*args)
  33 + @string.gsub(*args)
  34 + end
  35 +
  36 + def gsub!(*args)
  37 + @string.gsub!(*args)
  38 + end
  39 +end
  40 +
  41 +class Option < Struct.new(:qui, :quo, :qua)
  42 + def to_s
  43 + "-" * 100
  44 + end
  45 +end
  46 +
  47 +
  48 +TIMES = 100_000
  49 +CONTENT = <<-LOREM
  50 +Lorem ipsum dolor sit amet, consectetur adipisicing elit,
  51 +sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  52 +Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
  53 +nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
  54 +reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
  55 +Excepteur sint occaecat cupidatat non proident,
  56 +sunt in culpa qui officia deserunt mollit anim id est laborum.
  57 +LOREM
  58 +
  59 +Benchmark.bmbm do |x|
  60 + x.report("inheritance =~") do
  61 + TIMES.times do
  62 + s = Foo.new(CONTENT, Option.new)
  63 + s =~ /Duis/
  64 + end
  65 + end
  66 + x.report("delegation =~") do
  67 + TIMES.times do
  68 + s = Bar.new(CONTENT, Option.new)
  69 + s =~ /Duis/
  70 + end
  71 + end
  72 + x.report("composition =~") do
  73 + # TIMES.times do
  74 + # s = Baz.new(CONTENT, Option.new)
  75 + # s =~ /Duis/
  76 + # end
  77 + end
  78 + x.report("inheritance match") do
  79 + TIMES.times do
  80 + s = Foo.new(CONTENT, Option.new)
  81 + s.match(/Status:/)
  82 + end
  83 + end
  84 + x.report("delegation match") do
  85 + TIMES.times do
  86 + s = Bar.new(CONTENT, Option.new)
  87 + s.match(/Status:/)
  88 + end
  89 + end
  90 + x.report("composition match") do
  91 + TIMES.times do
  92 + s = Baz.new(CONTENT, Option.new)
  93 + s.match(/Status:/)
  94 + end
  95 + end
  96 + x.report("inheritance gsub") do
  97 + TIMES.times do
  98 + s = Foo.new(CONTENT, Option.new)
  99 + s.gsub(/ /, '_')
  100 + end
  101 + end
  102 + x.report("delegation gsub") do
  103 + TIMES.times do
  104 + s = Bar.new(CONTENT, Option.new)
  105 + s.gsub(/ /, '_')
  106 + end
  107 + end
  108 + x.report("composition gsub") do
  109 + TIMES.times do
  110 + s = Baz.new(CONTENT, Option.new)
  111 + s.gsub(/ /, '_')
  112 + end
  113 + end
  114 + x.report("inheritance gsub!") do
  115 + TIMES.times do
  116 + s = Foo.new(CONTENT, Option.new)
  117 + s.gsub!(/ /, '_')
  118 + end
  119 + end
  120 + x.report("delegation gsub!") do
  121 + TIMES.times do
  122 + s = Bar.new(CONTENT, Option.new)
  123 + s.gsub!(/ /, '_')
  124 + end
  125 + end
  126 + x.report("composition gsub!") do
  127 + TIMES.times do
  128 + s = Baz.new(CONTENT, Option.new)
  129 + s.gsub!(/ /, '_')
  130 + end
  131 + end
  132 + x.report("inheritance option") do
  133 + TIMES.times do
  134 + s = Foo.new(CONTENT, Option.new)
  135 + s.optional.to_s
  136 + end
  137 + end
  138 + x.report("delegation option") do
  139 + TIMES.times do
  140 + s = Bar.new(CONTENT, Option.new)
  141 + s.optional.to_s
  142 + end
  143 + end
  144 + x.report("composition option") do
  145 + TIMES.times do
  146 + s = Baz.new(CONTENT, Option.new)
  147 + s.optional.to_s
  148 + end
  149 + end
  150 +end

0 comments on commit ce867f8

Please sign in to comment.
Something went wrong with that request. Please try again.