Class: Avo::Licensing::HQ

Inherits:
Object
  • Object
show all
Defined in:
lib/avo/licensing/h_q.rb

Constant Summary collapse

ENDPOINT =
"https://v3.avohq.io/api/v3/licenses/check".freeze
REQUEST_TIMEOUT =

seconds

5
CACHE_TIME =

seconds

6.hours.to_i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_request = nil) ⇒ HQ

Returns a new instance of HQ.



17
18
19
20
# File 'lib/avo/licensing/h_q.rb', line 17

def initialize(current_request = nil)
  @current_request = current_request
  @cache_store = Avo.cache_store
end

Instance Attribute Details

#cache_storeObject

Returns the value of attribute cache_store.



5
6
7
# File 'lib/avo/licensing/h_q.rb', line 5

def cache_store
  @cache_store
end

#current_requestObject

Returns the value of attribute current_request.



4
5
6
# File 'lib/avo/licensing/h_q.rb', line 4

def current_request
  @current_request
end

Class Method Details

.cache_keyObject



12
13
14
# File 'lib/avo/licensing/h_q.rb', line 12

def cache_key
  "avo.hq-#{Avo::VERSION.parameterize}.response"
end

Instance Method Details

#cached_responseObject



78
79
80
# File 'lib/avo/licensing/h_q.rb', line 78

def cached_response
  cache_store.read self.class.cache_key
end

#clear_responseObject



52
53
54
# File 'lib/avo/licensing/h_q.rb', line 52

def clear_response
  cache_store.delete self.class.cache_key
end

#expire_cache_if_overdueObject

Some cache stores don’t auto-expire their keys and payloads so we need to do it for them



37
38
39
40
41
42
43
44
# File 'lib/avo/licensing/h_q.rb', line 37

def expire_cache_if_overdue
  return unless cached_response.present? || cached_response&.fetch(:fetched_at, nil).present?

  parsed_time = Time.parse(cached_response["fetched_at"].to_s)
  cache_should_expire = parsed_time < Time.now - CACHE_TIME

  clear_response if cache_should_expire
end

#fresh_responseObject



46
47
48
49
50
# File 'lib/avo/licensing/h_q.rb', line 46

def fresh_response
  clear_response

  make_request
end

#payloadObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/avo/licensing/h_q.rb', line 56

def payload
  result = {
    license: Avo.configuration.license,
    license_key: Avo.configuration.license_key,
    avo_version: Avo::VERSION,
    rails_version: Rails::VERSION::STRING,
    ruby_version: RUBY_VERSION,
    environment: Rails.env,
    ip: current_request&.ip,
    host: current_request&.host,
    port: current_request&.port,
    app_name: app_name
  }

  # metadata = Avo::Services::DebugService.avo_metadata
  # if metadata[:resources_count] != 0
  #   result[:avo_metadata] = "metadata"
  # end

  result
end

#responseObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/avo/licensing/h_q.rb', line 22

def response
  expire_cache_if_overdue

  # ------------------------------------------------------------------
  # You could set this to true to become a pro user for free.
  # I'd rather you didn't. Avo takes time & love to build,
  # and I can't do that if it doesn't pay my bills!
  #
  # If you want Pro, help pay for its development.
  # Can't afford it? Get in touch: [email protected]
  # ------------------------------------------------------------------
  make_request
end