hmachine 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. data/.gitignore +2 -2
  2. data/Gemfile +6 -4
  3. data/Gemfile.lock +51 -0
  4. data/README.md +123 -9
  5. data/Rakefile +12 -3
  6. data/bin/hmachine +99 -0
  7. data/hmachine.gemspec +132 -0
  8. data/lib/hmachine.rb +121 -12
  9. data/lib/hmachine/microformat.rb +39 -20
  10. data/lib/hmachine/microformat/adr.rb +22 -0
  11. data/lib/hmachine/microformat/geo.rb +48 -0
  12. data/lib/hmachine/microformat/hcard.rb +169 -11
  13. data/lib/hmachine/microformat/rellicense.rb +20 -0
  14. data/lib/hmachine/microformat/reltag.rb +38 -0
  15. data/lib/hmachine/microformat/votelinks.rb +42 -0
  16. data/lib/hmachine/microformat/xfn.rb +54 -0
  17. data/lib/hmachine/microformat/xmdp.rb +14 -0
  18. data/lib/hmachine/microformat/xoxo.rb +69 -0
  19. data/lib/hmachine/pattern.rb +26 -0
  20. data/lib/hmachine/pattern/abbr.rb +21 -0
  21. data/lib/hmachine/pattern/datetime.rb +75 -0
  22. data/lib/hmachine/pattern/typevalue.rb +32 -0
  23. data/lib/hmachine/pattern/url.rb +32 -0
  24. data/lib/hmachine/pattern/valueclass.rb +51 -0
  25. data/lib/hmachine/posh.rb +3 -0
  26. data/lib/hmachine/posh/anchor.rb +40 -0
  27. data/lib/hmachine/posh/base.rb +204 -0
  28. data/lib/hmachine/posh/definition_list.rb +41 -0
  29. data/test/fixtures/huffduffer.html +466 -0
  30. data/test/fixtures/likeorhate.html +48 -0
  31. data/test/fixtures/rel_license.html +4 -0
  32. data/test/fixtures/test-fixture/hcard/hcard1.html +147 -0
  33. data/test/fixtures/test-fixture/hcard/hcard11.html +123 -0
  34. data/test/fixtures/test-fixture/hcard/hcard12.html +178 -0
  35. data/test/fixtures/test-fixture/hcard/hcard17.html +165 -0
  36. data/test/fixtures/test-fixture/hcard/hcard2.html +264 -0
  37. data/test/fixtures/test-fixture/hcard/hcard3.html +144 -0
  38. data/test/fixtures/test-fixture/hcard/hcard4.html +117 -0
  39. data/test/fixtures/test-fixture/hcard/hcard5.html +119 -0
  40. data/test/fixtures/test-fixture/hcard/hcard6.html +188 -0
  41. data/test/fixtures/test-fixture/hcard/hcard7.html +188 -0
  42. data/test/fixtures/test-fixture/hcard/hcard8.html +130 -0
  43. data/test/fixtures/test-fixture/hcard/hcard9.html +111 -0
  44. data/test/fixtures/test-fixture/hcard/hcard99.html +215 -0
  45. data/test/fixtures/test-fixture/value-class-date-time/value-dt-test-YYYY-MM-DD--HH-MM.html +9 -0
  46. data/test/fixtures/test-fixture/value-class-date-time/value-dt-test-abbr-YYYY-MM-DD--HH-MM.html +4 -0
  47. data/test/fixtures/xfn.html +198 -0
  48. data/test/fixtures/xmdp.html +32 -0
  49. data/test/fixtures/xoxo.html +51 -0
  50. data/test/hmachine_test.rb +122 -6
  51. data/test/microformat/adr_test.rb +47 -0
  52. data/test/microformat/geo_test.rb +66 -0
  53. data/test/microformat/hcard_test.rb +487 -20
  54. data/test/microformat/rellicense_test.rb +36 -0
  55. data/test/microformat/reltag_test.rb +61 -0
  56. data/test/microformat/votelinks_test.rb +44 -0
  57. data/test/microformat/xfn_test.rb +28 -0
  58. data/test/microformat/xmdp_test.rb +16 -0
  59. data/test/microformat/xoxo_test.rb +51 -0
  60. data/test/microformat_test.rb +12 -34
  61. data/test/pattern/date_time_test.rb +55 -0
  62. data/test/pattern/value_class_test.rb +33 -0
  63. data/test/pattern_test.rb +132 -0
  64. data/test/posh/anchor_test.rb +41 -0
  65. data/test/posh/base_test.rb +150 -0
  66. data/test/posh/definition_list_test.rb +38 -0
  67. data/test/test_helper.rb +24 -6
  68. metadata +93 -15
  69. data/lib/hmachine/microformat/base.rb +0 -17
@@ -0,0 +1,41 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class PoshAnchorTest < Test::Unit::TestCase
4
+ setup do
5
+ @html = '<a href="http://something.com" rel="me friend" title="This is a link" type="good">Link to something</a>'
6
+ @doc = Nokogiri.parse(@html)
7
+ end
8
+
9
+ should 'parse all anchor tags from a document' do
10
+ link = HMachine::POSH::Anchor.parse(@doc)
11
+ assert_equal HMachine::POSH::Anchor, link.class
12
+ end
13
+
14
+ should 'have rel values' do
15
+ link = HMachine::POSH::Anchor.parse(@doc)
16
+ assert_respond_to link, :rel
17
+ assert_equal 2, link.rel.count
18
+ assert_equal 'me', link.rel.first
19
+ end
20
+
21
+ should 'have a url' do
22
+ link = HMachine::POSH::Anchor.parse(@doc)
23
+ assert_respond_to link, :url
24
+ assert_equal 'http://something.com', link.url
25
+ end
26
+
27
+ should 'have some text' do
28
+ link = HMachine::POSH::Anchor.parse(@doc)
29
+ assert_respond_to link, :text
30
+ assert_respond_to link, :content
31
+ assert_equal 'Link to something', link.text
32
+ assert_equal link.text, link.content
33
+ end
34
+
35
+ should 'have a title' do
36
+ link = HMachine::POSH::Anchor.parse(@doc)
37
+ assert_respond_to link, :type
38
+ assert_equal 'good', link.type
39
+ end
40
+
41
+ end
@@ -0,0 +1,150 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class PoshBaseTest < Test::Unit::TestCase
4
+ setup do
5
+ @html = get_fixture('hcard/commercenet.html')
6
+ @doc = Nokogiri.parse(@html, 'http://foobar.com/')
7
+ end
8
+
9
+ describe 'Inheritance' do
10
+ setup do
11
+ @test_class = Class.new(HMachine::POSH::Base)
12
+ @test_class.name :vcard
13
+ end
14
+
15
+ should "add a property to its group of properties" do
16
+ property = @test_class.add_property(:fn)
17
+ assert_equal property, @test_class.properties[:fn]
18
+ end
19
+
20
+ should 'further refine a property with a block' do
21
+ property = @test_class.add_property(:fn) do
22
+ add_property(:n)
23
+ end
24
+ assert_equal property[:n], @test_class.properties[:fn][:n]
25
+ end
26
+
27
+ should 'find a property' do
28
+ property = @test_class.add_property(:fn)
29
+ assert_equal property, @test_class[:fn]
30
+ end
31
+
32
+ should 'have one property' do
33
+ property = @test_class.has_one!(:fn)
34
+ assert_respond_to @test_class, :one
35
+ assert_equal 1, @test_class.one.length
36
+ assert @test_class.one.include?(property)
37
+ end
38
+
39
+ should 'has many properties' do
40
+ property = @test_class.has_many!(:tel)
41
+ assert_respond_to @test_class, :many
42
+ assert_equal 1, @test_class.many.length
43
+ assert_equal property, @test_class.many.first
44
+ end
45
+
46
+ should 'parse by properties' do
47
+ property = @test_class.has_one :fn
48
+ assert_equal property.first, @test_class.one.first
49
+ assert_equal 'CommerceNet', @test_class.parse(@doc)[:fn]
50
+ end
51
+
52
+ should 'parse itself out of a document' do
53
+ @test_class.has_one :fn
54
+ assert_instance_of @test_class, @test_class.parse(@doc)
55
+ end
56
+
57
+ should 'have one property and define an instance method' do
58
+ property = @test_class.has_one :fn
59
+ assert_equal property.first, @test_class.instance_variable_get(:@has_one).first
60
+ assert_respond_to @test_class.parse_first(@doc), :fn
61
+ assert_equal property.first.parse_first(@doc), @test_class.parse(@doc).fn
62
+ end
63
+
64
+ should 'have many of a type of properties and define an instance method' do
65
+ property = @test_class.has_many :tel
66
+ assert_respond_to @test_class.parse(@doc), :tel
67
+ end
68
+
69
+ should 'subclasses retain properties' do
70
+ @test_class.has_one :fn
71
+ @test_class.has_many :tel
72
+ klass = Class.new(@test_class)
73
+ klass.name :vcard
74
+ assert_equal @test_class.properties, klass.properties
75
+ assert_equal @test_class.instance_variable_get(:@has_one), klass.instance_variable_get(:@has_one)
76
+ assert_equal @test_class.instance_variable_get(:@has_many), klass.instance_variable_get(:@has_many)
77
+ klass.has_one :foo
78
+ assert !@test_class.properties.has_key?(:foo)
79
+ assert klass.properties.has_key?(:foo)
80
+ assert_respond_to klass.parse_first(@doc), :foo
81
+ end
82
+ end
83
+
84
+ describe 'Instance' do
85
+ setup do
86
+ @klass = Class.new(HMachine::POSH::Base)
87
+ property = 'vcard'
88
+ @doc = Nokogiri.parse(@html, 'http://foobar.com/')
89
+ @klass.search { |doc| doc.css(".#{property}") }
90
+ @klass.validate { |node| node['class'] && node['class'].split(' ').include?(property) }
91
+ @node = @klass.find_in(@doc).first
92
+ end
93
+
94
+ should 'have a node' do
95
+ assert_equal @node, @klass.new(@node).node
96
+ end
97
+
98
+ should 'have its own set of properties' do
99
+ fn = @klass.has_one :fn
100
+ @klass.has_many :foobar
101
+ vcard = @klass.new(@node)
102
+ assert vcard.properties.has_key?(:fn)
103
+ assert !vcard.properties.has_key?(:foobar)
104
+ assert_equal fn.first, vcard.properties[:fn]
105
+ end
106
+
107
+ should 'lookup a property value' do
108
+ fn = @klass.has_one :fn
109
+ vcard = @klass.new(@node)
110
+ assert_equal vcard.to_h[:fn], vcard[:fn]
111
+ assert_equal fn.first.parse_first(@node), vcard[:fn]
112
+ end
113
+
114
+ should 'convert to a hash' do
115
+ fn = @klass.has_one :fn
116
+ tel = @klass.has_many :tel
117
+ @klass.has_many :foobar
118
+ vcard = @klass.new(@node)
119
+ assert vcard.to_h.has_key?(:fn)
120
+ assert vcard.to_h.has_key?(:tel)
121
+ assert !vcard.to_h.has_key?(:foobar)
122
+ end
123
+
124
+ should 'not search for properties in nested microformats' do
125
+ nested = '<div class="vcard"><span class="fn">Mark Wunsch</span> and <div class="vcard"><span class="fn">Somebody else</span></div></div>'
126
+ doc = Nokogiri::HTML.parse(nested)
127
+ @klass.has_many :fn
128
+ vcard = @klass.parse_first(doc)
129
+ assert vcard.instance_variable_get(:@first_node) != vcard.node
130
+ assert_equal "Mark Wunsch", vcard.fn
131
+ end
132
+
133
+ should 'have a DSL for defining properties' do
134
+ @klass.has_one :fn
135
+ @klass.has_many :tel
136
+ @klass.has_many! :foobar
137
+ vcard = @klass.new(@node)
138
+ assert_respond_to vcard, :fn
139
+ assert_respond_to vcard, :tel
140
+ assert !vcard.respond_to?(:foobar)
141
+ end
142
+
143
+ should 'recall the source (url) of the document' do
144
+ vcard = @klass.new(@node)
145
+ assert_respond_to vcard, :source
146
+ assert_equal 'http://foobar.com/', vcard.source
147
+ end
148
+
149
+ end
150
+ end
@@ -0,0 +1,38 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class PoshDefinitionListTest < Test::Unit::TestCase
4
+ setup do
5
+ @html = get_fixture('xmdp.html')
6
+ @doc = Nokogiri.parse(@html)
7
+ end
8
+
9
+ should 'pull itself out of a document' do
10
+ dl = HMachine::POSH::DefinitionList.parse_first(@doc)
11
+ assert_equal HMachine::POSH::DefinitionList, dl.class
12
+ end
13
+
14
+ should 'build a hash out of a definition list' do
15
+ dl = HMachine::POSH::DefinitionList.find_in(@doc).first
16
+ dict = HMachine::POSH::DefinitionList.build_dictionary(dl)
17
+ assert dict.has_key?(:rel)
18
+ assert !dict.has_key?(:script)
19
+ end
20
+
21
+ should 'parse a defintion list' do
22
+ dl = HMachine::POSH::DefinitionList.parse_first(@doc)
23
+ assert_respond_to dl, :to_h
24
+ assert dl.to_h.has_key?(:rel)
25
+ assert dl.to_h[:rel].has_key?(:script)
26
+ end
27
+
28
+ should 'list its properties as its hash representation' do
29
+ dl = HMachine::POSH::DefinitionList.parse_first(@doc)
30
+ assert_equal dl.to_h, dl.properties
31
+ end
32
+
33
+ should 'have key lookup methods' do
34
+ dl = HMachine::POSH::DefinitionList.parse_first(@doc)
35
+ assert_equal dl.to_h[:rel], dl[:rel]
36
+ end
37
+
38
+ end
@@ -1,14 +1,32 @@
1
- require 'test/unit'
2
- require File.join(File.dirname(__FILE__), "../vendor/gems/environment")
3
-
4
- lib_path = File.join(File.dirname(__FILE__), '..', 'lib')
5
- $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
1
+ begin
2
+ # Try to require the preresolved locked set of gems.
3
+ require File.expand_path('../.bundle/environment', __FILE__)
4
+ rescue LoadError
5
+ # Fall back on doing an unlocked resolve at runtime.
6
+ require "rubygems"
7
+ require "bundler"
8
+ Bundler.setup
9
+ end
6
10
 
11
+ require 'test/unit'
7
12
  require 'contest'
8
13
  require 'redgreen'
14
+ require 'fakeweb'
9
15
 
10
- require 'hmachine'
16
+ begin
17
+ require 'hmachine'
18
+ rescue LoadError
19
+ lib_path = File.join(File.dirname(__FILE__), '..', 'lib')
20
+ $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
21
+ require 'hmachine'
22
+ end
11
23
 
12
24
  def get_fixture(filename)
13
25
  open(File.join(File.dirname(__FILE__), 'fixtures', "#{filename}")).read
26
+ end
27
+
28
+ # http://microformats.org/wiki/test-fixture
29
+ def test_fixture(filename)
30
+ html = get_fixture('test-fixture/' + filename)
31
+ doc = Nokogiri::HTML.parse(html).css('#uf').first
14
32
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hmachine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Mark Wunsch
@@ -9,24 +14,26 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-01-09 00:00:00 -05:00
13
- default_executable:
17
+ date: 2010-02-24 00:00:00 -05:00
18
+ default_executable: hmachine
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: nokogiri
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
23
29
  version: "0"
24
- version:
25
- description: A Ruby microformat parser powered by Nokogiri
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ description: A Ruby microformat parser and HTML toolkit powered by Nokogiri
26
33
  email:
27
34
  - mark@markwunsch.com
28
- executables: []
29
-
35
+ executables:
36
+ - hmachine
30
37
  extensions: []
31
38
 
32
39
  extra_rdoc_files:
@@ -35,18 +42,73 @@ extra_rdoc_files:
35
42
  files:
36
43
  - .gitignore
37
44
  - Gemfile
45
+ - Gemfile.lock
38
46
  - LICENSE
39
47
  - README.md
40
48
  - Rakefile
49
+ - bin/hmachine
50
+ - hmachine.gemspec
41
51
  - lib/hmachine.rb
42
52
  - lib/hmachine/microformat.rb
43
- - lib/hmachine/microformat/base.rb
53
+ - lib/hmachine/microformat/adr.rb
54
+ - lib/hmachine/microformat/geo.rb
44
55
  - lib/hmachine/microformat/hcard.rb
56
+ - lib/hmachine/microformat/rellicense.rb
57
+ - lib/hmachine/microformat/reltag.rb
58
+ - lib/hmachine/microformat/votelinks.rb
59
+ - lib/hmachine/microformat/xfn.rb
60
+ - lib/hmachine/microformat/xmdp.rb
61
+ - lib/hmachine/microformat/xoxo.rb
62
+ - lib/hmachine/pattern.rb
63
+ - lib/hmachine/pattern/abbr.rb
64
+ - lib/hmachine/pattern/datetime.rb
65
+ - lib/hmachine/pattern/typevalue.rb
66
+ - lib/hmachine/pattern/url.rb
67
+ - lib/hmachine/pattern/valueclass.rb
68
+ - lib/hmachine/posh.rb
69
+ - lib/hmachine/posh/anchor.rb
70
+ - lib/hmachine/posh/base.rb
71
+ - lib/hmachine/posh/definition_list.rb
45
72
  - test/fixtures/hcard/commercenet.html
46
73
  - test/fixtures/hcard/geo.html
74
+ - test/fixtures/huffduffer.html
75
+ - test/fixtures/likeorhate.html
76
+ - test/fixtures/rel_license.html
77
+ - test/fixtures/test-fixture/hcard/hcard1.html
78
+ - test/fixtures/test-fixture/hcard/hcard11.html
79
+ - test/fixtures/test-fixture/hcard/hcard12.html
80
+ - test/fixtures/test-fixture/hcard/hcard17.html
81
+ - test/fixtures/test-fixture/hcard/hcard2.html
82
+ - test/fixtures/test-fixture/hcard/hcard3.html
83
+ - test/fixtures/test-fixture/hcard/hcard4.html
84
+ - test/fixtures/test-fixture/hcard/hcard5.html
85
+ - test/fixtures/test-fixture/hcard/hcard6.html
86
+ - test/fixtures/test-fixture/hcard/hcard7.html
87
+ - test/fixtures/test-fixture/hcard/hcard8.html
88
+ - test/fixtures/test-fixture/hcard/hcard9.html
89
+ - test/fixtures/test-fixture/hcard/hcard99.html
90
+ - test/fixtures/test-fixture/value-class-date-time/value-dt-test-YYYY-MM-DD--HH-MM.html
91
+ - test/fixtures/test-fixture/value-class-date-time/value-dt-test-abbr-YYYY-MM-DD--HH-MM.html
92
+ - test/fixtures/xfn.html
93
+ - test/fixtures/xmdp.html
94
+ - test/fixtures/xoxo.html
47
95
  - test/hmachine_test.rb
96
+ - test/microformat/adr_test.rb
97
+ - test/microformat/geo_test.rb
48
98
  - test/microformat/hcard_test.rb
99
+ - test/microformat/rellicense_test.rb
100
+ - test/microformat/reltag_test.rb
101
+ - test/microformat/votelinks_test.rb
102
+ - test/microformat/xfn_test.rb
103
+ - test/microformat/xmdp_test.rb
104
+ - test/microformat/xoxo_test.rb
49
105
  - test/microformat_test.rb
106
+ - test/pattern/date_time_test.rb
107
+ - test/pattern/value_class_test.rb
108
+ - test/pattern_test.rb
109
+ - test/posh/anchor_test.rb
110
+ - test/posh/base_test.rb
111
+ - test/posh/definition_list_test.rb
50
112
  - test/test_helper.rb
51
113
  has_rdoc: true
52
114
  homepage: http://github.com/mwunsch/hmachine
@@ -61,23 +123,39 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
123
  requirements:
62
124
  - - ">="
63
125
  - !ruby/object:Gem::Version
126
+ segments:
127
+ - 0
64
128
  version: "0"
65
- version:
66
129
  required_rubygems_version: !ruby/object:Gem::Requirement
67
130
  requirements:
68
131
  - - ">="
69
132
  - !ruby/object:Gem::Version
133
+ segments:
134
+ - 0
70
135
  version: "0"
71
- version:
72
136
  requirements: []
73
137
 
74
138
  rubyforge_project:
75
- rubygems_version: 1.3.5
139
+ rubygems_version: 1.3.6
76
140
  signing_key:
77
141
  specification_version: 3
78
- summary: Ruby microformat parser
142
+ summary: Ruby microformat parser and HTML toolkit
79
143
  test_files:
80
144
  - test/hmachine_test.rb
145
+ - test/microformat/adr_test.rb
146
+ - test/microformat/geo_test.rb
81
147
  - test/microformat/hcard_test.rb
148
+ - test/microformat/rellicense_test.rb
149
+ - test/microformat/reltag_test.rb
150
+ - test/microformat/votelinks_test.rb
151
+ - test/microformat/xfn_test.rb
152
+ - test/microformat/xmdp_test.rb
153
+ - test/microformat/xoxo_test.rb
82
154
  - test/microformat_test.rb
155
+ - test/pattern/date_time_test.rb
156
+ - test/pattern/value_class_test.rb
157
+ - test/pattern_test.rb
158
+ - test/posh/anchor_test.rb
159
+ - test/posh/base_test.rb
160
+ - test/posh/definition_list_test.rb
83
161
  - test/test_helper.rb
@@ -1,17 +0,0 @@
1
- module HMachine
2
- module Microformat
3
- class Base
4
-
5
- def self.validate(node)
6
- node['class'] == self::ROOT_CLASS
7
- end
8
-
9
- def self.wiki_url
10
- self::WIKI_URL
11
- end
12
-
13
- attr_reader :node
14
-
15
- end
16
- end
17
- end