Add a function to generate HTTP::Params from Filters

This commit is contained in:
Samantaz Fox 2022-03-12 19:56:52 +01:00
parent fb2a331f79
commit 6991d0851f
No known key found for this signature in database
GPG Key ID: F42821059186176E
2 changed files with 133 additions and 0 deletions

View File

@ -254,4 +254,118 @@ Spectator.describe Invidious::Search::Filters do
)
end
end
# -------------------
# Encode (URL)
# -------------------
describe "#to_iv_params" do
it "Encodes date filter" do
Invidious::Search::Filters::Date.each do |value|
filters = described_class.new(date: value)
params = filters.to_iv_params
if value.none?
expect("#{params}").to eq("")
else
expect("#{params}").to eq("date=#{value.to_s.underscore}")
end
end
end
it "Encodes type filter" do
Invidious::Search::Filters::Type.each do |value|
filters = described_class.new(type: value)
params = filters.to_iv_params
if value.all?
expect("#{params}").to eq("")
else
expect("#{params}").to eq("type=#{value.to_s.underscore}")
end
end
end
it "Encodes duration filter" do
Invidious::Search::Filters::Duration.each do |value|
filters = described_class.new(duration: value)
params = filters.to_iv_params
if value.none?
expect("#{params}").to eq("")
else
expect("#{params}").to eq("duration=#{value.to_s.underscore}")
end
end
end
it "Encodes features filter (single)" do
Invidious::Search::Filters::Features.each do |value|
string = described_class.format_features(value)
filters = described_class.new(features: value)
expect("#{filters.to_iv_params}")
.to eq("features=" + FEATURES_TEXT[value])
end
end
it "Encodes features filter (multiple)" do
features = Invidious::Search::Filters::Features.flags(Subtitles, Live, ThreeSixty)
filters = described_class.new(features: features)
expect("#{filters.to_iv_params}")
.to eq("features=live%2Csubtitles%2C360") # %2C is a comma
end
it "Encodes sort filter" do
Invidious::Search::Filters::Sort.each do |value|
filters = described_class.new(sort: value)
params = filters.to_iv_params
if value.relevance?
expect("#{params}").to eq("")
else
expect("#{params}").to eq("sort=#{value.to_s.underscore}")
end
end
end
it "Encodes multiple filters" do
filters = described_class.new(
date: Invidious::Search::Filters::Date::Today,
duration: Invidious::Search::Filters::Duration::Medium,
features: Invidious::Search::Filters::Features.flags(Location, Purchased),
sort: Invidious::Search::Filters::Sort::Relevance
)
params = filters.to_iv_params
# Check the `date` param
expect(params).to have_key("date")
expect(params.fetch_all("date")).to contain_exactly("today")
# Check the `type` param
expect(params).to_not have_key("type")
expect(params["type"]?).to be_nil
# Check the `duration` param
expect(params).to have_key("duration")
expect(params.fetch_all("duration")).to contain_exactly("medium")
# Check the `features` param
expect(params).to have_key("features")
expect(params.fetch_all("features")).to contain_exactly("location,purchased")
# Check the `sort` param
expect(params).to_not have_key("sort")
expect(params["sort"]?).to be_nil
# Check if there aren't other parameters
params.delete("date")
params.delete("duration")
params.delete("features")
expect(params).to be_empty
end
end
end

View File

@ -228,6 +228,25 @@ module Invidious::Search
return filters
end
def to_iv_params : HTTP::Params
# Temporary variables
raw_params = {} of String => Array(String)
raw_params["date"] = [@date.to_s.underscore] if !@date.none?
raw_params["type"] = [@type.to_s.underscore] if !@type.all?
raw_params["sort"] = [@sort.to_s.underscore] if !@sort.relevance?
if !@duration.none?
raw_params["duration"] = [@duration.to_s.underscore]
end
if !@features.none?
raw_params["features"] = [Filters.format_features(@features)]
end
return HTTP::Params.new(raw_params)
end
# -------------------
# Youtube params
# -------------------