Class: OpenC3::PluginStoreModel

Inherits:
Model show all
Defined in:
lib/openc3/models/plugin_store_model.rb

Constant Summary collapse

PRIMARY_KEY =
'openc3_plugin_store'
DEFAULT_STORE_URL =
'https://store.openc3.com'
JSON_ENDPOINT =
'/api/v1.2/cosmos_plugins'

Instance Attribute Summary

Attributes inherited from Model

#name, #plugin, #scope, #updated_at

Class Method Summary collapse

Methods inherited from Model

#as_json, #check_disable_erb, #create, #deploy, #destroy, #destroyed?, #diff, filter, find_all_by_plugin, from_json, get, get_all_models, get_model, handle_config, #initialize, names, store, store_queued, #undeploy, #update

Constructor Details

This class inherits a constructor from OpenC3::Model

Class Method Details

.allObject



27
28
29
# File 'lib/openc3/models/plugin_store_model.rb', line 27

def self.all()
  Store.get(PRIMARY_KEY)
end

.ensure_existsObject



69
70
71
72
# File 'lib/openc3/models/plugin_store_model.rb', line 69

def self.ensure_exists
  plugins = self.all()
  self.update() if plugins.nil? or plugins.length.zero? or plugins[0]['error']
end

.get_by_id(id) ⇒ Object



40
41
42
43
# File 'lib/openc3/models/plugin_store_model.rb', line 40

def self.get_by_id(id)
  plugins = JSON.parse(all()) rescue []
  plugins.find { |plugin| plugin["id"] == Integer(id) }
end

.plugin_store_error(message) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/openc3/models/plugin_store_model.rb', line 31

def self.plugin_store_error(message)
  Store.set(PRIMARY_KEY, {
    date: Time.now.utc.iso8601,
    title: 'Plugin Store Error',
    body: message,
    error: true,
  }.to_json)
end

.set(plugin_store_data) ⇒ Object



23
24
25
# File 'lib/openc3/models/plugin_store_model.rb', line 23

def self.set(plugin_store_data)
  Store.set(PRIMARY_KEY, plugin_store_data)
end

.updateObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/openc3/models/plugin_store_model.rb', line 45

def self.update
  url_setting = SettingModel.get(name: 'store_url', scope: 'DEFAULT')
  store_url = url_setting['data'] if url_setting
  store_url = DEFAULT_STORE_URL if store_url.nil? or store_url.strip.empty?

  api_key_setting = SettingModel.get(name: 'store_api_key', scope: 'DEFAULT')
  api_key = api_key_setting['data'] if api_key_setting

  conn = Faraday.new(url: store_url) do |faraday|
    if api_key && !api_key.strip.empty?
      faraday.headers['Authorization'] = "Bearer #{api_key}"
    end
  end

  response = conn.get(JSON_ENDPOINT)
  if response.success?
    self.set(response.body)
  else
    self.plugin_store_error("Error contacting plugin store at #{store_url} (status: #{response.status})")
  end
rescue Exception => e
  self.plugin_store_error("Error contacting plugin store at #{store_url}. #{e.message})")
end