taq-sitemapper 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .*.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sitemapper.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module SiteMapper
2
+ VERSION = "0.0.3"
3
+ end
data/lib/sitemapper.rb ADDED
@@ -0,0 +1,53 @@
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
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "sitemapper/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "taq-sitemapper"
7
+ s.version = SiteMapper::VERSION
8
+ s.authors = ["Eustaquio Rangel"]
9
+ s.email = ["eustaquiorangel@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Create a sitemap}
12
+ s.description = %q{Create a sitemap with a data collection}
13
+
14
+ s.rubyforge_project = "sitemapper"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "nokogiri"
23
+ s.add_runtime_dependency "builder"
24
+ end
@@ -0,0 +1,40 @@
1
+ require "nokogiri"
2
+ require "test/unit"
3
+ require "#{File.expand_path(File.dirname(__FILE__))}/collections.rb"
4
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/sitemapper.rb"
5
+
6
+ class SiteMapperTest < Test::Unit::TestCase
7
+ def setup
8
+ @arraytest = [{url: "One" , lastmod: Time.now, freq: "monthly", priority: 1},
9
+ {url: "Two" , lastmod: Time.now, freq: "monthly", priority: 1},
10
+ {url: "Three", lastmod: Time.now, freq: "monthly", priority: 1}]
11
+
12
+ @sitemapper = SiteMapper::SiteMapper.new
13
+ @sitemapper.url = "http://localhost.com/sitemap.xml"
14
+ @sitemapper.loc = :url
15
+ @sitemapper.changefreq = :freq
16
+ @sitemapper.priority = :priority
17
+ @sitemapper.sitemap = "/tmp/sitemap.xml"
18
+ end
19
+
20
+ def test_write_sitemap
21
+ File.unlink(@sitemapper.sitemap) if File.exists?(@sitemapper.sitemap)
22
+ @sitemapper.write_sitemap(@arraytest.entries)
23
+ assert File.exists?(@sitemapper.sitemap), "sitemap file not found"
24
+ end
25
+
26
+ def test_url_size
27
+ test_write_sitemap
28
+ doc = Nokogiri::XML(File.open(@sitemapper.sitemap))
29
+ assert_equal @arraytest.entries.size, doc.search("url").size
30
+ end
31
+
32
+ def test_write_sitemap_with_extras
33
+ File.unlink(@sitemapper.sitemap) if File.exists?(@sitemapper.sitemap)
34
+ extras = [{url: "Four", lastmod: Time.now, freq: "yearly", priority: 0.5},
35
+ {url: "Five", lastmod: Time.now, freq: "yearly", priority: 0.5}]
36
+ @sitemapper.write_sitemap(@arraytest.entries,extras.entries)
37
+ doc = Nokogiri::XML(File.open(@sitemapper.sitemap))
38
+ assert_equal @arraytest.entries.size+extras.size, doc.search("url").size
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: taq-sitemapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eustaquio Rangel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-28 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: &82914730 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *82914730
25
+ - !ruby/object:Gem::Dependency
26
+ name: builder
27
+ requirement: &82914470 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *82914470
36
+ description: Create a sitemap with a data collection
37
+ email:
38
+ - eustaquiorangel@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - Rakefile
46
+ - lib/sitemapper.rb
47
+ - lib/sitemapper/version.rb
48
+ - sitemapper.gemspec
49
+ - test/sitemapper_test.rb
50
+ homepage: ''
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project: sitemapper
70
+ rubygems_version: 1.8.10
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Create a sitemap
74
+ test_files: []