diff --git a/src/plugin/subscriptions.cr b/src/plugin/subscriptions.cr index 2593970..5a19027 100644 --- a/src/plugin/subscriptions.cr +++ b/src/plugin/subscriptions.cr @@ -1,4 +1,5 @@ require "uuid" +require "big" enum FilterType String @@ -47,6 +48,23 @@ struct Filter _value.as_f32? || nil self.new key, value, type end + + def match_chapter(obj : JSON::Any) : Bool + raw_value = obj[key] + case type + when FilterType::String + raw_value.as_s.lower == value.as_s.lower + when FilterType::NumMin + when FilterType::DateMin + BigFloat.new(raw_value) >= BigFloat.new(value) + when FilterType::NumMax + when FilterType::DateMax + BigFloat.new(raw_value) <= BigFloat.new(value) + when FilterType::Array + raw_value.as_s.lower.split(",") + .map(&.strip).include? value.as_s.lower.strip + end + end end struct Subscription @@ -65,6 +83,10 @@ struct Subscription @created_at = Time.utc.to_unix @last_checked = Time.utc.to_unix end + + def match_chapter(obj : JSON::Any) : Bool + filters.all? &.match_chapter(obj) + end end struct SubscriptionList