Skip to content

Commit 41e0a13

Browse files
committed
Refactoring and cleaning up
1 parent cd43632 commit 41e0a13

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+530
-676
lines changed

docs/Scraper-Reference.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,39 +187,41 @@ More information about how filters work is available on the [Filter Reference](.
187187

188188
## Keeping scrapers up-to-date
189189

190-
In order to keep scrapers up-to-date the `get_latest_version(options, &block)` method should be overridden. If `self.release` is defined, this should return the latest version of the documentation. If `self.release` is not defined, it should return the Epoch time when the documentation was last modified. If the documentation will never change, simply return `1.0.0`. The result of this method is periodically reported in a "Documentation versions report" issue which helps maintainers keep track of outdated documentations.
190+
In order to keep scrapers up-to-date the `get_latest_version(opts)` method should be overridden. If `self.release` is defined, this should return the latest version of the documentation. If `self.release` is not defined, it should return the Epoch time when the documentation was last modified. If the documentation will never change, simply return `1.0.0`. The result of this method is periodically reported in a "Documentation versions report" issue which helps maintainers keep track of outdated documentations.
191191

192192
To make life easier, there are a few utility methods that you can use in `get_latest_version`:
193-
* `fetch(url, options, &block)`
193+
* `fetch(url, opts)`
194194

195-
Makes a GET request to the url and calls `&block` with the body.
195+
Makes a GET request to the url and returns the response body.
196196

197197
Example: [lib/docs/scrapers/bash.rb](../lib/docs/scrapers/bash.rb)
198-
* `fetch_doc(url, options, &block)`
198+
* `fetch_doc(url, opts)`
199199

200-
Makes a GET request to the url and calls `&block` with the HTML body converted to a Nokogiri document.
200+
Makes a GET request to the url and returns the HTML body converted to a Nokogiri document.
201201

202202
Example: [lib/docs/scrapers/git.rb](../lib/docs/scrapers/git.rb)
203-
* `fetch_json(url, options, &block)`
203+
* `fetch_json(url, opts)`
204204

205-
Makes a GET request to the url and calls `&block` with the JSON body converted to a dictionary.
206-
* `get_npm_version(package, options, &block)`
205+
Makes a GET request to the url and returns the JSON body converted to a dictionary.
207206

208-
Calls `&block` with the latest version of the given npm package.
207+
Example: [lib/docs/scrapers/mdn/mdn.rb](../lib/docs/scrapers/mdn/mdn.rb)
208+
* `get_npm_version(package, opts)`
209+
210+
Returns the latest version of the given npm package.
209211

210212
Example: [lib/docs/scrapers/bower.rb](../lib/docs/scrapers/bower.rb)
211-
* `get_latest_github_release(owner, repo, options, &block)`
213+
* `get_latest_github_release(owner, repo, opts)`
212214

213-
Calls `&block` with the latest GitHub release of the given repository ([format](https://developer.github.com/v3/repos/releases/#get-the-latest-release)).
215+
Returns the latest GitHub release of the given repository ([format](https://developer.github.com/v3/repos/releases/#get-the-latest-release)).
214216

215217
Example: [lib/docs/scrapers/jsdoc.rb](../lib/docs/scrapers/jsdoc.rb)
216-
* `get_github_tags(owner, repo, options, &block)`
218+
* `get_github_tags(owner, repo, opts)`
217219

218-
Calls `&block` with the list of tags on the given repository ([format](https://developer.github.com/v3/repos/#list-tags)).
220+
Returns the list of tags on the given repository ([format](https://developer.github.com/v3/repos/#list-tags)).
219221

220222
Example: [lib/docs/scrapers/liquid.rb](../lib/docs/scrapers/liquid.rb)
221-
* `get_github_file_contents(owner, repo, path, options, &block)`
223+
* `get_github_file_contents(owner, repo, path, opts)`
222224

223-
Calls `&block` with the contents of the requested file in the default branch of the given repository.
225+
Returns the contents of the requested file in the default branch of the given repository.
224226

225227
Example: [lib/docs/scrapers/minitest.rb](../lib/docs/scrapers/minitest.rb)

lib/docs/core/doc.rb

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -164,24 +164,23 @@ def build_pages(&block)
164164
raise NotImplementedError
165165
end
166166

167-
def get_scraper_version(opts, &block)
167+
def get_scraper_version(opts)
168168
if self.class.method_defined?(:options) and !options[:release].nil?
169-
block.call options[:release]
169+
options[:release]
170170
else
171171
# If options[:release] does not exist, we return the Epoch timestamp of when the doc was last modified in DevDocs production
172-
fetch_json('https://devdocs.io/docs.json', opts) do |json|
173-
items = json.select {|item| item['name'] == self.class.name}
174-
items = items.map {|item| item['mtime']}
175-
block.call items.max
176-
end
172+
json = fetch_json('https://devdocs.io/docs.json', opts)
173+
items = json.select {|item| item['name'] == self.class.name}
174+
items = items.map {|item| item['mtime']}
175+
items.max
177176
end
178177
end
179178

180179
# Should return the latest version of this documentation
181180
# If options[:release] is defined, it should be in the same format
182181
# If options[:release] is not defined, it should return the Epoch timestamp of when the documentation was last updated
183182
# If the docs will never change, simply return '1.0.0'
184-
def get_latest_version(options, &block)
183+
def get_latest_version(opts)
185184
raise NotImplementedError
186185
end
187186

@@ -216,55 +215,49 @@ def is_outdated(scraper_version, latest_version)
216215
# Utility methods for get_latest_version
217216
#
218217

219-
def fetch(url, options, &block)
218+
def fetch(url, opts)
220219
headers = {}
221220

222-
if options.key?(:github_token) and url.start_with?('https://api.github.com/')
223-
headers['Authorization'] = "token #{options[:github_token]}"
221+
if opts.key?(:github_token) and url.start_with?('https://api.github.com/')
222+
headers['Authorization'] = "token #{opts[:github_token]}"
224223
end
225224

226-
options[:logger].debug("Fetching #{url}")
225+
opts[:logger].debug("Fetching #{url}")
226+
response = Request.run(url, { headers: headers })
227227

228-
Request.run(url, { headers: headers }) do |response|
229-
if response.success?
230-
block.call response.body
231-
else
232-
options[:logger].error("Couldn't fetch #{url} (response code #{response.code})")
233-
block.call nil
234-
end
228+
if response.success?
229+
response.body
230+
else
231+
opts[:logger].error("Couldn't fetch #{url} (response code #{response.code})")
232+
nil
235233
end
236234
end
237235

238-
def fetch_doc(url, options, &block)
239-
fetch(url, options) do |body|
240-
block.call Nokogiri::HTML.parse(body, nil, 'UTF-8')
241-
end
236+
def fetch_doc(url, opts)
237+
body = fetch(url, opts)
238+
Nokogiri::HTML.parse(body, nil, 'UTF-8')
242239
end
243240

244-
def fetch_json(url, options, &block)
245-
fetch(url, options) do |body|
246-
block.call JSON.parse(body)
247-
end
241+
def fetch_json(url, opts)
242+
JSON.parse fetch(url, opts)
248243
end
249244

250-
def get_npm_version(package, options, &block)
251-
fetch_json("https://registry.npmjs.com/#{package}", options) do |json|
252-
block.call json['dist-tags']['latest']
253-
end
245+
def get_npm_version(package, opts)
246+
json = fetch_json("https://registry.npmjs.com/#{package}", opts)
247+
json['dist-tags']['latest']
254248
end
255249

256-
def get_latest_github_release(owner, repo, options, &block)
257-
fetch_json("https://api.github.com/repos/#{owner}/#{repo}/releases/latest", options, &block)
250+
def get_latest_github_release(owner, repo, opts)
251+
fetch_json("https://api.github.com/repos/#{owner}/#{repo}/releases/latest", opts)
258252
end
259253

260-
def get_github_tags(owner, repo, options, &block)
261-
fetch_json("https://api.github.com/repos/#{owner}/#{repo}/tags", options, &block)
254+
def get_github_tags(owner, repo, opts)
255+
fetch_json("https://api.github.com/repos/#{owner}/#{repo}/tags", opts)
262256
end
263257

264-
def get_github_file_contents(owner, repo, path, options, &block)
265-
fetch_json("https://api.github.com/repos/#{owner}/#{repo}/contents/#{path}", options) do |json|
266-
block.call(Base64.decode64(json['content']))
267-
end
258+
def get_github_file_contents(owner, repo, path, opts)
259+
json = fetch_json("https://api.github.com/repos/#{owner}/#{repo}/contents/#{path}", opts)
260+
Base64.decode64(json['content'])
268261
end
269262
end
270263
end

lib/docs/scrapers/angular.rb

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

158-
def get_latest_version(options, &block)
159-
get_npm_version('@angular/core', options, &block)
158+
def get_latest_version(opts)
159+
get_npm_version('@angular/core', opts)
160160
end
161161

162162
private

lib/docs/scrapers/angularjs.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class Angularjs < UrlScraper
7070
self.base_url = "https://code.angularjs.org/#{release}/docs/partials/"
7171
end
7272

73-
def get_latest_version(options, &block)
74-
get_npm_version('angular', options, &block)
73+
def get_latest_version(opts)
74+
get_npm_version('angular', opts)
7575
end
7676
end
7777
end

lib/docs/scrapers/ansible.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,9 @@ class Ansible < UrlScraper
8888
list_of_all_modules.html)
8989
end
9090

91-
def get_latest_version(options, &block)
92-
fetch_doc('https://docs.ansible.com/ansible/latest/index.html', options) do |doc|
93-
block.call doc.at_css('.DocSiteProduct-CurrentVersion').content.strip
94-
end
91+
def get_latest_version(opts)
92+
doc = fetch_doc('https://docs.ansible.com/ansible/latest/index.html', opts)
93+
doc.at_css('.DocSiteProduct-CurrentVersion').content.strip
9594
end
9695
end
9796
end

lib/docs/scrapers/apache.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ class Apache < UrlScraper
3434
Licensed under the Apache License, Version 2.0.
3535
HTML
3636

37-
def get_latest_version(options, &block)
38-
fetch_doc('http://httpd.apache.org/docs/', options) do |doc|
39-
block.call doc.at_css('#apcontents > ul a')['href'][0...-1]
40-
end
37+
def get_latest_version(opts)
38+
doc = fetch_doc('http://httpd.apache.org/docs/', opts)
39+
doc.at_css('#apcontents > ul a')['href'][0...-1]
4140
end
4241
end
4342
end

lib/docs/scrapers/apache_pig.rb

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

46-
def get_latest_version(options, &block)
47-
fetch_doc('https://pig.apache.org/', options) 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
46+
def get_latest_version(opts)
47+
doc = fetch_doc('https://pig.apache.org/', opts)
48+
item = doc.at_css('div[id="menu_1.2"] > .menuitem:last-child')
49+
item.content.strip.sub(/Release /, '')
5150
end
5251
end
5352
end

lib/docs/scrapers/async.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ class Async < UrlScraper
1818
Licensed under the MIT License.
1919
HTML
2020

21-
def get_latest_version(options, &block)
22-
fetch_doc('https://caolan.github.io/async/', options) do |doc|
23-
version = doc.at_css('#version-dropdown > a').content.strip[1..-1]
24-
block.call version
25-
end
21+
def get_latest_version(opts)
22+
doc = fetch_doc('https://caolan.github.io/async/', opts)
23+
doc.at_css('#version-dropdown > a').content.strip[1..-1]
2624
end
2725
end
2826
end

lib/docs/scrapers/babel.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ class Babel < UrlScraper
2323
'<div></div>'
2424
end
2525

26-
def get_latest_version(options, &block)
27-
fetch_doc('https://babeljs.io/docs/en/', options) do |doc|
28-
block.call doc.at_css('a[href="/versions"] > h3').content
29-
end
26+
def get_latest_version(opts)
27+
doc = fetch_doc('https://babeljs.io/docs/en/', opts)
28+
doc.at_css('a[href="/versions"] > h3').content
3029
end
3130
end
3231
end

lib/docs/scrapers/backbone.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ class Backbone < UrlScraper
2121
Licensed under the MIT License.
2222
HTML
2323

24-
def get_latest_version(options, &block)
25-
fetch_doc('https://backbonejs.org/', options) do |doc|
26-
version = doc.at_css('.version').content
27-
block.call version[1...-1]
28-
end
24+
def get_latest_version(opts)
25+
doc = fetch_doc('https://backbonejs.org/', opts)
26+
doc.at_css('.version').content[1...-1]
2927
end
3028
end
3129
end

0 commit comments

Comments
 (0)