Class: Avo::Licensing::HQ

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

Defined Under Namespace

Classes: RESPONSE_STRUCT

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.



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

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



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

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

Instance Method Details

#cached_responseObject



85
86
87
# File 'lib/avo/licensing/h_q.rb', line 85

def cached_response
  cache_store.read self.class.cache_key
end

#clear_responseObject



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

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



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

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



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

def fresh_response
  clear_response

  make_request
end

#payloadObject



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

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
  }

  begin
     = Avo::Services::DebugService.
  rescue => error
     = {
      error_message: error.message,
      error: "Failed to generate the Avo metadata"
    }
  end

  result[:avo_metadata] = 

  result
end

#responseObject



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

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: adrian@avohq.io
  # ------------------------------------------------------------------
  make_request
end