class StructCore::Refresher

Constants

GIT_CONTENT_REPOSITORY_BASE

Public Class Methods

internet?() click to toggle source

rubocop:enable Metrics/PerceivedComplexity rubocop:enable Metrics/MethodLength

# File lib/refresher/refresher.rb, line 72
def self.internet?
        dns_resolver = Resolv::DNS.new
        begin
                dns_resolver.getaddress('icann.org')
                return true
        rescue Resolv::ResolvError => _
                return false
        end
end
run() click to toggle source

There's not much sense refactoring this to be tiny methods. rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/MethodLength

# File lib/refresher/refresher.rb, line 18
def self.run
        # Silently fail whenever possible and try not to wait too long. Don't want to bug the users!
        return unless Refresher.internet?

        begin
                local_gem_version = Semantic::Version.new StructCore::VERSION
                struct_cache_dir = File.join Dir.tmpdir, 'struct-cache'
                Dir.mkdir struct_cache_dir unless File.exist? struct_cache_dir
        rescue StandardError => _
                return
        end

        cached_changelog_path = File.join struct_cache_dir, 'changelog.yml'
        if File.exist? cached_changelog_path
                begin
                        changelog = YAML.load_file cached_changelog_path
                rescue StandardError => _
                        return
                end

                return if changelog.nil? || changelog['updated'].nil?

                changed_date = Time.at(changelog['updated']).to_date
                return if changed_date.nil?

                if changed_date == Time.now.to_date
                        print changelog, local_gem_version
                        return
                end
        end

        # Keep the timeout super-short. This is a fairly big assumption and needs fine-tuning, but most devs
        # have awesome internet connections, so this should be fine for the most part. Don't want to keep
        # the UI stalling for too long!
        changelog_url = ENV['STRUCT_CHANGELOG_URL'] || "#{GIT_CONTENT_REPOSITORY_BASE}/changelog.yml"
        changelog_res = Excon.get(changelog_url, connect_timeout: 5)

        return unless changelog_res.status == 200 && !changelog_res.body.nil?

        begin
                changelog = YAML.safe_load changelog_res.body
                changelog['updated'] = Time.now.to_i
                FileUtils.mkdir_p struct_cache_dir
                FileUtils.rm_rf cached_changelog_path
                File.write cached_changelog_path, changelog.to_yaml
        rescue StandardError => _
                return
        end

        print changelog, local_gem_version
end

Private Class Methods

out_of_date?(latest_gem_version, local_gem_version) click to toggle source
# File lib/refresher/refresher.rb, line 82
def self.out_of_date?(latest_gem_version, local_gem_version)
        latest_gem_version > local_gem_version
end
print(changelog, local_gem_version) click to toggle source