cartographer 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ === 1.1.0 / 2011-03-28
2
+
3
+ * from_xml takes sitemap.xml content and integrates it into the current Cartographer object.
4
+ * uniq! processes URLs through a uniqueness filter.
5
+ * Nokogiri is now a runtime dependency.
6
+
1
7
  === 1.0.0 / 2011-03-23
2
8
 
3
9
  * 1 major enhancement
data/Rakefile CHANGED
@@ -17,12 +17,14 @@ spec = Hoe.spec 'cartographer' do
17
17
  self.rubyforge_name = nil
18
18
 
19
19
  extra_deps << ['haml', '>= 0']
20
+ extra_deps << ['nokogiri', '>= 0']
20
21
 
21
22
  extra_dev_deps << ['minitest', '>= 0']
22
23
  extra_dev_deps << ['hoe-roodi', '>= 0']
23
24
  extra_dev_deps << ['hoe-reek', '>= 0']
24
25
  extra_dev_deps << ['hoe-git', '>= 0']
25
- extra_dev_deps << ['nokogiri', '>= 0']
26
+
27
+ self.testlib = :minitest
26
28
  end
27
29
 
28
30
  task :install => [:gem] do
data/lib/cartographer.rb CHANGED
@@ -2,6 +2,7 @@ require 'find'
2
2
  require 'haml'
3
3
  require 'uri'
4
4
  require 'time'
5
+ require 'nokogiri'
5
6
 
6
7
  #
7
8
  # Cartographer is a sitemap manipulation library.
@@ -23,7 +24,7 @@ require 'time'
23
24
  # See Cartographer and Cartographer::URL RDoc for more information.
24
25
  #
25
26
  class Cartographer
26
- VERSION = '1.0.0'
27
+ VERSION = '1.1.0'
27
28
 
28
29
  ##
29
30
  # List of Cartographer::URL objects
@@ -97,12 +98,40 @@ class Cartographer
97
98
 
98
99
  ##
99
100
  #
100
- # Transforms the given Cartographer::URL list to the sitemap XML format.
101
+ # Transforms the given Cartographer::URL list to the sitemap XML format.
102
+ #
103
+ # Be aware this does not alter the root URL structure of these elements, so
104
+ # you may have mixed sitemap content if you do not rewrite the URLs.
101
105
  #
102
106
  def to_xml
103
107
  Haml::Engine.new(File.read(XML_TEMPLATE)).render(self)
104
108
  end
105
109
 
110
+ ##
111
+ #
112
+ # Given a string containing sitemap.xml content, parses it and adds each item
113
+ # to the URL list.
114
+ #
115
+ def from_xml(xml)
116
+ xml = Nokogiri::XML.parse(xml)
117
+ xml.root.xpath('xmlns:url').each do |url|
118
+ add Cartographer::URL.new(
119
+ :location => URI.parse(url.xpath('xmlns:loc').first.content),
120
+ :lastmod => (Time.parse(url.xpath('xmlns:lastmod').first.content) rescue Time.now),
121
+ :changefreq => (url.xpath('xmlns:changefreq').first.content rescue nil),
122
+ :priority => (url.xpath('xmlns:priority').first.content rescue nil)
123
+ )
124
+ end
125
+ end
126
+
127
+ ##
128
+ #
129
+ # Uniq's the URL list. This operation is destructive.
130
+ #
131
+ def uniq!
132
+ urls.uniq!
133
+ end
134
+
106
135
  ##
107
136
  # Cartographer::URL is a structure to enforce the sitemap format.
108
137
  #
@@ -125,7 +154,6 @@ class Cartographer
125
154
  # Priority, stored as a Numeric object, from 0 to 1 with decimal values included.
126
155
  attr_accessor :priority
127
156
 
128
-
129
157
  ##
130
158
  #
131
159
  # Constructor. Takes a hash of arguments:
@@ -168,5 +196,21 @@ class Cartographer
168
196
  def to_xml
169
197
  Haml::Engine.new(File.read(XML_TEMPLATE)).render(self)
170
198
  end
199
+
200
+ ##
201
+ # Computes a hash based off the URI.
202
+ def hash
203
+ location.hash
204
+ end
205
+
206
+ ##
207
+ #
208
+ # Compares two locations and returns true/false.
209
+ #
210
+ def eql?(url2)
211
+ location == url2.location
212
+ end
213
+
214
+ alias == eql?
171
215
  end
172
216
  end
@@ -44,6 +44,19 @@ class TestCartographer < MiniTest::Unit::TestCase
44
44
  sm.urls.map(&:location),
45
45
  URI.parse("http://example.org/test_cartographer/")
46
46
  )
47
+
48
+ require 'fileutils'
49
+ FileUtils.mkdir_p("test/prune_me")
50
+
51
+ sm = Cartographer.new("http://example.org")
52
+ sm.add_tree(File.expand_path("test")) { |x| x =~ /prune_me/ }
53
+
54
+ refute_includes(
55
+ sm.urls.map(&:location),
56
+ URI.parse("http://example.org/prune_me/")
57
+ )
58
+
59
+ FileUtils.rm_r("test/prune_me")
47
60
  end
48
61
 
49
62
  def test_to_xml
@@ -71,4 +84,36 @@ class TestCartographer < MiniTest::Unit::TestCase
71
84
  assert_equal [ "http://example.org/", "http://example.org/test_cartographer/" ],
72
85
  xml.root.xpath('//xmlns:loc', xml.root.namespaces).map(&:content)
73
86
  end
87
+
88
+ def test_from_xml
89
+ sm = Cartographer.new("http://example.org")
90
+ sm.add_tree(File.expand_path("test"))
91
+
92
+ assert_includes sm.urls.map { |x| x.location.path }, "/test_cartographer.rb"
93
+ assert_equal 1, sm.urls.select { |x| x.location.path == "/test_cartographer.rb" }.length
94
+
95
+ sm2 = Cartographer.new("http://example.org")
96
+ sm2.from_xml(sm.to_xml)
97
+
98
+ assert_includes sm2.urls.map { |x| x.location.path }, "/test_cartographer.rb"
99
+ assert_equal 1, sm2.urls.select { |x| x.location.path == "/test_cartographer.rb" }.length
100
+ end
101
+
102
+ def test_uniq!
103
+ sm = Cartographer.new("http://example.org")
104
+ sm.add_tree(File.expand_path("test"))
105
+
106
+ assert_includes sm.urls.map { |x| x.location.path }, "/test_cartographer.rb"
107
+ assert_equal 1, sm.urls.select { |x| x.location.path == "/test_cartographer.rb" }.length
108
+
109
+ sm.from_xml(sm.to_xml)
110
+
111
+ assert_includes sm.urls.map { |x| x.location.path }, "/test_cartographer.rb"
112
+ assert_equal 2, sm.urls.select { |x| x.location.path == "/test_cartographer.rb" }.length
113
+
114
+ sm.uniq!
115
+
116
+ assert_includes sm.urls.map { |x| x.location.path }, "/test_cartographer.rb"
117
+ assert_equal 1, sm.urls.select { |x| x.location.path == "/test_cartographer.rb" }.length
118
+ end
74
119
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: cartographer
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.0
5
+ version: 1.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Erik Hollensbe
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-03-26 00:00:00 -04:00
14
+ date: 2011-03-28 00:00:00 -04:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -26,7 +26,7 @@ dependencies:
26
26
  type: :runtime
27
27
  version_requirements: *id001
28
28
  - !ruby/object:Gem::Dependency
29
- name: minitest
29
+ name: nokogiri
30
30
  prerelease: false
31
31
  requirement: &id002 !ruby/object:Gem::Requirement
32
32
  none: false
@@ -34,10 +34,10 @@ dependencies:
34
34
  - - ">="
35
35
  - !ruby/object:Gem::Version
36
36
  version: "0"
37
- type: :development
37
+ type: :runtime
38
38
  version_requirements: *id002
39
39
  - !ruby/object:Gem::Dependency
40
- name: hoe-roodi
40
+ name: minitest
41
41
  prerelease: false
42
42
  requirement: &id003 !ruby/object:Gem::Requirement
43
43
  none: false
@@ -48,7 +48,7 @@ dependencies:
48
48
  type: :development
49
49
  version_requirements: *id003
50
50
  - !ruby/object:Gem::Dependency
51
- name: hoe-reek
51
+ name: hoe-roodi
52
52
  prerelease: false
53
53
  requirement: &id004 !ruby/object:Gem::Requirement
54
54
  none: false
@@ -59,7 +59,7 @@ dependencies:
59
59
  type: :development
60
60
  version_requirements: *id004
61
61
  - !ruby/object:Gem::Dependency
62
- name: hoe-git
62
+ name: hoe-reek
63
63
  prerelease: false
64
64
  requirement: &id005 !ruby/object:Gem::Requirement
65
65
  none: false
@@ -70,7 +70,7 @@ dependencies:
70
70
  type: :development
71
71
  version_requirements: *id005
72
72
  - !ruby/object:Gem::Dependency
73
- name: nokogiri
73
+ name: hoe-git
74
74
  prerelease: false
75
75
  requirement: &id006 !ruby/object:Gem::Requirement
76
76
  none: false