Class: Terminalwire::Client::ServerLicenseVerification

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/terminalwire/client/server_license_verification.rb

Overview

Checkes the server for a license verification at ‘terminalwire.com/licenses/verifications/` and displays the message to the user, if necessary.

Instance Method Summary collapse

Constructor Details

#initialize(url:) ⇒ ServerLicenseVerification

Returns a new instance of ServerLicenseVerification.



12
13
14
15
16
# File 'lib/terminalwire/client/server_license_verification.rb', line 12

def initialize(url:)
  @url = URI(url)
  @internet = Async::HTTP::Internet.new
  @cache_store = Terminalwire::Cache::File::Store.new(path: Terminalwire::Client.root_path.join("cache/licenses/verifications"))
end

Instance Method Details

#cacheObject



22
# File 'lib/terminalwire/client/server_license_verification.rb', line 22

def cache = @cache_store.entry key

#keyObject



18
19
20
# File 'lib/terminalwire/client/server_license_verification.rb', line 18

def key
  Base64.urlsafe_encode64 @url
end

#messageObject



51
52
53
# File 'lib/terminalwire/client/server_license_verification.rb', line 51

def message
  payload.dig(:shell, :output)
end

#payloadObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/terminalwire/client/server_license_verification.rb', line 24

def payload
  if cache.miss?
    logger.debug "Stale verification. Requesting new verification."
    request do |response|
      # Set the expiry on the file cache for the header.
      if max_age = response.headers["cache-control"].max_age
        logger.debug "Caching for #{max_age}"
        cache.expires = Time.now + max_age
      end

      # Process based on the response code.
      case response.status
      in 200
        logger.debug "License for #{@url} found."
        data = self.class.unpack response.read
        cache.value = data
        return data
      in 404
        logger.debug "License for #{@url} not found."
        return self.class.unpack response.read
      end
    end
  else
    return cache.value
  end
end