Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin P. Jung <[email protected]>
  • Loading branch information
headcr4sh committed Aug 29, 2024
0 parents commit de456aa
Show file tree
Hide file tree
Showing 13 changed files with 530 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Build

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
157 changes: 157 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "gravatar_api"
version = "0.1.0"
edition = "2021"

[dependencies]
sha2 = "0.9.1"
url = "2"
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Gravatar API Client for Rust

## Example Usage

```rust
use gravatar_api::avatars;

fn main() {
println!(
"{}",
avatars::Avatar::builder("[email protected]")
.size(512)
.default(avatars::Default::RoboHash)
.rating(avatars::Rating::G)
.build()
.image_url()
);
}
``
33 changes: 33 additions & 0 deletions src/avatar/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// The default image to display if the user's email does not have a Gravatar.
///
/// See <https://en.gravatar.com/site/implement/images/#default-image>.
#[derive(Clone, Debug)]
pub enum Default {
/// The URL of an image file to display as the default.
ImageUrl(String),

/// Instead of loading an image, the Gravatar URL will return an HTTP 404 (File Not Found)
/// response if the email is not found.
Http404,

/// A transparent PNG image.
Blank,

/// A simple, cartoon-style silhouetted outline of a person that does not vary by email hash.
MysteryPerson,

/// A geometric pattern based on the email hash.
Identicon,

/// A "monster" with different colors, faces, etc. that are generated by the email hash.
MonsterId,

/// A face with different features and backgrounds, generated by the email hash.
Wavatar,

/// An 8-bit arcade-style pixelated face that is generated by the email hash.
Retro,

/// A generated robot with different colors, faces, etc.
RoboHash,
}
23 changes: 23 additions & 0 deletions src/avatar/rating.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// The maximum rating level for which Gravatar will show the user's image instead of the specified
/// default.
///
/// See <https://en.gravatar.com/site/implement/images/#rating>.
#[derive(Clone, Debug)]
pub enum Rating {
/// Show "G"-rated images only,
/// suitable for display on all websites with any audience type.
G,

/// Show "PG"-rated images or lower only,
/// may contain rude gestures, provocatively dressed individuals, the lesser swear words,
/// or mild violence.
Pg,

/// Show "R"-rated images or lower only,
/// may contain such things as harsh profanity, intense violence, nudity, or hard drug use.
R,

/// Show all images, up to and including "X"-rated ones,
/// may contain sexual imagery or extremely disturbing violence.
X,
}
76 changes: 76 additions & 0 deletions src/avatars/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::fmt;
use url;

/// The default image to display if the user's email does not have a Gravatar.
///
/// See <https://en.gravatar.com/site/implement/images/#default-image>.
#[derive(Clone, Debug)]
pub enum Default {
/// The URL of an image file to display as the default.
ImageUrl(String),

/// Instead of loading an image, the Gravatar URL will return an HTTP 404 (File Not Found)
/// response if the email is not found.
Http404,

/// A transparent PNG image.
Blank,

/// A simple, cartoon-style silhouetted outline of a person that does not vary by email hash.
MysteryPerson,

/// A geometric pattern based on the email hash.
Identicon,

/// A "monster" with different colors, faces, etc. that are generated by the email hash.
MonsterId,

/// A face with different features and backgrounds, generated by the email hash.
Wavatar,

/// An 8-bit arcade-style pixelated face that is generated by the email hash.
Retro,

/// A generated robot with different colors, faces, etc.
RoboHash,
}

impl std::fmt::Display for Default {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let val = match self {
Default::Http404 => "404",
Default::MysteryPerson => "mp",
Default::Identicon => "identicon",
Default::MonsterId => "monsterid",
Default::Wavatar => "wavatar",
Default::Retro => "retro",
Default::RoboHash => "robohash",
Default::Blank => "blank",
Default::ImageUrl(ref u) => {
&url::form_urlencoded::byte_serialize(u.as_bytes()).collect::<String>()
}
};
f.write_str(val)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_to_string() {
assert_eq!(Default::Http404.to_string(), "404");
assert_eq!(Default::MysteryPerson.to_string(), "mp");
assert_eq!(Default::Identicon.to_string(), "identicon");
assert_eq!(Default::MonsterId.to_string(), "monsterid");
assert_eq!(Default::Wavatar.to_string(), "wavatar");
assert_eq!(Default::Retro.to_string(), "retro");
assert_eq!(Default::RoboHash.to_string(), "robohash");
assert_eq!(Default::Blank.to_string(), "blank");
assert_eq!(
Default::ImageUrl("[email protected]".to_string()).to_string(),
"anonymous%40example.com"
);
}
}
Loading

0 comments on commit de456aa

Please sign in to comment.