@@ -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
0 commit comments