Class: Unipept::ServerMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/server_message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#configurationObject (readonly)

Returns the value of attribute configuration.



7
8
9
# File 'lib/server_message.rb', line 7

def configuration
  @configuration
end

#message_urlObject (readonly)

Returns the value of attribute message_url.



7
8
9
# File 'lib/server_message.rb', line 7

def message_url
  @message_url
end

Instance Method Details

#fetch_server_messageObject

Fetches a message from the server and returns it



27
28
29
# File 'lib/server_message.rb', line 27

def fetch_server_message
  Typhoeus.get(@message_url, params: { version: Unipept::VERSION }).body.chomp
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.



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 = fetch_server_message
  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.

Returns:

  • (Boolean)


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_fetchedObject

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