taq-sitemapper 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,12 @@
1
+ This is a simple class to write a website sitemap file.
2
+ You can use it on two ways:
3
+
4
+ As a class
5
+
6
+ You can create a class and tell it to write the sitemap file when needed,
7
+ sending it a collection with the data to be written and optionally another one
8
+ with extra data. The extra data can be used, for example, to send static urls
9
+ that are not part of the first collection.
10
+
11
+
12
+
@@ -0,0 +1,11 @@
1
+ require "rubygems"
2
+ require "taq-sitemapper"
3
+
4
+ data = [{url: "One" , lastmod: Time.now, freq: "monthly", priority: 1},
5
+ {url: "Two" , lastmod: Time.now, freq: "monthly", priority: 1},
6
+ {url: "Three", lastmod: Time.now, freq: "monthly", priority: 1}]
7
+
8
+ sitemapper = Sitemapper::Sitemapper.new
9
+ sitemapper.loc = :url
10
+ sitemapper.file = "/tmp/sitemap_class.xml"
11
+ sitemapper.write_sitemap(data.entries)
@@ -0,0 +1,86 @@
1
+ require "builder"
2
+ require "open-uri"
3
+
4
+ module SiteMapper
5
+ module BaseMethods
6
+ def write_sitemap(collection=nil,extra_collection=nil)
7
+ file_ref = @file || (@sitemap[:file] rescue nil)
8
+ loc_ref = @loc || (@sitemap[:loc] rescue nil)
9
+ lastmod_ref = @lastmod || (@sitemap[:lastmod] rescue nil)
10
+ changefreq_ref = @changefreq || (@sitemap[:changefreq] rescue nil)
11
+ priority_ref = @priority || (@sitemap[:priority] rescue nil)
12
+
13
+ return false if file_ref.nil?
14
+
15
+ if collection.nil? && !(@sitemap[:collection] rescue nil).nil?
16
+ collection = send(@sitemap[:collection])
17
+ end
18
+ if extra_collection.nil? && !(@sitemap[:extra] rescue nil).nil?
19
+ extra_collection = send(@sitemap[:extra])
20
+ end
21
+
22
+ xml = Builder::XmlMarkup.new(:indent=>2)
23
+ collection = extra_collection + collection if extra_collection
24
+
25
+ xml.urlset(:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9") do
26
+ collection.each do |value|
27
+ loc = value.respond_to?(loc_ref.to_s.to_sym) ? value.send(loc_ref) : value[loc_ref]
28
+ lastmod = value.respond_to?(lastmod_ref.to_s.to_sym) ? value.send(lastmod_ref) : value[lastmod_ref]
29
+ change = value.respond_to?(changefreq_ref.to_s.to_sym) ? value.send(changefreq_ref) : value[changefreq_ref]
30
+ priority = value.respond_to?(priority_ref.to_s.to_sym) ? value.send(priority_ref) : value[priority_ref]
31
+
32
+ change = changefreq_ref if !change
33
+ priority = priority_ref if !priority
34
+
35
+ xml.url do
36
+ xml.loc loc
37
+ xml.lastmod lastmod
38
+ xml.changefreq change
39
+ xml.priority priority
40
+ end
41
+ end
42
+ end
43
+ content = xml.target!
44
+ File.open(file_ref,"w") do |file|
45
+ file << content
46
+ end
47
+ content
48
+ end
49
+
50
+ def ping
51
+ url_ref = @url || (@sitemap[:url] rescue nil)
52
+ return if url_ref.nil?
53
+ open("http://www.google.com/webmasters/tools/ping?sitemap=#{url_ref}").read
54
+ end
55
+ end
56
+
57
+ def self.included(where)
58
+ where.extend(BaseMethods)
59
+ class << where
60
+ attr_accessor :sitemap
61
+ end
62
+ where.sitemap = {}
63
+ where.sitemap[:url] = nil
64
+ where.sitemap[:loc] = :loc
65
+ where.sitemap[:lastmod] = :lastmod
66
+ where.sitemap[:changefreq] = "daily"
67
+ where.sitemap[:priority] = 1.00
68
+ where.sitemap[:file] = "/tmp/sitemap.xml"
69
+ where.sitemap[:collection] = :all
70
+ where.sitemap[:extra] = nil
71
+ end
72
+
73
+ class SiteMapper
74
+ include BaseMethods
75
+ attr_accessor :url, :loc, :lastmod, :changefreq, :priority, :file
76
+
77
+ def initialize
78
+ @url = nil
79
+ @loc = :loc
80
+ @lastmod = :lastmod
81
+ @changefreq = "daily"
82
+ @priority = 1.00
83
+ @file = nil
84
+ end
85
+ end
86
+ end
@@ -1,3 +1,3 @@
1
1
  module SiteMapper
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,17 +1,17 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "sitemapper/version"
3
+ require "taq-sitemapper/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "taq-sitemapper"
7
7
  s.version = SiteMapper::VERSION
8
8
  s.authors = ["Eustaquio Rangel"]
9
9
  s.email = ["eustaquiorangel@gmail.com"]
10
- s.homepage = ""
10
+ s.homepage = "https://github.com/taq/sitemapper"
11
11
  s.summary = %q{Create a sitemap}
12
12
  s.description = %q{Create a sitemap with a data collection}
13
13
 
14
- s.rubyforge_project = "sitemapper"
14
+ s.rubyforge_project = "taq-sitemapper"
15
15
 
16
16
  s.files = `git ls-files`.split("\n")
17
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Binary file
@@ -1,8 +1,26 @@
1
+ require "rubygems"
1
2
  require "nokogiri"
2
3
  require "test/unit"
3
- require "#{File.expand_path(File.dirname(__FILE__))}/collections.rb"
4
+ require "active_record"
4
5
  require "#{File.expand_path(File.dirname(__FILE__))}/../lib/sitemapper.rb"
5
6
 
7
+ ActiveRecord::Base.establish_connection({
8
+ adapter: "sqlite3",
9
+ database: "sitemapper.sqlite"
10
+ })
11
+
12
+ class SitemapperTestAR < ActiveRecord::Base
13
+ include SiteMapper
14
+ def self.sitemap_extra
15
+ [{loc: "One" , lastmod: Time.now, freq: "monthly", priority: 1},
16
+ {loc: "Two" , lastmod: Time.now, freq: "monthly", priority: 1},
17
+ {loc: "Three", lastmod: Time.now, freq: "monthly", priority: 1}]
18
+ end
19
+ def loc
20
+ url
21
+ end
22
+ end
23
+
6
24
  class SiteMapperTest < Test::Unit::TestCase
7
25
  def setup
8
26
  @arraytest = [{url: "One" , lastmod: Time.now, freq: "monthly", priority: 1},
@@ -14,27 +32,74 @@ class SiteMapperTest < Test::Unit::TestCase
14
32
  @sitemapper.loc = :url
15
33
  @sitemapper.changefreq = :freq
16
34
  @sitemapper.priority = :priority
17
- @sitemapper.sitemap = "/tmp/sitemap.xml"
35
+ @sitemapper.file = "/tmp/sitemap.xml"
18
36
  end
19
37
 
20
38
  def test_write_sitemap
21
- File.unlink(@sitemapper.sitemap) if File.exists?(@sitemapper.sitemap)
39
+ File.unlink(@sitemapper.file) if File.exists?(@sitemapper.file)
22
40
  @sitemapper.write_sitemap(@arraytest.entries)
23
- assert File.exists?(@sitemapper.sitemap), "sitemap file not found"
41
+ assert File.exists?(@sitemapper.file), "sitemap file not found"
24
42
  end
25
43
 
26
44
  def test_url_size
27
45
  test_write_sitemap
28
- doc = Nokogiri::XML(File.open(@sitemapper.sitemap))
46
+ doc = Nokogiri::XML(File.open(@sitemapper.file))
29
47
  assert_equal @arraytest.entries.size, doc.search("url").size
30
48
  end
31
49
 
32
50
  def test_write_sitemap_with_extras
33
- File.unlink(@sitemapper.sitemap) if File.exists?(@sitemapper.sitemap)
51
+ File.unlink(@sitemapper.file) if File.exists?(@sitemapper.file)
34
52
  extras = [{url: "Four", lastmod: Time.now, freq: "yearly", priority: 0.5},
35
53
  {url: "Five", lastmod: Time.now, freq: "yearly", priority: 0.5}]
36
54
  @sitemapper.write_sitemap(@arraytest.entries,extras.entries)
37
- doc = Nokogiri::XML(File.open(@sitemapper.sitemap))
55
+ doc = Nokogiri::XML(File.open(@sitemapper.file))
38
56
  assert_equal @arraytest.entries.size+extras.size, doc.search("url").size
39
57
  end
58
+
59
+ def test_ar_class_methods
60
+ assert_respond_to SitemapperTestAR, :write_sitemap
61
+ assert_respond_to SitemapperTestAR, :ping
62
+ end
63
+
64
+ def test_ar_class_attrs
65
+ assert_respond_to SitemapperTestAR, :sitemap
66
+ end
67
+
68
+ def test_class_collection
69
+ assert_equal :all, SitemapperTestAR.sitemap[:collection]
70
+ end
71
+
72
+ def test_class_collection
73
+ assert_nil SitemapperTestAR.sitemap[:extra]
74
+ end
75
+
76
+ def test_rw_class_attrs
77
+ str = "yearly"
78
+ SitemapperTestAR.sitemap[:changefreq] = str
79
+ assert_equal str, SitemapperTestAR.sitemap[:changefreq]
80
+ end
81
+
82
+ def test_class_methods_default_values
83
+ assert_equal "daily", SitemapperTestAR.sitemap[:changefreq]
84
+ end
85
+
86
+ def test_write_sitemap_on_class
87
+ file = SitemapperTestAR.sitemap[:file]
88
+ File.unlink(file) if File.exists?(file)
89
+ SitemapperTestAR.write_sitemap
90
+ assert File.exists?(file), "sitemap file not found"
91
+ doc = Nokogiri::XML(File.open(file))
92
+ assert_equal SitemapperTestAR.all.size, doc.search("url").size
93
+ end
94
+
95
+ def test_write_sitemap_on_class_with_extras
96
+ file = SitemapperTestAR.sitemap[:file]
97
+ File.unlink(file) if File.exists?(file)
98
+ SitemapperTestAR.sitemap[:extra] = :sitemap_extra
99
+
100
+ SitemapperTestAR.write_sitemap
101
+ assert File.exists?(file), "sitemap file not found"
102
+ doc = Nokogiri::XML(File.open(file))
103
+ assert_equal SitemapperTestAR.all.size+3, doc.search("url").size
104
+ end
40
105
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taq-sitemapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-28 00:00:00.000000000Z
12
+ date: 2012-01-29 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &82914730 !ruby/object:Gem::Requirement
16
+ requirement: &78621310 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *82914730
24
+ version_requirements: *78621310
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: builder
27
- requirement: &82914470 !ruby/object:Gem::Requirement
27
+ requirement: &78620770 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *82914470
35
+ version_requirements: *78620770
36
36
  description: Create a sitemap with a data collection
37
37
  email:
38
38
  - eustaquiorangel@gmail.com
@@ -42,12 +42,15 @@ extra_rdoc_files: []
42
42
  files:
43
43
  - .gitignore
44
44
  - Gemfile
45
+ - README
45
46
  - Rakefile
46
- - lib/sitemapper.rb
47
- - lib/sitemapper/version.rb
48
- - sitemapper.gemspec
47
+ - doc/sitemapper_class.rb
48
+ - lib/taq-sitemapper.rb
49
+ - lib/taq-sitemapper/version.rb
50
+ - taq-sitemapper.gemspec
51
+ - test/sitemapper.sqlite
49
52
  - test/sitemapper_test.rb
50
- homepage: ''
53
+ homepage: https://github.com/taq/sitemapper
51
54
  licenses: []
52
55
  post_install_message:
53
56
  rdoc_options: []
@@ -66,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
69
  - !ruby/object:Gem::Version
67
70
  version: '0'
68
71
  requirements: []
69
- rubyforge_project: sitemapper
72
+ rubyforge_project: taq-sitemapper
70
73
  rubygems_version: 1.8.10
71
74
  signing_key:
72
75
  specification_version: 3
@@ -1,53 +0,0 @@
1
- require "builder"
2
- require "open-uri"
3
-
4
- module SiteMapper
5
- class SiteMapper
6
- attr_accessor :url, :loc, :lastmod, :changefreq, :priority, :sitemap
7
-
8
- def initialize
9
- @url = nil
10
- @loc = :loc
11
- @lastmod = :lastmod
12
- @changefreq = "daily"
13
- @priority = 1.00
14
- @sitemap = nil
15
- end
16
-
17
- def write_sitemap(collection,extra_collection=nil)
18
- return false if @sitemap.nil?
19
-
20
- xml = Builder::XmlMarkup.new(:indent=>2)
21
- collection = extra_collection + collection if extra_collection
22
-
23
- xml.urlset(:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9") do
24
- collection.each do |value|
25
- loc = value.respond_to?(@loc.to_s.to_sym) ? value.send(@loc) : value[@loc]
26
- lastmod = value.respond_to?(@lastmod.to_s.to_sym) ? value.send(@lastmod) : value[@lastmod]
27
- change = value.respond_to?(@changefreq.to_s.to_sym) ? value.send(@changefreq) : value[@changefreq]
28
- priority = value.respond_to?(@priority.to_s.to_sym) ? value.send(@priority) : value[@priority]
29
-
30
- change = @changefreq if !change
31
- priority = @priority if !priority
32
-
33
- xml.url do
34
- xml.loc loc
35
- xml.lastmod lastmod
36
- xml.changefreq change
37
- xml.priority priority
38
- end
39
- end
40
- end
41
- content = xml.target!
42
- File.open(sitemap,"w") do |file|
43
- file << content
44
- end
45
- content
46
- end
47
-
48
- def ping
49
- return if @url.nil?
50
- open("http://www.google.com/webmasters/tools/ping?sitemap=#{@url}").read
51
- end
52
- end
53
- end