Class: Unipept::ServerMessage
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#message_url ⇒ Object
readonly
Returns the value of attribute message_url.
Instance Method Summary collapse
-
#fetch_server_message ⇒ Object
Fetches a message from the server and returns it.
-
#initialize(host) ⇒ ServerMessage
constructor
A new instance of ServerMessage.
-
#print ⇒ Object
Checks if the server has a message and prints it if not empty.
-
#recently_fetched? ⇒ Boolean
Returns true if the last check for a server message was less than a day ago.
-
#update_fetched ⇒ Object
Updates the last checked timestamp.
Constructor Details
#initialize(host) ⇒ ServerMessage
Returns a new instance of ServerMessage.
9 10 11 12 |
# File 'lib/server_message.rb', line 9 def initialize(host) @message_url = "#{host}/api/v2/messages.json" @configuration = Unipept::Configuration.new end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
7 8 9 |
# File 'lib/server_message.rb', line 7 def configuration @configuration end |
#message_url ⇒ Object (readonly)
Returns the value of attribute message_url.
7 8 9 |
# File 'lib/server_message.rb', line 7 def @message_url end |
Instance Method Details
#fetch_server_message ⇒ Object
Fetches a message from the server and returns it
27 28 29 |
# File 'lib/server_message.rb', line 27 def Typhoeus.get(@message_url, params: { version: Unipept::VERSION }).body.chomp end |
#print ⇒ Object
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.
17 18 19 20 21 22 23 24 |
# File 'lib/server_message.rb', line 17 def print return unless $stdout.tty? return if recently_fetched? resp = update_fetched puts resp unless resp.empty? end |
#recently_fetched? ⇒ Boolean
Returns true if the last check for a server message was less than a day ago.
33 34 35 36 |
# File 'lib/server_message.rb', line 33 def recently_fetched? last_fetched = @configuration['last_fetch_date'] !last_fetched.nil? && (last_fetched + (60 * 60 * 24)) > Time.now end |
#update_fetched ⇒ Object
Updates the last checked timestamp
39 40 41 42 |
# File 'lib/server_message.rb', line 39 def update_fetched @configuration['last_fetch_date'] = Time.now @configuration.save end |