Skip to content

Instantly share code, notes, and snippets.

@gausby
Last active November 11, 2015 17:40
Show Gist options
  • Save gausby/ae9c1f868dea03941ed7 to your computer and use it in GitHub Desktop.
Save gausby/ae9c1f868dea03941ed7 to your computer and use it in GitHub Desktop.
An Elixir port of the node.js module 'gravatar-url'. People often ask «where are the packages for Elixir?» Often the answer is «they are baked in; for anything else try out Hex.pm»
defmodule Gravatar do
@base_url "https://secure.gravatar.com/avatar/"
@doc """
Will get the corresponding gravatar url for a given email address. Options
can be passed in using a keyword list or a map.
iex> Gravatar.url("martin@gausby.dk", size: 200)
https://secure.gravatar.com/avatar/0e6a9f19e77fa18bf6f185258f2507d6?size=200
Read this site for a list of options:
https://secure.gravatar.com/site/implement/images/
"""
@spec url(String.t, Map) :: String.t
def url(email, opts \\ []) do
email = String.downcase(email)
hash = :crypto.hash(:md5, email) |> Base.encode16(case: :lower)
"#{@base_url}#{hash}?#{URI.encode_query(opts)}"
end
end
@gausby
Copy link
Author

gausby commented Nov 11, 2015

If you are looking for a gravatar module for your own Elixir project you should check out https://github.com/scrogson/exgravatar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment