module Quandl module Command class Task module Updatable extend ActiveSupport::Concern VERSION_URL="https://raw.github.com/quandl/quandl_command/master/lib/quandl/command/version.rb" included do before_execute :check_for_update_once_daily end def check_for_update_once_daily # short circuit return if options.production # onwards check_time = config.last_checked_for_update # check time present? if check_time.present? && check_time.is_a?(Time) # has it been more than one day? check_for_update if Time.now - 1.day > check_time || check_time > Time.now else check_for_update end end def check_for_update # notify user of impending update check print("# Checking for updates ... ") # lazy load dependencies since this happens infrequently require_check_for_update_dependencies # build request http, request = prepare_update_request # send request response = send_update_request(http, request) # handle output handle_update_response(response) rescue => err error("An unexpected error occured while checking for updates ... #{err}") error err.backtrace.join("\n") if trace? ensure config.last_checked_for_update = Time.now end private def require_check_for_update_dependencies require 'uri' require 'net/http' require 'open-uri' end def prepare_update_request uri = URI.parse( VERSION_URL ) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE [ http, Net::HTTP::Get.new(uri.request_uri) ] end def send_update_request(http, request) # send request response = http.request(request) # fetch version number response.body.split("\n").detect{|r| r =~ /VERSION/ }.split("'").last end def handle_update_response(response) return info(" you are up to date! #{response}") if Quandl::Command::VERSION == response # otherwise they are out of sync info(" A new version of quandl toolbelt has been released. #{response}. Please run 'quandl update'") end end end end end