-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
270 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package gravatar | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const ( | ||
defaultScheme = "https" | ||
defaultHostname = "gravatar.loli.net" | ||
) | ||
|
||
func NewGravatarFromEmail(email string) Gravatar { | ||
hasher := md5.Sum([]byte(strings.TrimSpace(email))) | ||
hash := hex.EncodeToString(hasher[:]) | ||
|
||
g := NewGravatar() | ||
g.Hash = hash | ||
g.Default = "retro" | ||
return g | ||
} | ||
|
||
func NewGravatar() Gravatar { | ||
return Gravatar{ | ||
Scheme: defaultScheme, | ||
Host: defaultHostname, | ||
} | ||
} | ||
|
||
type Gravatar struct { | ||
Scheme string | ||
Host string | ||
Hash string | ||
Default string | ||
Rating string | ||
Size int | ||
} | ||
|
||
func (g Gravatar) GetURL() string { | ||
path := "/avatar/" + g.Hash | ||
|
||
v := url.Values{} | ||
if g.Size > 0 { | ||
v.Add("s", strconv.Itoa(g.Size)) | ||
} | ||
|
||
if g.Rating != "" { | ||
v.Add("r", g.Rating) | ||
} | ||
|
||
if g.Default != "" { | ||
v.Add("d", g.Default) | ||
} | ||
|
||
url := url.URL{ | ||
Scheme: g.Scheme, | ||
Host: g.Host, | ||
Path: path, | ||
RawQuery: v.Encode(), | ||
} | ||
|
||
return url.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.