sitemaps_parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 84077b93f96854c50072c350407fb253b9e0a952
4
+ data.tar.gz: 6b1437162a1e98e3e882815cabea1ab5e4b1dc90
5
+ SHA512:
6
+ metadata.gz: 1e705e71f275f333a98dee425c0c11eddcab08b502233e0500328e926f3570227549ef7867206ad03dcd292a932b477b71592123b7ab456594c981dac5e624fb
7
+ data.tar.gz: 0decafa6d29c0785d01f2099c64691678df91123986e5f075decf3c5b09d55738195aa6d53c787dfbd757b39462c4df245711c5b1ce90640a88afdc89a28854c
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,17 @@
1
+ Metrics/LineLength:
2
+ Enabled: false
3
+
4
+ Metrics/AbcSize:
5
+ Enabled: false
6
+
7
+ Metrics/MethodLength:
8
+ Enabled: false
9
+
10
+ Style/StringLiterals:
11
+ Enabled: false
12
+
13
+ Style/RescueModifier:
14
+ Enabled: false
15
+
16
+ Lint/ShadowingOuterLocalVariable:
17
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at jon@accidental.cc. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sitemaps.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 TermScout
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Sitemaps
2
+
3
+ Discover, retrieve and parse XML sitemaps, according to the spec at [sitemaps.org](http://sitemaps.org).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sitemaps'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sitemaps
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'sitemaps'
25
+
26
+ # parse a sitemap from a string
27
+ Sitemaps.parse("<xml ns=\"...")
28
+
29
+ # fetch and parse a sitemap from a known url
30
+ Sitemaps.fetch("http://google.com/sitemap.xml", recurse: true, fetch: :builtin)
31
+
32
+ # attempt to discover a sitemap url for a domain
33
+ # parallel fetches a small set of 'normal' urls, returns whichever comes back first, most likely only one.
34
+ xml = Sitemaps.discover("http://example.com")
35
+ if url.present?
36
+ sitemap = Sitemaps.parse(xml)
37
+ end
38
+
39
+ # sitemap usage
40
+
41
+ sitemap.entries.first #> Struct(loc: 'http://example.com/page', lastmod: DateTime.utc, changefreq: :monthly, priority: 0.5)
42
+
43
+ urls = sitemap.entries.map(&:loc)
44
+ ```
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/termscout/sitemaps. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sitemaps"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/sitemaps.rb ADDED
@@ -0,0 +1,60 @@
1
+ require "active_support/core_ext/object/blank"
2
+
3
+ # Discover, fetch and parse XML sitemaps as defined by the `http://sitemaps.org` spec.
4
+ module Sitemaps
5
+ Entry = Struct.new(:loc, :lastmod, :changefreq, :priority)
6
+ Submap = Struct.new(:loc, :lastmod)
7
+ Sitemap = Struct.new(:entries, :sitemaps)
8
+
9
+ def self.parse(source)
10
+ Sitemaps::Parser.parse(source)
11
+ end
12
+
13
+ def self.fetch(url, fetch: nil, recurse: true)
14
+ fetch ||= -> (url) { Sitemaps::Fetcher.fetch(url) }
15
+ recurse ? fetch_recursive(url, fetch) : fetch_single(url, fetch)
16
+ end
17
+
18
+ def self.fetch_single(url, fetch)
19
+ source = fetch.call(parse_url(url))
20
+ Sitemaps::Parser.parse(source)
21
+ end
22
+
23
+ def self.fetch_recursive(url, fetch)
24
+ queue = [url]
25
+ maps = {}
26
+
27
+ # walk the queue, fetching the sitemap requested and adding
28
+ # new sitemaps to the queue as found
29
+ loop do
30
+ begin
31
+ url = queue.pop
32
+ break if url.nil?
33
+ next unless maps[url].nil?
34
+
35
+ maps[url] = fetch_single(url, fetch)
36
+ queue.push(*maps[url].sitemaps.map(&:loc))
37
+ rescue => ex
38
+ $stderr.puts "ERROR FETCHING: #{url}, #{ex.message}, ignoring..."
39
+ next
40
+ end
41
+ end
42
+
43
+ # collapse the recovered maps into a single one with everything
44
+ maps.each_with_object(Sitemap.new([], [])) do |(_, map), result|
45
+ result.sitemaps.concat(map.sitemaps)
46
+ result.entries.concat(map.entries)
47
+ end
48
+ end
49
+
50
+ def self.parse_url(url)
51
+ return url if url.is_a? URI
52
+
53
+ url = "http://#{url}" unless url =~ /^https?/
54
+ URI.parse(url)
55
+ end
56
+ end
57
+
58
+ require "sitemaps/version"
59
+ require "sitemaps/parser"
60
+ require "sitemaps/fetcher"
@@ -0,0 +1,42 @@
1
+ module Sitemaps
2
+ # Simple single purpose HTTP client
3
+ module Fetcher
4
+ class FetchError < StandardError; end
5
+ class MaxRedirectError < StandardError; end
6
+
7
+ @max_attempts = 10
8
+
9
+ def self.fetch(uri)
10
+ attempts = 0
11
+
12
+ until attempts >= @max_attempts
13
+ resp = Net::HTTP.get_response(uri)
14
+
15
+ # on a good 2xx response, return the body
16
+ if resp.code.to_s =~ /2\d\d/
17
+ if resp.header["Content-Encoding"].blank? && uri.path =~ /\.gz$/
18
+ return Zlib::GzipReader.new(StringIO.new(resp.body)).read
19
+ else
20
+ return resp.body
21
+ end
22
+
23
+ # on a 3xx response, handle the redirect
24
+ elsif resp.code.to_s =~ /3\d\d/
25
+ location = URI.parse(resp.header['location'])
26
+ location = uri + resp.header['location'] if location.relative?
27
+
28
+ uri = location
29
+ attempts += 1
30
+ next
31
+
32
+ # otherwise (4xx, 5xx) throw an exception
33
+ else
34
+ fail FetchError, "Failed to fetch URI, #{uri}, failed with response code: #{resp.code}"
35
+ end
36
+ end
37
+
38
+ # if we got here, we ran out of attempts
39
+ fail MaxRedirectError, "Failed to fetch URI #{uri}, redirected too many times" if attempts >= @max_attempts
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,51 @@
1
+ module Sitemaps
2
+ # Parse XML Sitemaps
3
+ module Parser
4
+ require "time"
5
+ require "rexml/document"
6
+ require "active_support"
7
+ require "active_support/core_ext/object/try"
8
+
9
+ def self.parse(source)
10
+ document = REXML::Document.new(source)
11
+ entries = document.elements.to_a("/urlset/url").map do |root|
12
+ loc = parse_loc(root) || next
13
+ mod = parse_lastmod(root)
14
+ freq = parse_changefreq(root)
15
+ pri = parse_priority(root)
16
+
17
+ Sitemaps::Entry.new(loc, mod, freq, pri)
18
+ end.reject(&:nil?)
19
+
20
+ sitemaps = document.elements.to_a("/sitemapindex/sitemap").map do |root|
21
+ loc = parse_loc(root) || next
22
+ mod = parse_lastmod(root)
23
+
24
+ Sitemaps::Submap.new(loc, mod)
25
+ end.reject(&:nil?)
26
+
27
+ Sitemaps::Sitemap.new(entries, sitemaps)
28
+ end
29
+
30
+ def self.parse_loc(root)
31
+ loc = root.get_text("loc").try(:value)
32
+ loc && URI.parse(loc) rescue nil
33
+ end
34
+
35
+ def self.parse_lastmod(root)
36
+ mod = root.get_text("lastmod").try(:value)
37
+ mod && Time.parse(mod) rescue nil
38
+ end
39
+
40
+ VALID_CHANGEFREQ = %w(always hourly daily weekly monthly yearly never).freeze
41
+ def self.parse_changefreq(root)
42
+ freq = root.get_text("changefreq").try(:value)
43
+ freq && VALID_CHANGEFREQ.include?(freq) ? freq.to_sym : nil
44
+ end
45
+
46
+ def self.parse_priority(root)
47
+ priority = root.get_text("priority").try(:value) || "0.5"
48
+ priority && Float(priority) rescue 0.5 # default priority according to spec
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module Sitemaps
2
+ VERSION = "0.1.0".freeze
3
+ end
data/sitemaps.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sitemaps/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sitemaps_parser"
8
+ spec.version = Sitemaps::VERSION
9
+ spec.authors = ["Jonathan Raphaelson"]
10
+ spec.email = ["jraphaelson@termscout.com"]
11
+
12
+ spec.summary = "Retrieve and parse sitemaps, according to the sitemaps.org spec."
13
+ spec.homepage = "http://github.com/termscout/sitemaps"
14
+ spec.license = "MIT"
15
+
16
+ files = `git ls-files -z`.split("\x0")
17
+ files.reject! { |f| f.match(%r{^(test|spec|features)/}) }
18
+
19
+ spec.files = files
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "webmock", "~> 1"
26
+ spec.add_development_dependency "vcr", "~> 3"
27
+ spec.add_development_dependency "rubocop", "~> 0.38.0"
28
+ spec.add_development_dependency "byebug", "~> 8.2"
29
+
30
+ spec.add_runtime_dependency "activesupport", "~> 4"
31
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sitemaps_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Raphaelson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: vcr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.38.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.38.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '8.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '8.2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activesupport
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '4'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '4'
125
+ description:
126
+ email:
127
+ - jraphaelson@termscout.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".travis.yml"
136
+ - CODE_OF_CONDUCT.md
137
+ - Gemfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - lib/sitemaps.rb
144
+ - lib/sitemaps/fetcher.rb
145
+ - lib/sitemaps/parser.rb
146
+ - lib/sitemaps/version.rb
147
+ - sitemaps.gemspec
148
+ homepage: http://github.com/termscout/sitemaps
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.2.2
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Retrieve and parse sitemaps, according to the sitemaps.org spec.
172
+ test_files: []