pj_nitin-big_sitemap 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/big_sitemap/builder.rb +124 -0
  2. metadata +2 -1
@@ -0,0 +1,124 @@
1
+ require 'builder'
2
+ require 'zlib'
3
+
4
+ class BigSitemap
5
+ class Builder < Builder::XmlMarkup
6
+ NAMESPACE = 'http://www.sitemaps.org/schemas/sitemap/0.9'
7
+ MAX_URLS = 50000
8
+
9
+ def initialize(options)
10
+ @gzip = options.delete(:gzip)
11
+ @max_urls = options.delete(:max_urls) || MAX_URLS
12
+ @index = options.delete(:index)
13
+ @paths = []
14
+ @parts = 0
15
+
16
+ if @filename = options.delete(:filename)
17
+ options[:target] = _get_writer
18
+ end
19
+
20
+ super(options)
21
+
22
+ @opened_tags = []
23
+ _init_document
24
+ end
25
+
26
+ def add_url!(url, time = nil, frequency = nil, priority = nil)
27
+ _rotate if @max_urls == @urls
28
+
29
+ tag!(@index ? 'sitemap' : 'url') do
30
+ loc url
31
+ # W3C format is the subset of ISO 8601
32
+ lastmod(time.utc.strftime('%Y-%m-%dT%H:%M:%S+00:00')) unless time.nil?
33
+ changefreq(frequency) unless frequency.nil?
34
+ priority(priority) unless priority.nil?
35
+ end
36
+ @urls += 1
37
+ end
38
+
39
+ def close!
40
+ _close_document
41
+ target!.close if target!.respond_to?(:close)
42
+ end
43
+
44
+ def paths!
45
+ @paths
46
+ end
47
+
48
+ private
49
+
50
+ def _get_writer
51
+ if @filename
52
+ filename = @filename.dup
53
+ filename << "_#{@parts}" if @parts > 0
54
+ filename << '.xml'
55
+ filename << '.gz' if @gzip
56
+ _open_writer(filename)
57
+ else
58
+ target!
59
+ end
60
+ end
61
+
62
+ def _open_writer(filename)
63
+ file = File.open(filename, 'w+')
64
+ @paths << filename
65
+ @gzip ? Zlib::GzipWriter.new(file) : file
66
+ end
67
+
68
+ def _init_document
69
+ @urls = 0
70
+ instruct!
71
+ _open_tag(@index ? 'sitemapindex' : 'urlset', :xmlns => NAMESPACE)
72
+ end
73
+
74
+ def _rotate
75
+ # write out the current document and start writing into a new file
76
+ close!
77
+ @parts += 1
78
+ @target = _get_writer
79
+ _init_document
80
+ end
81
+
82
+ # add support for:
83
+ # xml.open_foo!(attrs)
84
+ # xml.close_foo!
85
+ def method_missing(method, *args, &block)
86
+ if method.to_s =~ /^(open|close)_(.+)!$/
87
+ operation, name = $1, $2
88
+ name = "#{name}:#{args.shift}" if Symbol === args.first
89
+
90
+ if 'open' == operation
91
+ _open_tag(name, args.first)
92
+ else
93
+ _close_tag(name)
94
+ end
95
+ else
96
+ super
97
+ end
98
+ end
99
+
100
+ # opens a tag, bumps up level but doesn't require a block
101
+ def _open_tag(name, attrs)
102
+ _indent
103
+ _start_tag(name, attrs)
104
+ _newline
105
+ @level += 1
106
+ @opened_tags << name
107
+ end
108
+
109
+ # closes a tag block by decreasing the level and inserting a close tag
110
+ def _close_tag(name)
111
+ @opened_tags.pop
112
+ @level -= 1
113
+ _indent
114
+ _end_tag(name)
115
+ _newline
116
+ end
117
+
118
+ def _close_document
119
+ for name in @opened_tags.reverse
120
+ _close_tag(name)
121
+ end
122
+ end
123
+ end
124
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pj_nitin-big_sitemap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Rabarts
@@ -46,6 +46,7 @@ files:
46
46
  - README.rdoc
47
47
  - VERSION.yml
48
48
  - lib/big_sitemap.rb
49
+ - lib/big_sitemap/builder.rb
49
50
  - test/big_sitemap_test.rb
50
51
  - test/fixtures
51
52
  - test/fixtures/test_model.rb