From 96ceef464d909cc8f727dad9d0ba13f32be50c4b Mon Sep 17 00:00:00 2001 From: Dirk Elmendorf Date: Tue, 20 Oct 2020 10:17:49 -0500 Subject: [PATCH 1/5] feature: added in a way to control the options passed to the avatar via keywords --- lib/faker/avatar.ex | 136 ++++++++++++++++++++++++++++++++----- test/faker/avatar_test.exs | 46 ++++++++++--- 2 files changed, 153 insertions(+), 29 deletions(-) diff --git a/lib/faker/avatar.ex b/lib/faker/avatar.ex index dd79d7037..99b200b91 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 - "http://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 - "http://robohash.org/#{slug}" + image_url_with_opts(slug: slug, ssl: false, raw: true) end @doc """ @@ -50,19 +50,26 @@ defmodule Faker.Avatar do ## Examples iex> Faker.Avatar.image_url(200, 200) - "http://robohash.org/set_set2/bgset_bg2/ppkQqaIfGqx?size=200x200" + "http://robohash.org/set_set1/bgset_bg2/kQqaIfGqxsjFoNIT?size=200x200" iex> Faker.Avatar.image_url(800, 600) - "http://robohash.org/set_set2/bgset_bg2/oNITNnu6?size=800x600" + "http://robohash.org/set_set2/bgset_bg2/6?size=800x600" iex> Faker.Avatar.image_url(32, 32) - "http://robohash.org/set_set3/bgset_bg1/J?size=32x32" + "http://robohash.org/set_set2/bgset_bg2/J?size=32x32" iex> Faker.Avatar.image_url(128, 128) - "http://robohash.org/set_set1/bgset_bg2/JNth88PrhGDhwp4LNQMt?size=128x128" + "http://robohash.org/set_set3/bgset_bg1/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) - "http://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,114 @@ 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 - "http://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 + + + """ + @spec image_url_with_opts( + ssl: boolean(), + set: integer(), + bg: integer(), + raw: boolean(), + slug: String.t(), + height: integer(), + width: integer + ) :: 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/test/faker/avatar_test.exs b/test/faker/avatar_test.exs index 6f2e25447..8acde0e2f 100644 --- a/test/faker/avatar_test.exs +++ b/test/faker/avatar_test.exs @@ -9,17 +9,41 @@ defmodule Faker.AvatarTest do assert String.starts_with?(image_url(), "http://robohash.org/") end - test "image_url/1" do - assert String.starts_with?(image_url("myslug"), "http://robohash.org/") - assert String.contains?(image_url("myslug"), "myslug") - end + test "image_url/1" do + assert String.starts_with?(image_url("myslug"), "http://robohash.org/") + assert String.contains?(image_url("myslug"), "myslug") + end - test "image_url/2" do - assert String.contains?(image_url(40, 60), "40x60") - end + test "image_url/2" do + assert String.contains?(image_url(40, 60), "40x60") + end + + test "image_url/3" 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 - test "image_url/3" do - assert String.contains?(image_url("myslug", 70, 20), "70x20") - assert String.contains?(image_url("myslug", 70, 20), "myslug") - end end From bac293e49f5913bfed1e5a230b598f306f7c1523 Mon Sep 17 00:00:00 2001 From: Dirk Elmendorf Date: Tue, 20 Oct 2020 10:24:35 -0500 Subject: [PATCH 2/5] Added in a changelog line --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 914434321..d35226a15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,13 @@ Change log itself follows [Keep a CHANGELOG](http://keepachangelog.com) format. ## Unreleased + + ### Added +- `Faker.Avatar.image_url_with_opts/1` [[delmendo](https://github.com/delmendo)] + + ### Changed ### Deprecated From ceec4c75a3323d33c0cc309904f345911ae4ecbb Mon Sep 17 00:00:00 2001 From: Dirk Elmendorf Date: Tue, 20 Oct 2020 11:09:59 -0500 Subject: [PATCH 3/5] Fixed formatting Fixed credo issue with random_slug() --- lib/faker/avatar.ex | 2 +- test/faker/avatar_test.exs | 60 ++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/lib/faker/avatar.ex b/lib/faker/avatar.ex index 99b200b91..d3cab8598 100644 --- a/lib/faker/avatar.ex +++ b/lib/faker/avatar.ex @@ -161,7 +161,7 @@ defmodule Faker.Avatar do defp query(_), do: "" - defp random_slug(), do: "#{Lorem.characters(1..20)}" + 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) diff --git a/test/faker/avatar_test.exs b/test/faker/avatar_test.exs index 8acde0e2f..d8c29ac2f 100644 --- a/test/faker/avatar_test.exs +++ b/test/faker/avatar_test.exs @@ -9,41 +9,43 @@ defmodule Faker.AvatarTest do assert String.starts_with?(image_url(), "http://robohash.org/") end - test "image_url/1" do - assert String.starts_with?(image_url("myslug"), "http://robohash.org/") - assert String.contains?(image_url("myslug"), "myslug") - end - - test "image_url/2" do - assert String.contains?(image_url(40, 60), "40x60") - end - - test "image_url/3" 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 - + test "image_url/1" do + assert String.starts_with?(image_url("myslug"), "http://robohash.org/") + assert String.contains?(image_url("myslug"), "myslug") + end + test "image_url/2" do + assert String.contains?(image_url(40, 60), "40x60") + end - 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" + test "image_url/3" 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 - 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") + 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.contains?(image_url_with_opts(ssl: true, slug: "hello"), "hello") + assert String.starts_with?( + image_url_with_opts(ssl: true, slug: "hello", height: 20, width: 10), + "https://robohash.org/" + ) - end + 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 From 89bb937e1c24c50e6b22ccaf8b714ce43e7f951a Mon Sep 17 00:00:00 2001 From: Dirk Elmendorf Date: Tue, 20 Oct 2020 19:30:44 -0500 Subject: [PATCH 4/5] Fixing dialyzer type spec. --- lib/faker/avatar.ex | 47 ++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/faker/avatar.ex b/lib/faker/avatar.ex index d3cab8598..7fb05df3f 100644 --- a/lib/faker/avatar.ex +++ b/lib/faker/avatar.ex @@ -115,15 +115,23 @@ defmodule Faker.Avatar do """ - @spec image_url_with_opts( - ssl: boolean(), - set: integer(), - bg: integer(), - raw: boolean(), - slug: String.t(), - height: integer(), - width: integer - ) :: String.t() + @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, @@ -136,15 +144,18 @@ defmodule Faker.Avatar do ] ) do opt_map = - Enum.into(opts, %{ - ssl: false, - set: nil, - bg: nil, - raw: false, - slug: nil, - height: nil, - width: nil - }) + 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) From 41b276c20a9f0d2429a60bfdab3a57235a8d7728 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 6 Mar 2023 09:07:23 +0000 Subject: [PATCH 5/5] Bump earmark from 1.4.25 to 1.4.37 Bumps [earmark](https://github.com/pragdave/earmark) from 1.4.25 to 1.4.37. - [Release notes](https://github.com/pragdave/earmark/releases) - [Changelog](https://github.com/pragdave/earmark/blob/master/RELEASE.md) - [Commits](https://github.com/pragdave/earmark/compare/v1.4.25...v1.4.37) --- updated-dependencies: - dependency-name: earmark dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- mix.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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"},