_plugins/crate_gen.rb
# Fetches a URL and caches the result as a tmp file for future calls
# This allows rerunning this script without hammerring APIs
Fetches a URL and caches the result as a tmp file for future calls
This allows rerunning this script without hammerring APIs
Fetches a URL and caches the result as a tmp file for future calls This allows rerunning this script without hammerring APIs
def cached_request(url, headers={}, limit=3)
      if limit == 0
        puts "Error: reached retry limit"
        return {}
      end
      dir = url.sub(/^https?:\/\//, File.join(__dir__, "../_tmp/"))
      path = File.join(dir, "index.json")
      FileUtils.mkdir_p(dir)
      unless File.exists? path
        uri = URI(url)
        req = Net::HTTP::Get.new(uri)
        headers&.each { |k,v| req[k] = v }
        res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => (uri.scheme == 'https')) { |http|
          http.request(req)
        }
        if res.is_a?(Net::HTTPSuccess)
          data = res.body
          File.open(path, 'w') do |f|
            f.write data
          end
        elsif res.is_a?(Net::HTTPRedirection)
          puts "#{res.code}: #{res.message} for #{url} (will retry redirect)"
          return cached_request(res['location'], headers, limit - 1)
        else
          puts "#{res.code}: #{res.message} for #{url}"
          return {}
        end
      end
      JSON.parse(File.read(path))
    end
def cached_request ( url , headers = { } , limit = 3 ) if limit == 0 puts "Error: reached retry limit" return { } end dir = url . sub ( / ^https?: \/ \/ / , File . join ( __dir__ , "../_tmp/" ) ) path = File . join ( dir , "index.json" ) FileUtils . mkdir_p ( dir ) unless File . exists? path uri = URI ( url ) req = Net :: HTTP :: Get . new ( uri ) headers &. each { | k , v | req [ k ] = v } res = Net :: HTTP . start ( uri . hostname , uri . port , :use_ssl => ( uri . scheme == 'https' ) ) { | http | http . request ( req ) } if res . is_a? ( Net :: HTTPSuccess ) data = res . body File . open ( path , 'w' ) do | f | f . write data end elsif res . is_a? ( Net :: HTTPRedirection ) puts "#{res.code}: #{res.message} for #{url} (will retry redirect)" return cached_request ( res [ 'location' ] , headers , limit - 1 ) else puts "#{res.code}: #{res.message} for #{url}" return { } end end JSON . parse ( File . read ( path ) ) end
Fetches a URL and caches the result as a tmp file for future calls
This allows rerunning this script without hammerring APIs
Fetches a URL and caches the result as a tmp file for future calls This allows rerunning this script without hammerring APIs
url
headers
limit