Skip to content

Commit 2d1e8aa

Browse files
committed
Add better logging and get_latest_version implementations for 10 scrapers
1 parent d0300dd commit 2d1e8aa

File tree

13 files changed

+128
-21
lines changed

13 files changed

+128
-21
lines changed

lib/docs/core/scraper.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def options
132132
end
133133
end
134134

135-
def get_latest_version
135+
def get_latest_version(&block)
136136
raise NotImplementedError
137137
end
138138

@@ -231,6 +231,38 @@ def additional_options
231231
{}
232232
end
233233

234+
# Utility methods for get_latest_version
235+
236+
def fetch(url, &block)
237+
Request.run(url) do |response|
238+
if response.success?
239+
block.call response.body
240+
else
241+
block.call nil
242+
end
243+
end
244+
end
245+
246+
def fetch_doc(url, &block)
247+
fetch(url) do |body|
248+
parser = Parser.new(body)
249+
block.call parser.html
250+
end
251+
end
252+
253+
def fetch_json(url, &block)
254+
fetch(url) do |body|
255+
json = JSON.parse(body)
256+
block.call json
257+
end
258+
end
259+
260+
def get_npm_version(package, &block)
261+
fetch_json("https://registry.npmjs.com/#{package}") do |json|
262+
block.call json['dist-tags']['latest']
263+
end
264+
end
265+
234266
module FixInternalUrlsBehavior
235267
def self.included(base)
236268
base.extend ClassMethods

lib/docs/scrapers/angular.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ def handle_response(response)
155155
end
156156
end
157157

158+
def get_latest_version(&block)
159+
get_npm_version('@angular/core', &block)
160+
end
161+
158162
private
159163

160164
def parse(response)

lib/docs/scrapers/angularjs.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,9 @@ class Angularjs < UrlScraper
6969
self.release = '1.2.32'
7070
self.base_url = "https://code.angularjs.org/#{release}/docs/partials/"
7171
end
72+
73+
def get_latest_version(&block)
74+
get_npm_version('angular', &block)
75+
end
7276
end
7377
end

lib/docs/scrapers/ansible.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,11 @@ class Ansible < UrlScraper
8787
quickstart.html
8888
list_of_all_modules.html)
8989
end
90+
91+
def get_latest_version(&block)
92+
fetch_doc('https://docs.ansible.com/ansible/latest/index.html') do |doc|
93+
block.call doc.at_css('.DocSiteProduct-CurrentVersion').content.strip
94+
end
95+
end
9096
end
9197
end

lib/docs/scrapers/apache.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,11 @@ class Apache < UrlScraper
3333
&copy; 2018 The Apache Software Foundation<br>
3434
Licensed under the Apache License, Version 2.0.
3535
HTML
36+
37+
def get_latest_version(&block)
38+
fetch_doc('http://httpd.apache.org/docs/') do |doc|
39+
block.call doc.at_css('#apcontents > ul a')['href'][0...-1]
40+
end
41+
end
3642
end
3743
end

lib/docs/scrapers/apache_pig.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,11 @@ class ApachePig < UrlScraper
4343
self.base_url = "https://pig.apache.org/docs/r#{release}/"
4444
end
4545

46+
def get_latest_version(&block)
47+
fetch_doc('https://pig.apache.org/') do |doc|
48+
item = doc.at_css('div[id="menu_1.2"] > .menuitem:last-child')
49+
block.call item.content.strip.sub(/Release /, '')
50+
end
51+
end
4652
end
4753
end

lib/docs/scrapers/async.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@ class Async < UrlScraper
1717
&copy; 2010&ndash;2018 Caolan McMahon<br>
1818
Licensed under the MIT License.
1919
HTML
20+
21+
def get_latest_version(&block)
22+
fetch_doc('https://caolan.github.io/async/') do |doc|
23+
version = doc.at_css('#version-dropdown > a').content.strip[1..-1]
24+
block.call version
25+
end
26+
end
2027
end
2128
end

lib/docs/scrapers/babel.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,11 @@ class Babel < UrlScraper
2222
stub '' do
2323
'<div></div>'
2424
end
25+
26+
def get_latest_version(&block)
27+
fetch_doc('https://babeljs.io/docs/en/') do |doc|
28+
block.call doc.at_css('a[href="/versions"] > h3').content
29+
end
30+
end
2531
end
2632
end

lib/docs/scrapers/backbone.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,12 @@ class Backbone < UrlScraper
2020
&copy; 2010&ndash;2016 Jeremy Ashkenas, DocumentCloud<br>
2121
Licensed under the MIT License.
2222
HTML
23+
24+
def get_latest_version(&block)
25+
fetch_doc('https://backbonejs.org/') do |doc|
26+
version = doc.at_css('.version').content
27+
block.call version[1...-1]
28+
end
29+
end
2330
end
2431
end

lib/docs/scrapers/bash.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@ class Bash < UrlScraper
1717
Copyright &copy; 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.<br>
1818
Licensed under the GNU Free Documentation License.
1919
HTML
20+
21+
def get_latest_version(&block)
22+
fetch('https://www.gnu.org/software/bash/manual/html_node/index.html') do |body|
23+
version = body.scan(/, Version ([0-9.]+)/)[0][0]
24+
block.call version[0...-1]
25+
end
26+
end
2027
end
2128
end

0 commit comments

Comments
 (0)