1
0
Fork 0
discourse-custom-wizard-unl.../lib/api/endpoint.rb

51 Zeilen
1,4 KiB
Ruby

2019-05-31 09:54:11 +02:00
class CustomWizard::Api::Endpoint
include ActiveModel::SerializerSupport
attr_accessor :id,
:method,
:url
def initialize(name, params)
@name = name
if data = params.is_a?(String) ? ::JSON.parse(params) : params
data.each do |k, v|
self.send "#{k}=", v if self.respond_to?(k)
end
2019-05-31 09:54:11 +02:00
end
end
def self.set(name, data)
model = data[:endpoint_id] ? self.get(name, data[:endpoint_id]) : {}
2019-05-31 09:54:11 +02:00
endpoint_id = model[:endpoint_id] || SecureRandom.hex(8)
data.each do |k, v|
model.send "#{k}=", v if model.respond_to?(k)
end
PluginStore.set("custom_wizard_api_#{name}", "endpoint_#{endpoint_id}", model.as_json)
2019-05-31 09:54:11 +02:00
self.get(name)
2019-05-31 09:54:11 +02:00
end
def self.get(name, endpoint_id)
2019-05-31 09:54:11 +02:00
return nil if !endpoint_id
data = PluginStore.get("custom_wizard_api_#{name}", "endpoint_#{endpoint_id}")
2019-05-31 09:54:11 +02:00
data[:id] = endpoint_id
self.new(name, data)
end
def self.remove(name)
PluginStoreRow.where("plugin_name = 'custom_wizard_api_#{name}' AND key LIKE 'endpoint_%'").destroy_all
2019-05-31 09:54:11 +02:00
end
def self.list
PluginStoreRow.where("plugin_name LIKE 'custom_wizard_api_%' AND key LIKE 'endpoint_%'")
.map do |record|
name = record['plugin_name'].sub("custom_wizard_api_", "")
2019-05-31 09:54:11 +02:00
data = ::JSON.parse(record['value'])
data[:id] = record['key'].split('_').last
self.new(name, data)
2019-05-31 09:54:11 +02:00
end
end
end