Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Avatar.image_url/1 that takes a map #549

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 164 additions & 18 deletions lib/faker/avatar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,26 @@ defmodule Faker.Avatar do
iex> Faker.Avatar.image_url()
"https://robohash.org/set_set1/bgset_bg2/kQqaIfGqxsjFoNIT"
iex> Faker.Avatar.image_url()
"https://robohash.org/set_set2/bgset_bg2/6"
"https://robohash.org/set_set1/bgset_bg2/6"
iex> Faker.Avatar.image_url()
"https://robohash.org/set_set2/bgset_bg2/J"
"https://robohash.org/set_set1/bgset_bg2/J"
iex> Faker.Avatar.image_url()
"https://robohash.org/set_set3/bgset_bg1/JNth88PrhGDhwp4LNQMt"
"""
@spec image_url() :: String.t()
def image_url do
"https://robohash.org#{set()}#{bg()}/#{Lorem.characters(1..20)}"
image_url_with_opts()
end

@doc """
Return avatar url for given `slug`.
Return avatar url for given `slug`, `set`, and `bg`, with size `width` x `height` pixels.
Valid parameters are:

- `slug: string()` - slug to generate avatar image for
- `set: integer() | :random` - set number from 1 to 5
- `bg: integer() | :random` - background number from 1 to 2
- `width: integer()` - image width
- `height: integer()` - image height (defaults to `width`)

## Examples

Expand All @@ -37,9 +44,31 @@ defmodule Faker.Avatar do
"https://robohash.org/plug"
iex> Faker.Avatar.image_url('ecto')
"https://robohash.org/ecto"
iex> Faker.Avatar.image_url(%{slug: 'phoenix', width: 100, set: 5})
"https://robohash.org/set_set5/phoenix?size=100x100"
"""
@spec image_url(binary) :: String.t()
def image_url(slug) do
@spec image_url(binary | charlist | %{
set: integer | :random,
bg: integer | :random,
width: integer,
height: integer,
slug: String.t()
}) :: String.t()
def image_url(%{} = params) do
set = Map.get(params, :set)
set = set && "/set_set#{set}"
set = set == :random && set() || set
bg = Map.get(params, :bg)
bg = bg && "/bgset_bg#{bg}"
bg = bg == :random && bg() || bg
width = Map.get(params, :width)
height = Map.get(params, :height) || width
size = width && "?size=#{width}x#{height}"
slug = Map.get(params, :slug) || Lorem.characters(1..20)

"https://robohash.org#{set}#{bg}/#{slug}#{size}"
end
def image_url(slug) when is_binary(slug) or is_list(slug) do
"https://robohash.org/#{slug}"
end

Expand All @@ -50,19 +79,26 @@ defmodule Faker.Avatar do
## Examples

iex> Faker.Avatar.image_url(200, 200)
"https://robohash.org/set_set2/bgset_bg2/ppkQqaIfGqx?size=200x200"
"https://robohash.org/set_set3/bgset_bg2/ppkQqaIfGqx?size=200x200"
iex> Faker.Avatar.image_url(800, 600)
"https://robohash.org/set_set2/bgset_bg2/oNITNnu6?size=800x600"
"https://robohash.org/set_set1/bgset_bg2/oNITNnu6?size=800x600"
iex> Faker.Avatar.image_url(32, 32)
"https://robohash.org/set_set3/bgset_bg1/J?size=32x32"
iex> Faker.Avatar.image_url(128, 128)
"https://robohash.org/set_set1/bgset_bg2/JNth88PrhGDhwp4LNQMt?size=128x128"
"https://robohash.org/set_set2/bgset_bg2/JNth88PrhGDhwp4LNQMt?size=128x128"
"""
@spec image_url(integer, integer) :: String.t()
def image_url(width, height)
when is_integer(width) and is_integer(height) do
slug = Lorem.characters(1..20)
"https://robohash.org#{set()}#{bg()}/#{slug}?size=#{width}x#{height}"
image_url_with_opts(
ssl: false,
raw: false,
set: nil,
bg: nil,
slug: nil,
height: height,
width: width
)
end

@doc """
Expand All @@ -82,21 +118,131 @@ defmodule Faker.Avatar do
@spec image_url(binary, integer, integer) :: String.t()
def image_url(slug, width, height)
when is_integer(width) and is_integer(height) do
"https://robohash.org/#{slug}?size=#{width}x#{height}"
image_url_with_opts(
ssl: false,
raw: true,
set: nil,
bg: nil,
slug: slug,
height: height,
width: width
)
end

@doc """
Return avatar url for given set of options. This gives you more control of how the
url is generated.

Options -
* ssl - boolean - true - returns an https:// url
* bg - integer - the background set to use for the image
* set - integer - the image set to use for the image
* slug - string - a string used to generate the hash
* raw - boolean - true - does not generate a random set/bg for the image
* height - integer - the height in pixels of the image
* width - integer - the width in pixels of the image


"""
@type ssl_option :: {:ssl, boolean}
@type raw_option :: {:raw, boolean}
@type set_option :: {:set, integer | nil}
@type bg_option :: {:bg, integer | nil}
@type height_option :: {:height, integer | nil}
@type width_option :: {:width, integer | nil}
@type slug_option :: {:slug, String.t() | nil}

@spec image_url_with_opts([
ssl_option
| raw_option
| set_option
| bg_option
| height_option
| width_option
| slug_option
]) :: String.t()
def image_url_with_opts(
opts \\ [
ssl: false,
set: nil,
bg: nil,
raw: false,
slug: nil,
height: nil,
width: nil
]
) do
opt_map =
Enum.into(
opts,
%{
ssl: false,
set: nil,
bg: nil,
raw: false,
slug: nil,
height: nil,
width: nil
}
)

"#{robohash_url(opt_map[:ssl])}#{set_url(opt_map)}#{bg_url(opt_map)}/#{slug(opt_map)}#{
query(opt_map)
}"
end

defp slug(%{slug: nil}), do: random_slug()
defp slug(%{slug: value}), do: value
defp slug(_), do: random_slug()

defp query(%{height: height, width: width}) when is_integer(height) and is_integer(width) do
"?size=#{width}x#{height}"
end

defp query(_), do: ""

defp random_slug, do: "#{Lorem.characters(1..20)}"
defp set_url(%{raw: true}), do: ""
defp set_url(%{raw: false, set: nil}), do: set()
defp set_url(%{raw: false, set: set_value}), do: set_to_url(set_value)
defp bg_url(%{raw: true}), do: ""
defp bg_url(%{raw: false, bg: nil}), do: bg()
defp bg_url(%{raw: false, bg: bg_value}), do: bg_to_url(bg_value)

defp robohash_url(true) do
"https://robohash.org"
end

defp robohash_url(_) do
"http://robohash.org"
end

defp bg_to_url(number) do
case number do
2 -> "/bgset_bg2"
_ -> "/bgset_bg1"
end
end

defp set_to_url(number) do
case number do
2 -> "/set_set2"
3 -> "/set_set3"
_ -> "/set_set1"
end
end

defp bg do
%{
0 => "/bgset_bg1",
1 => "/bgset_bg2"
}[Faker.random_between(0, 1)]
bg_to_url(Faker.random_between(1, 2))
end

defp set do
%{
0 => "/set_set1",
1 => "/set_set2",
2 => "/set_set3"
}[Faker.random_between(0, 2)]
2 => "/set_set3",
3 => "/set_set4",
4 => "/set_set5"
}[Faker.random_between(0, 4)]
end
end
28 changes: 28 additions & 0 deletions test/faker/avatar_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ defmodule Faker.AvatarTest do
test "image_url/1" do
assert String.starts_with?(image_url("myslug"), "https://robohash.org/")
assert String.contains?(image_url("myslug"), "myslug")

assert image_url(%{slug: "myslug", set: 5}) == "https://robohash.org/set_set5/myslug"
end

test "image_url/2" do
Expand All @@ -22,4 +24,30 @@ defmodule Faker.AvatarTest do
assert String.contains?(image_url("myslug", 70, 20), "70x20")
assert String.contains?(image_url("myslug", 70, 20), "myslug")
end

test "image_url_with/0" do
assert String.starts_with?(image_url_with_opts(), "http://robohash.org/")
assert String.contains?(image_url_with_opts(), "/set_set")
assert String.contains?(image_url_with_opts(), "/bgset_bg")
end

test "image_url_with_opts/1" do
assert image_url_with_opts(ssl: true, set: 2, bg: 2, slug: "hello", height: 20, width: 10) ==
"https://robohash.org/set_set2/bgset_bg2/hello?size=10x20"

assert image_url_with_opts(ssl: false, set: 2, bg: 2, slug: "hello", height: 20, width: 10) ==
"http://robohash.org/set_set2/bgset_bg2/hello?size=10x20"

assert String.starts_with?(
image_url_with_opts(ssl: true, slug: "hello", height: 20, width: 10),
"https://robohash.org/"
)

assert String.contains?(
image_url_with_opts(ssl: true, slug: "hello", height: 20, width: 10),
"hello?size=10x20"
)

assert String.contains?(image_url_with_opts(ssl: true, slug: "hello"), "hello")
end
end