Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: reyesyang/sitemaper
base: 211b39bb1a
...
head fork: reyesyang/sitemaper
compare: c8f4e79143
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
View
11 .gitignore
@@ -2,8 +2,12 @@
*.rbc
.bundle
.config
-coverage
+.yardoc
+Gemfile.lock
InstalledFiles
+_yardoc
+coverage
+doc/
lib/bundler/man
pkg
rdoc
@@ -11,8 +15,3 @@ spec/reports
test/tmp
test/version_tmp
tmp
-
-# YARD artifacts
-.yardoc
-_yardoc
-doc/
View
4 Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in sitemaper.gemspec
+gemspec
View
22 LICENSE.txt
@@ -0,0 +1,22 @@
+Copyright (c) 2014 Reyes Yang
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
29 README.md
@@ -1,4 +1,29 @@
-sitemaper
-=========
+# Sitemaper(Comming soon)
Some rake tasks used to generate sitemap
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'sitemaper'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install sitemaper
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Contributing
+
+1. Fork it ( http://github.com/<my-github-username>/sitemaper/fork )
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Add some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
View
7 Rakefile
@@ -0,0 +1,7 @@
+require "bundler/gem_tasks"
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new
+
+task default: :spec
+task test: :spec
View
5 lib/sitemaper.rb
@@ -0,0 +1,5 @@
+require "sitemaper/version"
+
+module Sitemaper
+ # Your code goes here...
+end
View
69 lib/sitemaper/sitemap_file.rb
@@ -0,0 +1,69 @@
+require 'builder'
+
+module Sitemaper
+ class ContentOverflowError < StandardError; end
+
+ class SitemapFile
+ attr_accessor :file_path, :items_count, :items_content, :reserved_content, :bytesize
+
+ def initialize(file_path)
+ @file_path = file_path
+ @items_count = 0
+ @items_content = ''
+ @reserved_content = build_reserved_content
+ @bytesize = @reserved_content.bytesize
+ end
+
+ def content
+ @reserved_content.gsub /(<\/\w+?>)$/, "#{@items_content}\\1"
+ end
+
+ def add(options)
+ item_str = build_item_xml(options).target!
+ @bytesize += item_str.bytesize
+ @items_count += 1
+
+ if !overflow?
+ @items_content << item_str
+ else
+ @bytesize -= item_str.bytesize
+ @items_count -= 1
+ nil
+ end
+ end
+
+ def generate!
+ if !overflow?
+ File.open(@file_path, 'w') { |file| file.write content }
+ @file_path
+ else
+ nil
+ end
+ end
+
+ private
+
+ def overflow?
+ @bytesize > 10 * 1024 * 1024 || @items_count > 50000
+ end
+
+ def build_reserved_content
+ xml = Builder::XmlMarkup.new
+ xml.instruct!
+ xml.urlset(xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9") { |urlset| urlset }
+ xml.target!
+ end
+
+ def build_item_xml(options)
+ xml = Builder::XmlMarkup.new
+ xml.url do |url|
+ url.loc options[:loc]
+ url.lastmod options[:lastmod]
+ url.changefreq(options[:changefreq] || 'weekly')
+ url.priority(options[:priority] || 0.5)
+ end
+
+ xml
+ end
+ end
+end
View
3  lib/sitemaper/version.rb
@@ -0,0 +1,3 @@
+module Sitemaper
+ VERSION = "0.0.1"
+end
View
26 sitemaper.gemspec
@@ -0,0 +1,26 @@
+# coding: utf-8
+lib = File.expand_path('../lib', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+require 'sitemaper/version'
+
+Gem::Specification.new do |spec|
+ spec.name = "sitemaper"
+ spec.version = Sitemaper::VERSION
+ spec.authors = ["Reyes Yang"]
+ spec.email = ["reyes.yang@gmail.com"]
+ spec.summary = %q{A ruby tool used to generate sitemap.}
+ spec.description = %q{A ruby tool used to generate sitemap.}
+ spec.homepage = ""
+ spec.license = "MIT"
+
+ spec.files = `git ls-files -z`.split("\x0")
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
+ spec.require_paths = ["lib"]
+
+ spec.add_development_dependency "bundler", "~> 1.5"
+ spec.add_development_dependency "rake"
+ spec.add_development_dependency "rspec"
+
+ spec.add_runtime_dependency "builder"
+end
View
67 spec/sitemaper/sitemap_file_spec.rb
@@ -0,0 +1,67 @@
+require 'sitemaper/sitemap_file'
+
+describe Sitemaper::SitemapFile do
+ subject(:sitemap_file) { Sitemaper::SitemapFile.new('/tmp/sitemap.xml') }
+ let(:reserved_content) do
+ '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>'
+ end
+ let(:items_content) do
+ '<url><loc>http://reyesyang.info</loc><lastmod>1988-2-29</lastmod><changefreq>weekly</changefreq><priority>0.5</priority></url>'
+ end
+ let(:content) do
+ '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>http://reyesyang.info</loc><lastmod>1988-2-29</lastmod><changefreq>weekly</changefreq><priority>0.5</priority></url></urlset>'
+ end
+
+ let(:options) do
+ {
+ loc: 'http://reyesyang.info',
+ lastmod: '1988-2-29'
+ }
+ end
+
+ describe '#initialize' do
+ its(:file_path) { should eq('/tmp/sitemap.xml') }
+ its(:items_count) { should eq(0) }
+ its(:items_content) { should eq('') }
+ its(:reserved_content) { should eq(reserved_content) }
+ its(:bytesize) { should eq(reserved_content.bytesize) }
+ end
+
+ describe '#content' do
+ context 'items_content is blank' do
+ it 'return reserved_content' do
+ expect(sitemap_file.content).to eq(reserved_content)
+ end
+ end
+
+ context 'items_content is not blank' do
+ it 'return reserved_content with items_content' do
+ sitemap_file.items_content = items_content
+ expect(sitemap_file.content).to eq(content)
+ end
+ end
+ end
+
+ describe '#add' do
+ context 'fit limit' do
+ it 'return items_content' do
+ expect(sitemap_file.add(options)).to eq(items_content)
+ end
+ end
+
+ context 'over limit' do
+ it 'return nil when bytesize more than 10M' do
+ subject.bytesize = 10 * 1024 * 1024
+ expect(sitemap_file.add(options)).to be_nil
+ end
+
+ it 'return nil when items_count more than 50000' do
+ subject.items_count = 50000
+ expect(sitemap_file.add(options)).to be_nil
+ end
+ end
+ end
+
+ describe '#generate!' do
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.