Skip to content

Commit 3dc17a9

Browse files
committed
Finish get_latest_version for 81 scrapers and add uploading functionality
1 parent 2d1e8aa commit 3dc17a9

Some content is hidden

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

85 files changed

+739
-68
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ group :docs do
4040
gem 'unix_utils', require: false
4141
gem 'tty-pager', require: false
4242
gem 'net-sftp', '>= 2.1.3.rc2', require: false
43+
gem 'terminal-table', require: false
4344
end
4445

4546
group :test do

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ GEM
101101
unicode-display_width (~> 1.4.0)
102102
unicode_utils (~> 1.4.0)
103103
strings-ansi (0.1.0)
104+
terminal-table (1.8.0)
105+
unicode-display_width (~> 1.1, >= 1.1.1)
104106
thin (1.7.2)
105107
daemons (~> 1.0, >= 1.0.9)
106108
eventmachine (~> 1.0, >= 1.0.4)
@@ -153,6 +155,7 @@ DEPENDENCIES
153155
sinatra-contrib
154156
sprockets
155157
sprockets-helpers
158+
terminal-table
156159
thin
157160
thor
158161
tty-pager

lib/docs/core/scraper.rb

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def options
132132
end
133133
end
134134

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

@@ -147,15 +147,15 @@ def get_latest_version(&block)
147147
# 1 -> 2 = outdated
148148
# 1.1 -> 1.2 = outdated
149149
# 1.1.1 -> 1.1.2 = not outdated
150-
def is_outdated(current_version, latest_version)
151-
current_parts = current_version.split(/\./).map(&:to_i)
150+
def is_outdated(scraper_version, latest_version)
151+
scraper_parts = scraper_version.split(/\./).map(&:to_i)
152152
latest_parts = latest_version.split(/\./).map(&:to_i)
153153

154154
# Only check the first two parts, the third part is for patch updates
155155
[0, 1].each do |i|
156-
break if i >= current_parts.length or i >= latest_parts.length
157-
return true if latest_parts[i] > current_parts[i]
158-
return false if latest_parts[i] < current_parts[i]
156+
break if i >= scraper_parts.length or i >= latest_parts.length
157+
return true if latest_parts[i] > scraper_parts[i]
158+
return false if latest_parts[i] < scraper_parts[i]
159159
end
160160

161161
false
@@ -231,38 +231,62 @@ def additional_options
231231
{}
232232
end
233233

234+
#
234235
# Utility methods for get_latest_version
236+
#
235237

236-
def fetch(url, &block)
237-
Request.run(url) do |response|
238+
def fetch(url, options, &block)
239+
headers = {}
240+
241+
if options.key?(:github_token) and url.start_with?('https://api.github.com/')
242+
headers['Authorization'] = "token #{options[:github_token]}"
243+
end
244+
245+
options[:logger].debug("Fetching #{url}")
246+
247+
Request.run(url, { headers: headers }) do |response|
238248
if response.success?
239249
block.call response.body
240250
else
251+
options[:logger].error("Couldn't fetch #{url} (response code #{response.code})")
241252
block.call nil
242253
end
243254
end
244255
end
245256

246-
def fetch_doc(url, &block)
247-
fetch(url) do |body|
248-
parser = Parser.new(body)
249-
block.call parser.html
257+
def fetch_doc(url, options, &block)
258+
fetch(url, options) do |body|
259+
block.call Nokogiri::HTML.parse body, nil, 'UTF-8'
250260
end
251261
end
252262

253-
def fetch_json(url, &block)
254-
fetch(url) do |body|
263+
def fetch_json(url, options, &block)
264+
fetch(url, options) do |body|
255265
json = JSON.parse(body)
256266
block.call json
257267
end
258268
end
259269

260-
def get_npm_version(package, &block)
261-
fetch_json("https://registry.npmjs.com/#{package}") do |json|
270+
def get_npm_version(package, options, &block)
271+
fetch_json("https://registry.npmjs.com/#{package}", options) do |json|
262272
block.call json['dist-tags']['latest']
263273
end
264274
end
265275

276+
def get_latest_github_release(owner, repo, options, &block)
277+
fetch_json("https://api.github.com/repos/#{owner}/#{repo}/releases/latest", options, &block)
278+
end
279+
280+
def get_github_tags(owner, repo, options, &block)
281+
fetch_json("https://api.github.com/repos/#{owner}/#{repo}/tags", options, &block)
282+
end
283+
284+
def get_github_file_contents(owner, repo, path, options, &block)
285+
fetch_json("https://api.github.com/repos/#{owner}/#{repo}/contents/#{path}", options) do |json|
286+
block.call(Base64.decode64(json['content']))
287+
end
288+
end
289+
266290
module FixInternalUrlsBehavior
267291
def self.included(base)
268292
base.extend ClassMethods

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(&block)
159-
get_npm_version('@angular/core', &block)
158+
def get_latest_version(options, &block)
159+
get_npm_version('@angular/core', options, &block)
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(&block)
74-
get_npm_version('angular', &block)
73+
def get_latest_version(options, &block)
74+
get_npm_version('angular', options, &block)
7575
end
7676
end
7777
end

lib/docs/scrapers/ansible.rb

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

91-
def get_latest_version(&block)
92-
fetch_doc('https://docs.ansible.com/ansible/latest/index.html') do |doc|
91+
def get_latest_version(options, &block)
92+
fetch_doc('https://docs.ansible.com/ansible/latest/index.html', options) do |doc|
9393
block.call doc.at_css('.DocSiteProduct-CurrentVersion').content.strip
9494
end
9595
end

lib/docs/scrapers/apache.rb

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

37-
def get_latest_version(&block)
38-
fetch_doc('http://httpd.apache.org/docs/') do |doc|
37+
def get_latest_version(options, &block)
38+
fetch_doc('http://httpd.apache.org/docs/', options) do |doc|
3939
block.call doc.at_css('#apcontents > ul a')['href'][0...-1]
4040
end
4141
end

lib/docs/scrapers/apache_pig.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ 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|
46+
def get_latest_version(options, &block)
47+
fetch_doc('https://pig.apache.org/', options) do |doc|
4848
item = doc.at_css('div[id="menu_1.2"] > .menuitem:last-child')
4949
block.call item.content.strip.sub(/Release /, '')
5050
end

lib/docs/scrapers/async.rb

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

21-
def get_latest_version(&block)
22-
fetch_doc('https://caolan.github.io/async/') do |doc|
21+
def get_latest_version(options, &block)
22+
fetch_doc('https://caolan.github.io/async/', options) do |doc|
2323
version = doc.at_css('#version-dropdown > a').content.strip[1..-1]
2424
block.call version
2525
end

lib/docs/scrapers/babel.rb

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

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

0 commit comments

Comments
 (0)