Skip to content

Commit 74ce1f0

Browse files
committed
Add functionality to work with profiles API.
1 parent 3e2dcd2 commit 74ce1f0

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Added
11+
12+
- Functionality to work with Gravatar profiles has been added.
13+
814
## [0.3.0] - 2024-09-01
915

1016
### Changed

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ extern crate url;
55

66
pub mod avatars;
77
mod common;
8+
pub mod profiles;

src/profiles/format.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use std::fmt;
2+
3+
/// The data format to be used when retrieving profiles.
4+
///
5+
/// See <https://docs.gravatar.com/api/profiles/#data-formats>.
6+
#[derive(Clone, Debug)]
7+
pub enum Format {
8+
/// JSON
9+
/// See <https://docs.gravatar.com/api/profiles/json/>.
10+
Json,
11+
12+
/// XML
13+
/// See <https://docs.gravatar.com/api/profiles/xml/>.
14+
Xml,
15+
16+
/// PHP
17+
/// See <https://docs.gravatar.com/api/profiles/php/>.
18+
Php,
19+
20+
/// VCF/vCard
21+
/// See <https://docs.gravatar.com/api/profiles/vcf-vcard/>.
22+
Vcard,
23+
24+
// QR code image in PNG format
25+
// See <https://docs.gravatar.com/api/profiles/qr-codes/>
26+
QrCode,
27+
}
28+
29+
impl std::fmt::Display for Format {
30+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31+
let val = match self {
32+
Format::Json => "json",
33+
Format::Xml => "xml",
34+
Format::Php => "php",
35+
Format::Vcard => "vcf",
36+
Format::QrCode => "qr",
37+
};
38+
f.write_str(val)
39+
}
40+
}
41+
42+
#[cfg(test)]
43+
mod tests {
44+
use super::*;
45+
46+
#[test]
47+
fn test_to_string() {
48+
assert_eq!(Format::Json.to_string(), "json");
49+
assert_eq!(Format::Xml.to_string(), "xml");
50+
assert_eq!(Format::Php.to_string(), "php");
51+
assert_eq!(Format::Vcard.to_string(), "vcf");
52+
assert_eq!(Format::QrCode.to_string(), "qr");
53+
}
54+
}

src/profiles/mod.rs

+69
Original file line numberDiff line numberDiff line change
@@ -1 +1,70 @@
11
const BASE_URL: &str = "https://gravatar.com/";
2+
3+
use url::Url;
4+
5+
use crate::common::email_hash;
6+
7+
mod format;
8+
9+
pub use format::Format;
10+
11+
/// Representation of a single Gravatar profile URL.
12+
#[derive(Clone, Debug)]
13+
pub struct Profile {
14+
email: String,
15+
}
16+
17+
impl Profile {
18+
pub fn builder(email: &str) -> ProfileBuilder {
19+
ProfileBuilder::new(email)
20+
}
21+
22+
/// Returns the URL of the Gravatar image.
23+
pub fn profile_url(self: &Self, format: Format) -> Url {
24+
let str = format!(
25+
"{}{}.{}",
26+
BASE_URL,
27+
email_hash(&self.email),
28+
format.to_string()
29+
);
30+
Url::parse(&str).unwrap()
31+
}
32+
}
33+
34+
// Builder for Profile instances.
35+
#[derive(core::default::Default)]
36+
pub struct ProfileBuilder {
37+
email: String,
38+
}
39+
40+
impl ProfileBuilder {
41+
pub fn new(email: &str) -> ProfileBuilder {
42+
ProfileBuilder {
43+
email: email.to_string(),
44+
..core::default::Default::default()
45+
}
46+
}
47+
/// Builds the Profile instance.
48+
pub fn build(self) -> Profile {
49+
Profile { email: self.email }
50+
}
51+
}
52+
53+
#[cfg(test)]
54+
mod tests {
55+
use super::*;
56+
57+
#[test]
58+
fn test_profile_builder() {
59+
let email = "[email protected]";
60+
61+
let builder = Profile::builder(email);
62+
let profile = builder.build();
63+
assert_eq!(email, profile.email);
64+
assert_eq!("https://gravatar.com/f807b5609eae64257bf4877652ea49fee40ac2451c152c12fa596ffeda647157.json", profile.profile_url(Format::Json).to_string());
65+
assert_eq!("https://gravatar.com/f807b5609eae64257bf4877652ea49fee40ac2451c152c12fa596ffeda647157.xml", profile.profile_url(Format::Xml).to_string());
66+
assert_eq!("https://gravatar.com/f807b5609eae64257bf4877652ea49fee40ac2451c152c12fa596ffeda647157.php", profile.profile_url(Format::Php).to_string());
67+
assert_eq!("https://gravatar.com/f807b5609eae64257bf4877652ea49fee40ac2451c152c12fa596ffeda647157.vcf", profile.profile_url(Format::Vcard).to_string());
68+
assert_eq!("https://gravatar.com/f807b5609eae64257bf4877652ea49fee40ac2451c152c12fa596ffeda647157.qr", profile.profile_url(Format::QrCode).to_string());
69+
}
70+
}

0 commit comments

Comments
 (0)