Class: Jav::Licensing::HQ

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

Constant Summary collapse

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

seconds

5
CACHE_TIME =

seconds

3600 * 12

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.



16
17
18
19
# File 'lib/jav/licensing/h_q.rb', line 16

def initialize(current_request = nil)
  @current_request = current_request
  @cache_store = Jav::App.cache_store
end

Instance Attribute Details

#cache_storeObject

Returns the value of attribute cache_store.



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

def cache_store
  @cache_store
end

#current_requestObject

Returns the value of attribute current_request.



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

def current_request
  @current_request
end

Class Method Details

.cache_keyObject



11
12
13
# File 'lib/jav/licensing/h_q.rb', line 11

def cache_key
  "jav.hq-#{Jav::VERSION.parameterize}.response"
end

Instance Method Details

#cached_responseObject



121
122
123
124
# File 'lib/jav/licensing/h_q.rb', line 121

def cached_response
  response
  # cache_store.read self.class.cache_key
end

#clear_responseObject



59
60
61
# File 'lib/jav/licensing/h_q.rb', line 59

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



42
43
44
45
46
47
48
49
50
51
# File 'lib/jav/licensing/h_q.rb', line 42

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



53
54
55
56
57
# File 'lib/jav/licensing/h_q.rb', line 53

def fresh_response
  clear_response

  make_request
end

#jav_metadataObject



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
114
115
116
117
118
119
# File 'lib/jav/licensing/h_q.rb', line 83

def 
  resources = App.resources
  dashboards = App.dashboards
  field_definitions = resources.map(&:get_field_definitions)
  fields_count = field_definitions.map(&:count).sum
  fields_per_resource = format("%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: Jav.configuration.main_menu.present?,
    profile_menu_present: Jav.configuration.profile_menu.present?,
    cache_store: Jav::App.cache_store&.class&.to_s,
    **
  }
rescue StandardError => e
  {
    error: e.message
  }
end

#payloadObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jav/licensing/h_q.rb', line 63

def payload
  result = {
    license: Jav.configuration.license,
    license_key: Jav.configuration.license_key,
    jav_version: Jav::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
  }

   = 
  result[:jav_metadata] =  if [:resources_count] != 0

  result
end

#responseObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jav/licensing/h_q.rb', line 21

def response
  { 'id' => 'pro',
    'valid' => true,
    'payload' => {},
    'expiry' => 3600,
    'fetched_at' => Time.zone.now,
    'license' => 'pro',
    'license_key' => nil }
  # expire_cache_if_overdue
  # ::Rails.cache.fetch(self.class.cache_key) do
  #   { 'id' => 'pro',
  #     'valid' => true,
  #     'payload' => {},
  #     'expiry' => 3600,
  #     'fetched_at' => Time.zone.now,
  #     'license' => 'pro',
  #     'license_key' => nil }
  # end
end