Skip to content

Commit 6628f11

Browse files
committed
Initial commit
Signed-off-by: Benjamin P. Jung <[email protected]>
0 parents  commit 6628f11

File tree

14 files changed

+559
-0
lines changed

14 files changed

+559
-0
lines changed

.github/workflows/build.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
pull_request:
7+
branches: ["main"]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Build
19+
run: cargo build --verbose
20+
- name: Run tests
21+
run: cargo test --verbose

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.lock

+157
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "gravatar_api"
3+
version = "0.1.0"
4+
authors = ["Benjamin P. Jung <[email protected]>"]
5+
description = "Access to the public Gravatar API"
6+
edition = "2021"
7+
license = "MIT"
8+
repository = "https://github.com/cathive/rust-gravatar-api"
9+
10+
[dependencies]
11+
sha2 = "0.9.1"
12+
url = "2"

LICENSE

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright (c) 2013-2022 The rust-url developers
2+
3+
Permission is hereby granted, free of charge, to any
4+
person obtaining a copy of this software and associated
5+
documentation files (the "Software"), to deal in the
6+
Software without restriction, including without
7+
limitation the rights to use, copy, modify, merge,
8+
publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software
10+
is furnished to do so, subject to the following
11+
conditions:
12+
13+
The above copyright notice and this permission notice
14+
shall be included in all copies or substantial portions
15+
of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19+
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21+
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24+
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25+
DEALINGS IN THE SOFTWARE.

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Gravatar API Client for Rust
2+
3+
## Example Usage
4+
5+
```rust
6+
use gravatar_api::avatars;
7+
8+
fn main() {
9+
println!(
10+
"{}",
11+
avatars::Avatar::builder("[email protected]")
12+
.size(512)
13+
.default(avatars::Default::RoboHash)
14+
.rating(avatars::Rating::G)
15+
.build()
16+
.image_url()
17+
);
18+
}
19+
``

src/avatar/default.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// The default image to display if the user's email does not have a Gravatar.
2+
///
3+
/// See <https://en.gravatar.com/site/implement/images/#default-image>.
4+
#[derive(Clone, Debug)]
5+
pub enum Default {
6+
/// The URL of an image file to display as the default.
7+
ImageUrl(String),
8+
9+
/// Instead of loading an image, the Gravatar URL will return an HTTP 404 (File Not Found)
10+
/// response if the email is not found.
11+
Http404,
12+
13+
/// A transparent PNG image.
14+
Blank,
15+
16+
/// A simple, cartoon-style silhouetted outline of a person that does not vary by email hash.
17+
MysteryPerson,
18+
19+
/// A geometric pattern based on the email hash.
20+
Identicon,
21+
22+
/// A "monster" with different colors, faces, etc. that are generated by the email hash.
23+
MonsterId,
24+
25+
/// A face with different features and backgrounds, generated by the email hash.
26+
Wavatar,
27+
28+
/// An 8-bit arcade-style pixelated face that is generated by the email hash.
29+
Retro,
30+
31+
/// A generated robot with different colors, faces, etc.
32+
RoboHash,
33+
}

src/avatar/rating.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// The maximum rating level for which Gravatar will show the user's image instead of the specified
2+
/// default.
3+
///
4+
/// See <https://en.gravatar.com/site/implement/images/#rating>.
5+
#[derive(Clone, Debug)]
6+
pub enum Rating {
7+
/// Show "G"-rated images only,
8+
/// suitable for display on all websites with any audience type.
9+
G,
10+
11+
/// Show "PG"-rated images or lower only,
12+
/// may contain rude gestures, provocatively dressed individuals, the lesser swear words,
13+
/// or mild violence.
14+
Pg,
15+
16+
/// Show "R"-rated images or lower only,
17+
/// may contain such things as harsh profanity, intense violence, nudity, or hard drug use.
18+
R,
19+
20+
/// Show all images, up to and including "X"-rated ones,
21+
/// may contain sexual imagery or extremely disturbing violence.
22+
X,
23+
}

src/avatars/default.rs

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use std::fmt;
2+
use url;
3+
4+
/// The default image to display if the user's email does not have a Gravatar.
5+
///
6+
/// See <https://en.gravatar.com/site/implement/images/#default-image>.
7+
#[derive(Clone, Debug)]
8+
pub enum Default {
9+
/// The URL of an image file to display as the default.
10+
ImageUrl(String),
11+
12+
/// Instead of loading an image, the Gravatar URL will return an HTTP 404 (File Not Found)
13+
/// response if the email is not found.
14+
Http404,
15+
16+
/// A transparent PNG image.
17+
Blank,
18+
19+
/// A simple, cartoon-style silhouetted outline of a person that does not vary by email hash.
20+
MysteryPerson,
21+
22+
/// A geometric pattern based on the email hash.
23+
Identicon,
24+
25+
/// A "monster" with different colors, faces, etc. that are generated by the email hash.
26+
MonsterId,
27+
28+
/// A face with different features and backgrounds, generated by the email hash.
29+
Wavatar,
30+
31+
/// An 8-bit arcade-style pixelated face that is generated by the email hash.
32+
Retro,
33+
34+
/// A generated robot with different colors, faces, etc.
35+
RoboHash,
36+
}
37+
38+
impl std::fmt::Display for Default {
39+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40+
let val = match self {
41+
Default::Http404 => "404",
42+
Default::MysteryPerson => "mp",
43+
Default::Identicon => "identicon",
44+
Default::MonsterId => "monsterid",
45+
Default::Wavatar => "wavatar",
46+
Default::Retro => "retro",
47+
Default::RoboHash => "robohash",
48+
Default::Blank => "blank",
49+
Default::ImageUrl(ref u) => {
50+
&url::form_urlencoded::byte_serialize(u.as_bytes()).collect::<String>()
51+
}
52+
};
53+
f.write_str(val)
54+
}
55+
}
56+
57+
#[cfg(test)]
58+
mod tests {
59+
use super::*;
60+
61+
#[test]
62+
fn test_to_string() {
63+
assert_eq!(Default::Http404.to_string(), "404");
64+
assert_eq!(Default::MysteryPerson.to_string(), "mp");
65+
assert_eq!(Default::Identicon.to_string(), "identicon");
66+
assert_eq!(Default::MonsterId.to_string(), "monsterid");
67+
assert_eq!(Default::Wavatar.to_string(), "wavatar");
68+
assert_eq!(Default::Retro.to_string(), "retro");
69+
assert_eq!(Default::RoboHash.to_string(), "robohash");
70+
assert_eq!(Default::Blank.to_string(), "blank");
71+
assert_eq!(
72+
Default::ImageUrl("[email protected]".to_string()).to_string(),
73+
"anonymous%40example.com"
74+
);
75+
}
76+
}

0 commit comments

Comments
 (0)