Class: Avo::Licensing::HQ

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

Constant Summary collapse

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

seconds

5
CACHE_TIME =

seconds

3600

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::App.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

#avo_metadataObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/avo/licensing/h_q.rb', line 80

def 
  resources = App.resources
  dashboards = App.dashboards
  field_definitions = resources.map(&:get_field_definitions)
  fields_count = field_definitions.map(&:count).sum
  fields_per_resource = sprintf("%0.01f", fields_count / (resources.count + 0.0))

  field_types = {}
  custom_fields_count = 0
  field_definitions.each do |fields|
    fields.each do |field|
      field_types[field.type] ||= 0
      field_types[field.type] += 1

      custom_fields_count += 1 if field.custom?
    end
  end

  {
    resources_count: resources.count,
    dashboards_count: dashboards.count,
    fields_count: fields_count,
    fields_per_resource: fields_per_resource,
    custom_fields_count: custom_fields_count,
    field_types: field_types,
    **(:actions),
    **(:filters),
    main_menu_present: Avo.configuration.main_menu.present?,
    profile_menu_present: Avo.configuration.profile_menu.present?,
    **
  }
rescue
  {}
end

#cached_responseObject



115
116
117
# File 'lib/avo/licensing/h_q.rb', line 115

def cached_response
  cache_store.read self.class.cache_key
end

#clear_responseObject



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

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
45
46
# File 'lib/avo/licensing/h_q.rb', line 37

def expire_cache_if_overdue
  return unless cached_response.present?
  return unless cached_response["fetched_at"].present?

  allowed_time = 1.hour
  parsed_time = Time.parse(cached_response["fetched_at"].to_s)
  time_has_passed = parsed_time < Time.now - allowed_time

  clear_response if time_has_passed
end

#fresh_responseObject



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

def fresh_response
  clear_response

  make_request
end

#payloadObject



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

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
  }

   = 
  if [:resources_count] != 0
    result[:avo_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: adrian@avohq.io
  # ------------------------------------------------------------------
  make_request
end