hmachine 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -2
- data/Gemfile +6 -4
- data/Gemfile.lock +51 -0
- data/README.md +123 -9
- data/Rakefile +12 -3
- data/bin/hmachine +99 -0
- data/hmachine.gemspec +132 -0
- data/lib/hmachine.rb +121 -12
- data/lib/hmachine/microformat.rb +39 -20
- data/lib/hmachine/microformat/adr.rb +22 -0
- data/lib/hmachine/microformat/geo.rb +48 -0
- data/lib/hmachine/microformat/hcard.rb +169 -11
- data/lib/hmachine/microformat/rellicense.rb +20 -0
- data/lib/hmachine/microformat/reltag.rb +38 -0
- data/lib/hmachine/microformat/votelinks.rb +42 -0
- data/lib/hmachine/microformat/xfn.rb +54 -0
- data/lib/hmachine/microformat/xmdp.rb +14 -0
- data/lib/hmachine/microformat/xoxo.rb +69 -0
- data/lib/hmachine/pattern.rb +26 -0
- data/lib/hmachine/pattern/abbr.rb +21 -0
- data/lib/hmachine/pattern/datetime.rb +75 -0
- data/lib/hmachine/pattern/typevalue.rb +32 -0
- data/lib/hmachine/pattern/url.rb +32 -0
- data/lib/hmachine/pattern/valueclass.rb +51 -0
- data/lib/hmachine/posh.rb +3 -0
- data/lib/hmachine/posh/anchor.rb +40 -0
- data/lib/hmachine/posh/base.rb +204 -0
- data/lib/hmachine/posh/definition_list.rb +41 -0
- data/test/fixtures/huffduffer.html +466 -0
- data/test/fixtures/likeorhate.html +48 -0
- data/test/fixtures/rel_license.html +4 -0
- data/test/fixtures/test-fixture/hcard/hcard1.html +147 -0
- data/test/fixtures/test-fixture/hcard/hcard11.html +123 -0
- data/test/fixtures/test-fixture/hcard/hcard12.html +178 -0
- data/test/fixtures/test-fixture/hcard/hcard17.html +165 -0
- data/test/fixtures/test-fixture/hcard/hcard2.html +264 -0
- data/test/fixtures/test-fixture/hcard/hcard3.html +144 -0
- data/test/fixtures/test-fixture/hcard/hcard4.html +117 -0
- data/test/fixtures/test-fixture/hcard/hcard5.html +119 -0
- data/test/fixtures/test-fixture/hcard/hcard6.html +188 -0
- data/test/fixtures/test-fixture/hcard/hcard7.html +188 -0
- data/test/fixtures/test-fixture/hcard/hcard8.html +130 -0
- data/test/fixtures/test-fixture/hcard/hcard9.html +111 -0
- data/test/fixtures/test-fixture/hcard/hcard99.html +215 -0
- data/test/fixtures/test-fixture/value-class-date-time/value-dt-test-YYYY-MM-DD--HH-MM.html +9 -0
- data/test/fixtures/test-fixture/value-class-date-time/value-dt-test-abbr-YYYY-MM-DD--HH-MM.html +4 -0
- data/test/fixtures/xfn.html +198 -0
- data/test/fixtures/xmdp.html +32 -0
- data/test/fixtures/xoxo.html +51 -0
- data/test/hmachine_test.rb +122 -6
- data/test/microformat/adr_test.rb +47 -0
- data/test/microformat/geo_test.rb +66 -0
- data/test/microformat/hcard_test.rb +487 -20
- data/test/microformat/rellicense_test.rb +36 -0
- data/test/microformat/reltag_test.rb +61 -0
- data/test/microformat/votelinks_test.rb +44 -0
- data/test/microformat/xfn_test.rb +28 -0
- data/test/microformat/xmdp_test.rb +16 -0
- data/test/microformat/xoxo_test.rb +51 -0
- data/test/microformat_test.rb +12 -34
- data/test/pattern/date_time_test.rb +55 -0
- data/test/pattern/value_class_test.rb +33 -0
- data/test/pattern_test.rb +132 -0
- data/test/posh/anchor_test.rb +41 -0
- data/test/posh/base_test.rb +150 -0
- data/test/posh/definition_list_test.rb +38 -0
- data/test/test_helper.rb +24 -6
- metadata +93 -15
- data/lib/hmachine/microformat/base.rb +0 -17
| @@ -0,0 +1,36 @@ | |
| 1 | 
            +
            require File.join(File.dirname(__FILE__), '..', 'test_helper')
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class RelLicenseTest < Test::Unit::TestCase
         | 
| 4 | 
            +
              setup do
         | 
| 5 | 
            +
                @html = get_fixture('rel_license.html')
         | 
| 6 | 
            +
                @doc = Nokogiri::HTML.parse(@html)
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
              
         | 
| 9 | 
            +
              describe 'Class' do    
         | 
| 10 | 
            +
                should 'find itself in a document' do
         | 
| 11 | 
            +
                  assert_equal 2, HMachine::Microformat::RelLicense.find_in(@doc).count
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
              
         | 
| 15 | 
            +
              describe 'Instance' do
         | 
| 16 | 
            +
                setup do
         | 
| 17 | 
            +
                  @klass = HMachine::Microformat::RelLicense
         | 
| 18 | 
            +
                  @node = @klass.find_in(@doc).first
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
                
         | 
| 21 | 
            +
                should 'have a license' do
         | 
| 22 | 
            +
                  test = @klass.new(@node)
         | 
| 23 | 
            +
                  assert_respond_to test, :license
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
                
         | 
| 26 | 
            +
                should 'parse the license out of the node' do
         | 
| 27 | 
            +
                  test = @klass.new(@node)
         | 
| 28 | 
            +
                  assert_equal 'http://creativecommons.org/licenses/by/2.0/', test.license
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
                
         | 
| 31 | 
            +
                should 'be the license when converted to a string' do
         | 
| 32 | 
            +
                  test = @klass.new(@node)
         | 
| 33 | 
            +
                  assert_equal test.license, test.to_s
         | 
| 34 | 
            +
                end
         | 
| 35 | 
            +
              end
         | 
| 36 | 
            +
            end
         | 
| @@ -0,0 +1,61 @@ | |
| 1 | 
            +
            require File.join(File.dirname(__FILE__), '..', 'test_helper')
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class RelTagTest < Test::Unit::TestCase
         | 
| 4 | 
            +
              setup do
         | 
| 5 | 
            +
                @html = get_fixture('huffduffer.html')
         | 
| 6 | 
            +
                @doc = Nokogiri::HTML.parse(@html)
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
              
         | 
| 9 | 
            +
              describe 'Class' do    
         | 
| 10 | 
            +
                should 'find itself in a document' do
         | 
| 11 | 
            +
                  first_entry = @doc.css('.hfeed > .hentry').first
         | 
| 12 | 
            +
                  assert_equal 8, HMachine::Microformat::RelTag.find_in(first_entry).count
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
                
         | 
| 15 | 
            +
                should 'parse itself out of a document' do
         | 
| 16 | 
            +
                  first_entry = @doc.css('.hfeed > .hentry').first
         | 
| 17 | 
            +
                  assert_equal HMachine::Microformat::RelTag, HMachine::Microformat::RelTag.parse(first_entry).first.class
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
              
         | 
| 21 | 
            +
              describe 'Instance' do
         | 
| 22 | 
            +
                setup do
         | 
| 23 | 
            +
                  @klass = HMachine::Microformat::RelTag
         | 
| 24 | 
            +
                  first_entry = @doc.css('.hfeed > .hentry').first
         | 
| 25 | 
            +
                  @node = @klass.find_in(first_entry).first
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
                
         | 
| 28 | 
            +
                should 'have a name that is the tag' do
         | 
| 29 | 
            +
                  test = @klass.new(@node)
         | 
| 30 | 
            +
                  assert_equal test.tag.keys.first, test.name
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
                
         | 
| 33 | 
            +
                should 'convert to a string easily' do
         | 
| 34 | 
            +
                  test = @klass.new(@node)
         | 
| 35 | 
            +
                  assert_equal test.name, "#{test}"
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
                
         | 
| 38 | 
            +
                should 'store the url for the tag' do
         | 
| 39 | 
            +
                  test = @klass.new(@node)
         | 
| 40 | 
            +
                  assert_equal test.tag.values.first, test.url
         | 
| 41 | 
            +
                end
         | 
| 42 | 
            +
                
         | 
| 43 | 
            +
                should 'hash should be a representation of the tag' do
         | 
| 44 | 
            +
                  test = @klass.new(@node)
         | 
| 45 | 
            +
                  assert_equal test.tag, test.to_h
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
              end
         | 
| 48 | 
            +
              
         | 
| 49 | 
            +
              describe 'Parsing' do
         | 
| 50 | 
            +
                setup do
         | 
| 51 | 
            +
                  @klass = HMachine::Microformat::RelTag
         | 
| 52 | 
            +
                end
         | 
| 53 | 
            +
                
         | 
| 54 | 
            +
                should "get a tag" do
         | 
| 55 | 
            +
                  first_entry = @doc.css('.hfeed > .hentry').first
         | 
| 56 | 
            +
                  test = @klass.new(@klass.find_in(first_entry).first)
         | 
| 57 | 
            +
                  assert_equal "event%3Aspeaker%3Dtim+o%27reilly", test.tag.keys.first
         | 
| 58 | 
            +
                end
         | 
| 59 | 
            +
              end  
         | 
| 60 | 
            +
              
         | 
| 61 | 
            +
            end
         | 
| @@ -0,0 +1,44 @@ | |
| 1 | 
            +
            require File.join(File.dirname(__FILE__), '..', 'test_helper')
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class VoteLinksTest < Test::Unit::TestCase
         | 
| 4 | 
            +
              setup do
         | 
| 5 | 
            +
                @html = get_fixture('likeorhate.html')
         | 
| 6 | 
            +
                @doc = Nokogiri::HTML.parse(@html)
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
              
         | 
| 9 | 
            +
              describe 'Class' do    
         | 
| 10 | 
            +
                should 'find itself in a document' do
         | 
| 11 | 
            +
                  assert_equal 3, HMachine::Microformat::VoteLinks.find_in(@doc).count
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
              
         | 
| 15 | 
            +
              describe 'Instance' do
         | 
| 16 | 
            +
                setup do
         | 
| 17 | 
            +
                  @klass = HMachine::Microformat::VoteLinks
         | 
| 18 | 
            +
                  @node = @klass.find_in(@doc).first
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
                
         | 
| 21 | 
            +
                should 'be a specific type of vote' do
         | 
| 22 | 
            +
                  test = @klass.new(@node)
         | 
| 23 | 
            +
                  assert_equal 'vote-for', test.type
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
                
         | 
| 26 | 
            +
                should 'check vote type' do
         | 
| 27 | 
            +
                  test = @klass.new(@node)
         | 
| 28 | 
            +
                  assert test.for?, "VoteLink is type of #{test.type}"
         | 
| 29 | 
            +
                  assert !test.against?
         | 
| 30 | 
            +
                  assert !test.abstain?
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
                
         | 
| 33 | 
            +
                should 'store the url of the vote' do
         | 
| 34 | 
            +
                  test = @klass.new(@node)
         | 
| 35 | 
            +
                  assert_equal 'http://likeorhate.com/rate/?id=1191956&rate=like', test.url
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
                
         | 
| 38 | 
            +
                should "Additional human-readable commentary can be added using the existing 'title' attribute" do
         | 
| 39 | 
            +
                  test = @klass.new(@node)
         | 
| 40 | 
            +
                  assert_equal 'Do you like it? Click now and vote!', test.title
         | 
| 41 | 
            +
                end
         | 
| 42 | 
            +
                
         | 
| 43 | 
            +
              end
         | 
| 44 | 
            +
            end
         | 
| @@ -0,0 +1,28 @@ | |
| 1 | 
            +
            require File.join(File.dirname(__FILE__), '..', 'test_helper')
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class XFNTest < Test::Unit::TestCase
         | 
| 4 | 
            +
              setup do
         | 
| 5 | 
            +
                html = get_fixture('xfn.html')
         | 
| 6 | 
            +
                @doc = Nokogiri::HTML.parse(html)
         | 
| 7 | 
            +
                @klass = HMachine::Microformat::XFN
         | 
| 8 | 
            +
              end
         | 
| 9 | 
            +
              
         | 
| 10 | 
            +
              should 'search the document for relationships' do
         | 
| 11 | 
            +
                contact = @klass.parse_first(@doc)
         | 
| 12 | 
            +
                assert_respond_to contact, :rel
         | 
| 13 | 
            +
                contact.rel.include? 'contact'
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
              
         | 
| 16 | 
            +
              should 'define boolean methods' do
         | 
| 17 | 
            +
                contact = @klass.parse_first(@doc)
         | 
| 18 | 
            +
                assert_respond_to contact, :friendship?
         | 
| 19 | 
            +
                assert_respond_to contact, :me?
         | 
| 20 | 
            +
                assert contact.friendship?
         | 
| 21 | 
            +
              end
         | 
| 22 | 
            +
              
         | 
| 23 | 
            +
              should 'be able to parse just "me" relationships' do
         | 
| 24 | 
            +
                me = @klass.parse_me(@doc)
         | 
| 25 | 
            +
                assert me.first.me?
         | 
| 26 | 
            +
              end
         | 
| 27 | 
            +
              
         | 
| 28 | 
            +
            end
         | 
| @@ -0,0 +1,16 @@ | |
| 1 | 
            +
            require File.join(File.dirname(__FILE__), '..', 'test_helper')
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class XMDPTest < Test::Unit::TestCase
         | 
| 4 | 
            +
              setup do
         | 
| 5 | 
            +
                html = get_fixture('xmdp.html')
         | 
| 6 | 
            +
                doc = Nokogiri::HTML.parse(html)
         | 
| 7 | 
            +
                @klass = HMachine::Microformat::XMDP
         | 
| 8 | 
            +
                @node = @klass.find_in(doc).first
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
              
         | 
| 11 | 
            +
              should 'be just like a definition list' do
         | 
| 12 | 
            +
                xmdp = @klass.new(@node)
         | 
| 13 | 
            +
                assert xmdp.to_h.has_key?(:rel)
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
              
         | 
| 16 | 
            +
            end
         | 
| @@ -0,0 +1,51 @@ | |
| 1 | 
            +
            require File.join(File.dirname(__FILE__), '..', 'test_helper')
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class XOXOTest < Test::Unit::TestCase
         | 
| 4 | 
            +
              setup do
         | 
| 5 | 
            +
                html = get_fixture('xoxo.html')
         | 
| 6 | 
            +
                doc = Nokogiri::HTML.parse(html)
         | 
| 7 | 
            +
                @klass = HMachine::Microformat::XOXO
         | 
| 8 | 
            +
                @node = @klass.find_in(doc).first
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
                
         | 
| 11 | 
            +
              should 'construct an outline' do
         | 
| 12 | 
            +
                test = @klass.new(@node)
         | 
| 13 | 
            +
                assert_equal 5, test.outline.count
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
              
         | 
| 16 | 
            +
              should 'convert to an array easily' do
         | 
| 17 | 
            +
                test = @klass.new(@node)
         | 
| 18 | 
            +
                assert_equal test.outline, test.to_a
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
              
         | 
| 21 | 
            +
              should 'index quickly into the outline' do
         | 
| 22 | 
            +
                test = @klass.new(@node)
         | 
| 23 | 
            +
                assert_equal test.to_a[0], test[0]
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
              
         | 
| 26 | 
            +
              should 'convert list items to an array' do
         | 
| 27 | 
            +
                test = @klass.new(@node)
         | 
| 28 | 
            +
                assert_instance_of Array, test[0]
         | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
              
         | 
| 31 | 
            +
              should 'convert lists to arrays' do
         | 
| 32 | 
            +
                test = @klass.new(@node)
         | 
| 33 | 
            +
                assert_instance_of Array, test[0][1]
         | 
| 34 | 
            +
              end
         | 
| 35 | 
            +
              
         | 
| 36 | 
            +
              should 'convert definition lists to hashes' do
         | 
| 37 | 
            +
                test = @klass.new(@node)
         | 
| 38 | 
            +
                assert test[3].first.has_key?("description"), "Definition List is: #{test[3].first.inspect}"
         | 
| 39 | 
            +
              end
         | 
| 40 | 
            +
              
         | 
| 41 | 
            +
              should 'convert links to hashes' do
         | 
| 42 | 
            +
                test = @klass.new(@node)
         | 
| 43 | 
            +
                assert test[4].first.has_key?(:url), "Definition List is: #{test[4].first.inspect}"
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
              
         | 
| 46 | 
            +
              should 'know if this is a blogroll' do
         | 
| 47 | 
            +
                test = @klass.new(@node)
         | 
| 48 | 
            +
                assert !test.blogroll?, "Test is a blogroll"
         | 
| 49 | 
            +
              end    
         | 
| 50 | 
            +
              
         | 
| 51 | 
            +
            end
         | 
    
        data/test/microformat_test.rb
    CHANGED
    
    | @@ -1,42 +1,20 @@ | |
| 1 1 | 
             
            require File.join(File.dirname(__FILE__), 'test_helper')
         | 
| 2 2 |  | 
| 3 | 
            -
            class MicroformatTest < Test::Unit::TestCase
         | 
| 4 | 
            -
               | 
| 5 | 
            -
                 | 
| 6 | 
            -
                 | 
| 7 | 
            -
                @hcard_class = HMachine::Microformat::HCard
         | 
| 8 | 
            -
              end
         | 
| 9 | 
            -
                
         | 
| 10 | 
            -
              test 'creates a microformat for a given node' do
         | 
| 11 | 
            -
                hcard = HMachine::Microformat.create_for_node(@hcard_class, @document.css(@hcard_class::ROOT_SELECTOR)[0])
         | 
| 12 | 
            -
                assert hcard.is_a?(@hcard_class), "Created a #{hcard.class}"
         | 
| 13 | 
            -
              end
         | 
| 14 | 
            -
              
         | 
| 15 | 
            -
              test "rejects invalid nodes" do
         | 
| 16 | 
            -
                hcard = HMachine::Microformat.create_for_node(@hcard_class, @document)
         | 
| 17 | 
            -
                assert hcard.nil? 
         | 
| 18 | 
            -
              end
         | 
| 19 | 
            -
              
         | 
| 20 | 
            -
              test 'finds a given microformat in a document' do
         | 
| 21 | 
            -
                first_hcard = HMachine::Microformat.find_in_node(@hcard_class, @document)[0]
         | 
| 22 | 
            -
                assert first_hcard.is_a?(@hcard_class), "Object is a #{first_hcard.class}"
         | 
| 3 | 
            +
            class MicroformatTest < Test::Unit::TestCase  
         | 
| 4 | 
            +
              should 'map a symbol to a design pattern' do
         | 
| 5 | 
            +
                assert HMachine::Microformat::HCard, HMachine::Microformat.map(:hcard)
         | 
| 6 | 
            +
                assert HMachine::Microformat::RelTag, HMachine.map(:reltag)
         | 
| 23 7 | 
             
              end
         | 
| 24 8 |  | 
| 25 | 
            -
               | 
| 26 | 
            -
                 | 
| 27 | 
            -
                 | 
| 9 | 
            +
              should 'find all microformats in a document' do
         | 
| 10 | 
            +
                huffduff = HMachine::Microformat.find_all get_fixture('huffduffer.html')
         | 
| 11 | 
            +
                assert_equal 45, huffduff.count
         | 
| 28 12 | 
             
              end
         | 
| 29 13 |  | 
| 30 | 
            -
               | 
| 31 | 
            -
                 | 
| 32 | 
            -
                 | 
| 14 | 
            +
              should 'find specific microformats in a document' do
         | 
| 15 | 
            +
                huffduff = HMachine::Microformat.find get_fixture('huffduffer.html'), :hcard
         | 
| 16 | 
            +
                assert_equal 'mawunsch', huffduff.first.fn
         | 
| 17 | 
            +
                assert_equal 10, huffduff.count
         | 
| 33 18 | 
             
              end
         | 
| 34 | 
            -
             | 
| 35 | 
            -
              describe 'Find hCard' do    
         | 
| 36 | 
            -
                test 'document contains an hCard' do
         | 
| 37 | 
            -
                  first_hcard = HMachine::Microformat.find_hcard(@document)[0]
         | 
| 38 | 
            -
                  assert first_hcard.is_a?(@hcard_class), "Object is a #{first_hcard.class}"
         | 
| 39 | 
            -
                end
         | 
| 40 | 
            -
              end
         | 
| 41 | 
            -
              
         | 
| 19 | 
            +
             | 
| 42 20 | 
             
            end
         | 
| @@ -0,0 +1,55 @@ | |
| 1 | 
            +
            require File.join(File.dirname(__FILE__),'..','test_helper')
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class DateTimePatternTest < Test::Unit::TestCase
         | 
| 4 | 
            +
              setup do
         | 
| 5 | 
            +
                @pattern = HMachine::Pattern::DateTime
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
              
         | 
| 8 | 
            +
              should 'recognize a simple iso8601 date' do
         | 
| 9 | 
            +
                assert @pattern.date?('2010-02-14')
         | 
| 10 | 
            +
                assert @pattern.date?('2010-185')
         | 
| 11 | 
            +
                assert !@pattern.date?('12:00pm')
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
              
         | 
| 14 | 
            +
              should 'recognize a simple iso8601 time' do
         | 
| 15 | 
            +
                assert @pattern.time?('12:00')
         | 
| 16 | 
            +
                assert @pattern.time?('4:00pm')
         | 
| 17 | 
            +
                assert @pattern.time?('12:45:30Z')
         | 
| 18 | 
            +
                assert !@pattern.time?('2010-02-14')
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
              
         | 
| 21 | 
            +
              should 'build an iso8601 date' do
         | 
| 22 | 
            +
                assert '2010-02-14', @pattern.date('February 14, 2010')
         | 
| 23 | 
            +
                assert_equal @pattern.date('July 4 2010'), @pattern.date('2010-185')
         | 
| 24 | 
            +
                assert_equal '2010-1-1', @pattern.date('January 2010')
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
              
         | 
| 27 | 
            +
              should 'build an iso8601 time' do
         | 
| 28 | 
            +
                assert_equal 'T12:0:0-18000', @pattern.time('12:00')
         | 
| 29 | 
            +
                assert_equal 'T13:0:0-18000', @pattern.time('1:00pm')
         | 
| 30 | 
            +
                assert_equal 'T4:45:30Z', @pattern.time('4:45:30Z')
         | 
| 31 | 
            +
                assert !@pattern.time('12')
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
              
         | 
| 34 | 
            +
              should 'build a iso8601 timestamp' do
         | 
| 35 | 
            +
                assert_equal '2010-2-14', @pattern.iso8601('2010-02-14')
         | 
| 36 | 
            +
                assert_equal 'T4:45:30Z', @pattern.iso8601('4:45:30Z')
         | 
| 37 | 
            +
                assert_equal '2010-2-14T4:45:30Z', @pattern.iso8601('February 14 2010 4:45:30Z')
         | 
| 38 | 
            +
                assert @pattern.iso8601('Hello world').empty?
         | 
| 39 | 
            +
              end
         | 
| 40 | 
            +
              
         | 
| 41 | 
            +
              should 'validate a DateTime string' do
         | 
| 42 | 
            +
                assert @pattern.valid?('2010-02-14')
         | 
| 43 | 
            +
                assert !@pattern.valid?('Hello World!')
         | 
| 44 | 
            +
                assert !@pattern.valid?('+441223 123 123')
         | 
| 45 | 
            +
              end
         | 
| 46 | 
            +
              
         | 
| 47 | 
            +
              should 'convert a datetime string to a Time object' do
         | 
| 48 | 
            +
                valentines_day = @pattern.extract_from('2010-02-14')
         | 
| 49 | 
            +
                afternoon = @pattern.extract_from('16:30')
         | 
| 50 | 
            +
                assert_equal Time, valentines_day.class
         | 
| 51 | 
            +
                assert_equal 2010, valentines_day.year
         | 
| 52 | 
            +
                assert_equal 16, afternoon.hour
         | 
| 53 | 
            +
              end
         | 
| 54 | 
            +
              
         | 
| 55 | 
            +
            end
         | 
| @@ -0,0 +1,33 @@ | |
| 1 | 
            +
            require File.join(File.dirname(__FILE__),'..','test_helper')
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class ValueClassPatternTest < Test::Unit::TestCase
         | 
| 4 | 
            +
              setup do
         | 
| 5 | 
            +
                @pattern = HMachine::Pattern::ValueClass    
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
              
         | 
| 8 | 
            +
              should 'concatenate two html elements to create one datetime value' do
         | 
| 9 | 
            +
                html = get_fixture('test-fixture/value-class-date-time/value-dt-test-YYYY-MM-DD--HH-MM.html')
         | 
| 10 | 
            +
                doc = Nokogiri.parse(html)
         | 
| 11 | 
            +
                doc_dtstart = doc.css('.dtstart').first
         | 
| 12 | 
            +
                doc_dtend = doc.css('.dtend').first
         | 
| 13 | 
            +
                
         | 
| 14 | 
            +
                dtstart = @pattern.extract_from(doc_dtstart)
         | 
| 15 | 
            +
                dtend = @pattern.extract_from(doc_dtend)
         | 
| 16 | 
            +
                assert_equal Time, dtstart.class
         | 
| 17 | 
            +
                assert_equal 2009, dtstart.year
         | 
| 18 | 
            +
                assert_equal 6, dtstart.mon
         | 
| 19 | 
            +
                assert_equal 26, dtstart.day
         | 
| 20 | 
            +
                assert_equal 19, dtstart.hour
         | 
| 21 | 
            +
                assert_equal 22, dtend.hour
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
              
         | 
| 24 | 
            +
              should 'concatenate abbr title attribute and the text from a span element to create one datetime value' do
         | 
| 25 | 
            +
                html = get_fixture('test-fixture/value-class-date-time/value-dt-test-abbr-YYYY-MM-DD--HH-MM.html')
         | 
| 26 | 
            +
                doc = Nokogiri.parse(html)
         | 
| 27 | 
            +
                
         | 
| 28 | 
            +
                dtstart = @pattern.extract_from(doc)
         | 
| 29 | 
            +
                assert_equal 2008, dtstart.year
         | 
| 30 | 
            +
                assert_equal 2, dtstart.wday
         | 
| 31 | 
            +
              end
         | 
| 32 | 
            +
              
         | 
| 33 | 
            +
            end
         | 
| @@ -0,0 +1,132 @@ | |
| 1 | 
            +
            require File.join(File.dirname(__FILE__), 'test_helper')
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class PatternTest < Test::Unit::TestCase
         | 
| 4 | 
            +
              describe 'Mapper' do
         | 
| 5 | 
            +
                should 'map a symbol to a design pattern' do
         | 
| 6 | 
            +
                  assert HMachine::Pattern::ValueClass, HMachine::Pattern.map(:value_class)
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
              end
         | 
| 9 | 
            +
              
         | 
| 10 | 
            +
              describe 'URI Design Pattern' do
         | 
| 11 | 
            +
                should 'have an extraction method' do
         | 
| 12 | 
            +
                  assert_respond_to HMachine::Pattern::URL, :extract
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
                
         | 
| 15 | 
            +
                should 'normalize URLs' do
         | 
| 16 | 
            +
                  assert_respond_to HMachine::Pattern::URL, :normalize
         | 
| 17 | 
            +
                  assert_equal 'http://foobar.com/', HMachine::Pattern::URL.normalize('http://foobar.com')
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
                
         | 
| 20 | 
            +
                should 'extract the href out of an a element' do
         | 
| 21 | 
            +
                  doc = Nokogiri.parse('<a href="http://foobar.com">Hello</a>')
         | 
| 22 | 
            +
                  a = doc.css('a').first
         | 
| 23 | 
            +
                  assert_equal 'http://foobar.com/', HMachine::Pattern::URL.extract_from(a)
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
                
         | 
| 26 | 
            +
                should 'extract the src out of an img element' do
         | 
| 27 | 
            +
                  doc = Nokogiri.parse('<img src="http://foobar.com" alt="Foobar" />')
         | 
| 28 | 
            +
                  img = doc.css('img').first
         | 
| 29 | 
            +
                  assert_equal 'http://foobar.com/', HMachine::Pattern::URL.extract_from(img)
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
              end
         | 
| 32 | 
            +
              
         | 
| 33 | 
            +
              describe 'Type/Value Pattern' do
         | 
| 34 | 
            +
                setup do
         | 
| 35 | 
            +
                  @html = get_fixture('test-fixture/hcard/hcard6.html')
         | 
| 36 | 
            +
                  @doc = Nokogiri::HTML.parse(@html).css('#uf').first
         | 
| 37 | 
            +
                end
         | 
| 38 | 
            +
                
         | 
| 39 | 
            +
                should 'extract the type and value of an element' do
         | 
| 40 | 
            +
                  html = Nokogiri.parse(%Q{<span class="email">
         | 
| 41 | 
            +
            	      <span class="type">internet</span> 
         | 
| 42 | 
            +
            	      <a href="mailto:notthis@example.com">john@example.com</a>
         | 
| 43 | 
            +
                  </span>})
         | 
| 44 | 
            +
                  doc = html.css('.email').first
         | 
| 45 | 
            +
                  type_value = HMachine::Pattern::TypeValue.extract_from(doc)
         | 
| 46 | 
            +
                  assert type_value.has_key?(:type)
         | 
| 47 | 
            +
                  assert_equal :internet, type_value[:type]
         | 
| 48 | 
            +
                  assert_equal 'john@example.com', type_value[:value]
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
              end
         | 
| 51 | 
            +
              
         | 
| 52 | 
            +
              describe 'Abbr Design Pattern' do
         | 
| 53 | 
            +
                should 'have an extraction method' do
         | 
| 54 | 
            +
                  assert_respond_to HMachine::Pattern::Abbr, :extract
         | 
| 55 | 
            +
                end
         | 
| 56 | 
            +
                
         | 
| 57 | 
            +
                should 'extract the title out of an abbreviation element' do
         | 
| 58 | 
            +
                  doc = Nokogiri.parse('<abbr title="Hello machine">Hello Human</abbr>')
         | 
| 59 | 
            +
                  node = doc.css('abbr').first
         | 
| 60 | 
            +
                  assert_equal 'Hello machine', HMachine::Pattern::Abbr.extract_from(node)
         | 
| 61 | 
            +
                end
         | 
| 62 | 
            +
                
         | 
| 63 | 
            +
                should 'continue as usual if no title is found' do
         | 
| 64 | 
            +
                  doc = Nokogiri.parse('<abbr>Hello human</abbr>')
         | 
| 65 | 
            +
                  node = doc.css('abbr').first
         | 
| 66 | 
            +
                  assert_equal 'Hello human', HMachine::Pattern::Abbr.extract_from(node)
         | 
| 67 | 
            +
                end
         | 
| 68 | 
            +
              end
         | 
| 69 | 
            +
              
         | 
| 70 | 
            +
              describe 'Value Class Pattern' do
         | 
| 71 | 
            +
                # http://microformats.org/wiki/value-class-pattern-tests
         | 
| 72 | 
            +
                should 'find value class in a document' do
         | 
| 73 | 
            +
                  doc = Nokogiri.parse(%q{
         | 
| 74 | 
            +
                    <span class="tel">
         | 
| 75 | 
            +
                      <span class="type">Home</span>:
         | 
| 76 | 
            +
                      <span class="value">+44</span> (0) <span class="value">1223 123 123</span>
         | 
| 77 | 
            +
                    </span>
         | 
| 78 | 
            +
                  })
         | 
| 79 | 
            +
                  assert HMachine::Pattern::ValueClass.found_in?(doc)
         | 
| 80 | 
            +
                end
         | 
| 81 | 
            +
                
         | 
| 82 | 
            +
                should 'ignore nested value classes' do
         | 
| 83 | 
            +
                  doc = Nokogiri.parse(%q{
         | 
| 84 | 
            +
                    <p class="description">
         | 
| 85 | 
            +
                      <span class="value">
         | 
| 86 | 
            +
                        <em class="value">Puppies Rule!</em>
         | 
| 87 | 
            +
                        <strong>But kittens are better!</strong>
         | 
| 88 | 
            +
                     </span>
         | 
| 89 | 
            +
                    </p>
         | 
| 90 | 
            +
                  })
         | 
| 91 | 
            +
                  assert_equal 1, HMachine::Pattern::ValueClass.find_in(doc).size
         | 
| 92 | 
            +
                  assert_not_equal 'Puppies Rule!', HMachine::Pattern::ValueClass.extract_from(doc)
         | 
| 93 | 
            +
                  assert HMachine::Pattern::ValueClass.valid?(HMachine::Pattern::ValueClass.find_in(doc).first)
         | 
| 94 | 
            +
                end
         | 
| 95 | 
            +
                
         | 
| 96 | 
            +
                should 'extract the value' do
         | 
| 97 | 
            +
                  doc = Nokogiri.parse(%q{<p class="description"><span class="value">Value Class Pattern</span></p>})
         | 
| 98 | 
            +
                  assert_equal 'Value Class Pattern', HMachine::Pattern::ValueClass.extract_from(doc)
         | 
| 99 | 
            +
                end
         | 
| 100 | 
            +
                
         | 
| 101 | 
            +
                should 'concatenate values' do
         | 
| 102 | 
            +
                  doc = Nokogiri.parse(%q{
         | 
| 103 | 
            +
                    <span class="tel">
         | 
| 104 | 
            +
                      <span class="type">Home</span>:
         | 
| 105 | 
            +
                      <span class="value">+44</span> (0) <span class="value">1223 123 123</span>
         | 
| 106 | 
            +
                    </span>
         | 
| 107 | 
            +
                  })
         | 
| 108 | 
            +
                  assert_equal '+441223 123 123', HMachine::Pattern::ValueClass.extract_from(doc)
         | 
| 109 | 
            +
                end
         | 
| 110 | 
            +
                
         | 
| 111 | 
            +
                should 'extract the value intelligently from an abbr element' do
         | 
| 112 | 
            +
                  doc = Nokogiri.parse(%q{<span class="dtstart"><abbr class="value" title="hello">this Tuesday</abbr></span>})
         | 
| 113 | 
            +
                  assert_not_equal 'this Tuesday', HMachine::Pattern::ValueClass.extract_from(doc)
         | 
| 114 | 
            +
                  assert_equal 'hello', HMachine::Pattern::ValueClass.extract_from(doc)
         | 
| 115 | 
            +
                end
         | 
| 116 | 
            +
                
         | 
| 117 | 
            +
                should 'use the alt attribute for images or areas' do
         | 
| 118 | 
            +
                  doc = Nokogiri.parse(%q{<div class="photo">Here's me: <img src="photo.jpg" class="value" alt="Whatever" /></div>})
         | 
| 119 | 
            +
                  assert_equal "Whatever", HMachine::Pattern::ValueClass.extract_from(doc)
         | 
| 120 | 
            +
                end
         | 
| 121 | 
            +
                
         | 
| 122 | 
            +
                should 'understand the value-title pattern' do
         | 
| 123 | 
            +
                  doc = Nokogiri.parse(%q{
         | 
| 124 | 
            +
                  <span class="dtstart">
         | 
| 125 | 
            +
                    <span class="value-title" title="hi"> </span>
         | 
| 126 | 
            +
                    Friday, June 5th at 8pm
         | 
| 127 | 
            +
                  </span>})
         | 
| 128 | 
            +
                  assert_equal 'hi', HMachine::Pattern::ValueClass.extract_from(doc)
         | 
| 129 | 
            +
                end
         | 
| 130 | 
            +
                
         | 
| 131 | 
            +
              end
         | 
| 132 | 
            +
            end
         |