1
Fork 0

Add query parameters to the generated URL.

This commit is contained in:
Bauke 2022-04-02 18:37:30 +02:00
parent 24f907b59b
commit b3c08e565a
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 21 additions and 1 deletions

View File

@ -15,6 +15,7 @@ path = "source/lib.rs"
[dependencies]
md5 = "0.7.0"
urlencoding = "2.1.0"
[dev-dependencies]
insta = "1.14.0"

View File

@ -64,8 +64,27 @@ impl Generator {
pub fn generate(&self, email: &str) -> String {
let base_url = &self.base_url;
let hash = Self::hash_email(email);
let query_parameters = self.query_parameters();
format!("https://{base_url}/avatar/{hash}{query_parameters}")
}
format!("https://{base_url}/avatar/{hash}")
/// Returns all configurable options as a query parameter string.
pub fn query_parameters(&self) -> String {
fn encode<D: std::fmt::Display>(data: D) -> String {
urlencoding::encode(&data.to_string()).into_owned()
}
let mut query_parameters = vec![];
if let Some(image_size) = self.image_size {
query_parameters.push(format!("s={}", encode(image_size)));
}
if query_parameters.is_empty() {
String::new()
} else {
format!("?{}", query_parameters.join("&"))
}
}
/// Configures the Generator to use a custom base URL for generated URLs.