Skip to content

Commit 7ee41b8

Browse files
committed
fix: add uri to signage plugin
1 parent 64a8a38 commit 7ee41b8

4 files changed

Lines changed: 41 additions & 0 deletions

File tree

migration/db/migrations/20260319100500000_add_plugins_and_playlist_fields.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ CREATE TABLE IF NOT EXISTS "signage_plugin"(
1515

1616
name TEXT NOT NULL,
1717
description TEXT,
18+
uri TEXT NOT NULL,
1819
enabled BOOLEAN NOT NULL DEFAULT TRUE,
1920
params JSONB NOT NULL DEFAULT '{}'::jsonb,
2021
defaults JSONB NOT NULL DEFAULT '{}'::jsonb,

spec/generator.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ module PlaceOS::Model
106106
def self.signage_plugin(
107107
name : String = Faker::Hacker.noun,
108108
description : String = "",
109+
uri : String = "/plugins/default",
109110
authority : Authority? = nil,
110111
params : Hash(String, JSON::Any) = {"type" => JSON::Any.new("object"), "properties" => JSON::Any.new({"play_at_period" => JSON::Any.new({"type" => JSON::Any.new("integer")} of String => JSON::Any)} of String => JSON::Any)},
111112
defaults : Hash(String, JSON::Any) = {"play_at_period" => JSON::Any.new(10_i64)},
@@ -118,6 +119,7 @@ module PlaceOS::Model
118119
SignagePlugin.new(
119120
name: name,
120121
description: description,
122+
uri: uri,
121123
authority_id: authority.id,
122124
params: params,
123125
defaults: defaults,

spec/signage_plugin_spec.cr

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,27 @@ module PlaceOS::Model
6060
)
6161
plugin.save.should eq true
6262
end
63+
64+
it "allows a local resource URI" do
65+
plugin = Generator.signage_plugin(uri: "/plugins/weather")
66+
plugin.save.should eq true
67+
end
68+
69+
it "allows an https URL" do
70+
plugin = Generator.signage_plugin(uri: "https://example.com/plugins/weather")
71+
plugin.save.should eq true
72+
end
73+
74+
it "rejects http URLs" do
75+
plugin = Generator.signage_plugin(uri: "http://example.com/plugins/weather")
76+
plugin.save.should eq false
77+
plugin.errors.any? { |e| e.field == :uri }.should eq true
78+
end
79+
80+
it "requires a uri" do
81+
plugin = Generator.signage_plugin(uri: "")
82+
plugin.save.should eq false
83+
plugin.errors.any? { |e| e.field == :uri }.should eq true
84+
end
6385
end
6486
end

src/placeos-models/signage_plugin.cr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require "uri"
12
require "./base/model"
23

34
module PlaceOS::Model
@@ -8,6 +9,7 @@ module PlaceOS::Model
89

910
attribute name : String, es_subfield: "keyword"
1011
attribute description : String = ""
12+
attribute uri : String
1113

1214
belongs_to Authority, foreign_key: "authority_id"
1315

@@ -19,6 +21,20 @@ module PlaceOS::Model
1921
###############################################################################################
2022

2123
validates :name, presence: true
24+
validates :uri, presence: true
25+
26+
validate ->(this : SignagePlugin) {
27+
return unless uri = this.uri.presence
28+
begin
29+
parsed = URI.parse(uri)
30+
raise "requires a request target" unless parsed.request_target.presence
31+
if scheme = parsed.scheme
32+
raise "scheme must be https" unless scheme.downcase == "https"
33+
end
34+
rescue error
35+
this.validation_error(:uri, "not valid: #{error.message}")
36+
end
37+
}
2238

2339
# ensure keys in defaults exist in params properties
2440
validate ->(this : SignagePlugin) {

0 commit comments

Comments
 (0)