diff --git a/CHANGELOG.md b/CHANGELOG.md index 46b3d773e..6b8b9ffa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ Change log itself follows [Keep a CHANGELOG](http://keepachangelog.com) format. ## Unreleased + + ### Added ### Changed @@ -31,6 +33,9 @@ Change log itself follows [Keep a CHANGELOG](http://keepachangelog.com) format. - `Faker.Fruit.PtBr` [[@f-francine](https://github.com/f-francine)] - `Faker.Commerce.PtBr` [[@f-francine](https://github.com/f-francine)] +- `Faker.Avatar.image_url_with_opts/1` [[delmendo](https://github.com/delmendo)] + + ### Changed - `Faker.Vehicles` add makes and models that are multi-word, refactor existing fns [[jersearls](https://github.com/jersearls)] - `Faker.Avatar` switch to `https` to prevent redirect [[igas](https://github.com/igas)] diff --git a/lib/faker/avatar.ex b/lib/faker/avatar.ex index 9daff7050..b7be48b7d 100644 --- a/lib/faker/avatar.ex +++ b/lib/faker/avatar.ex @@ -21,7 +21,7 @@ defmodule Faker.Avatar do """ @spec image_url() :: String.t() def image_url do - "https://robohash.org#{set()}#{bg()}/#{Lorem.characters(1..20)}" + image_url_with_opts() end @doc """ @@ -40,7 +40,7 @@ defmodule Faker.Avatar do """ @spec image_url(binary) :: String.t() def image_url(slug) do - "https://robohash.org/#{slug}" + image_url_with_opts(slug: slug, ssl: false, raw: true) end @doc """ @@ -61,8 +61,15 @@ defmodule Faker.Avatar do @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 """ @@ -82,21 +89,125 @@ 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)] + set_to_url(Faker.random_between(1, 3)) end end diff --git a/mix.lock b/mix.lock index 9055a5b86..53029a8cb 100644 --- a/mix.lock +++ b/mix.lock @@ -2,8 +2,8 @@ "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"}, "credo": {:hex, :credo, "1.6.4", "ddd474afb6e8c240313f3a7b0d025cc3213f0d171879429bf8535d7021d9ad78", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "c28f910b61e1ff829bffa056ef7293a8db50e87f2c57a9b5c3f57eee124536b7"}, "dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"}, - "earmark": {:hex, :earmark, "1.4.25", "43fd256e37f671528727745f0fc760227e16f40dced303216f766eade06b7b10", [:mix], [{:earmark_parser, "~> 1.4.25", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "073c606ea4c3cc52920e8ae8ebe788c6af03d3e39782b25e5d14ceaa2f692998"}, - "earmark_parser": {:hex, :earmark_parser, "1.4.25", "2024618731c55ebfcc5439d756852ec4e85978a39d0d58593763924d9a15916f", [:mix], [], "hexpm", "56749c5e1c59447f7b7a23ddb235e4b3defe276afc220a6227237f3efe83f51e"}, + "earmark": {:hex, :earmark, "1.4.37", "56ce845c543393aa3f9b294c818c3d783452a4a67e4ab18c4303a954a8b59363", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "d86d5e12868db86d5321b00e62a4bbcb4150346e4acc9a90a041fb188a5cb106"}, + "earmark_parser": {:hex, :earmark_parser, "1.4.31", "a93921cdc6b9b869f519213d5bc79d9e218ba768d7270d46fdcf1c01bacff9e2", [:mix], [], "hexpm", "317d367ee0335ef037a87e46c91a2269fef6306413f731e8ec11fc45a7efd059"}, "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, "ex_doc": {:hex, :ex_doc, "0.28.4", "001a0ea6beac2f810f1abc3dbf4b123e9593eaa5f00dd13ded024eae7c523298", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bf85d003dd34911d89c8ddb8bda1a958af3471a274a4c2150a9c01c78ac3f8ed"}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, diff --git a/test/faker/avatar_test.exs b/test/faker/avatar_test.exs index af7eae09b..604a47c8d 100644 --- a/test/faker/avatar_test.exs +++ b/test/faker/avatar_test.exs @@ -22,4 +22,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