Sha256: 8a653ecaa1d7a3b559c11e864a9eb515b5869b84c2bdb4e702be7a807664e8fd

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'typhoeus'

require_relative 'configuration'

module Unipept
  class ServerMessage
    attr_reader :message_url, :configuration

    def initialize(host)
      @message_url = "#{host}/api/v1/messages.json"
      @configuration = Unipept::Configuration.new
    end

    # Checks if the server has a message and prints it if not empty.
    # We will only check this once a day and won't print anything if the quiet
    # option is set or if we output to a file.
    def print
      return unless $stdout.tty?
      return if recently_fetched?

      resp = fetch_server_message
      update_fetched
      puts resp unless resp.empty?
    end

    # Fetches a message from the server and returns it
    def fetch_server_message
      Typhoeus.get(@message_url, params: { version: Unipept::VERSION }).body.chomp
    end

    # Returns true if the last check for a server message was less than a day
    # ago.
    def recently_fetched?
      last_fetched = @configuration['last_fetch_date']
      !last_fetched.nil? && (last_fetched + (60 * 60 * 24)) > Time.now
    end

    # Updates the last checked timestamp
    def update_fetched
      @configuration['last_fetch_date'] = Time.now
      @configuration.save
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unipept-2.2.2 lib/server_message.rb