2019-05-31 09:54:11 +02:00
|
|
|
class CustomWizard::Api::Endpoint
|
|
|
|
include ActiveModel::SerializerSupport
|
|
|
|
|
|
|
|
attr_accessor :id,
|
|
|
|
:method,
|
|
|
|
:url
|
|
|
|
|
2019-06-02 12:54:31 +02:00
|
|
|
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
|
|
|
|
|
2019-06-02 12:54:31 +02:00
|
|
|
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
|
|
|
|
|
2019-06-02 12:54:31 +02:00
|
|
|
PluginStore.set("custom_wizard_api_#{name}", "endpoint_#{endpoint_id}", model.as_json)
|
2019-05-31 09:54:11 +02:00
|
|
|
|
2019-06-02 12:54:31 +02:00
|
|
|
self.get(name)
|
2019-05-31 09:54:11 +02:00
|
|
|
end
|
|
|
|
|
2019-06-02 12:54:31 +02:00
|
|
|
def self.get(name, endpoint_id)
|
2019-05-31 09:54:11 +02:00
|
|
|
return nil if !endpoint_id
|
2019-06-02 12:54:31 +02:00
|
|
|
data = PluginStore.get("custom_wizard_api_#{name}", "endpoint_#{endpoint_id}")
|
2019-05-31 09:54:11 +02:00
|
|
|
data[:id] = endpoint_id
|
2019-06-02 12:54:31 +02:00
|
|
|
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|
|
2019-06-02 12:54:31 +02:00
|
|
|
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
|
2019-06-02 12:54:31 +02:00
|
|
|
self.new(name, data)
|
2019-05-31 09:54:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|