From c331acaa376ee3df45e920df39f80176a060e765 Mon Sep 17 00:00:00 2001 From: Nicolas Brousse Date: Sun, 29 Jul 2018 16:45:21 +0200 Subject: [PATCH 1/2] Update Placeholdit doc Show how to use Placehold to download a file from Placeholdit url. --- doc/placeholdit.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/doc/placeholdit.md b/doc/placeholdit.md index ca23bca037..e28342f328 100644 --- a/doc/placeholdit.md +++ b/doc/placeholdit.md @@ -17,3 +17,18 @@ Faker::Placeholdit.image("50x50", 'jpeg', :random, :random) #=> "http://placehol Faker::Placeholdit.image("50x50", 'jpg', 'ffffff', '000', 'Some Custom Text') #=> "http://placehold.it/50x50.jpg/ffffff/000?text='Some Custom Text'" ``` + +## Tips + +If you want to have this file downloaded, like in your tests, you could use this following piece of code: + +```ruby +def image_file(size = '300x300', format = 'png', background_color = nil, text_color = nil, text = nil) + file = Tempfile.new("faker_placeholdit") + file.binmode + file << Net::HTTP.get(URI(Faker::Placeholdit.image(size, format, background_color, text_color, text))) + file.close + + ::File.new(file.path) +end +``` From 400a6b6f02b4bd8789c2158c7274bcd53bf937e7 Mon Sep 17 00:00:00 2001 From: Nicolas Brousse Date: Sun, 29 Jul 2018 16:46:15 +0200 Subject: [PATCH 2/2] Code cleanup --- doc/placeholdit.md | 14 +++++++------- lib/faker/placeholdit.rb | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/doc/placeholdit.md b/doc/placeholdit.md index e28342f328..cf0035eb64 100644 --- a/doc/placeholdit.md +++ b/doc/placeholdit.md @@ -3,19 +3,19 @@ ```ruby Faker::Placeholdit.image #=> "http://placehold.it/300x300.png/000" -Faker::Placeholdit.image("50x50") #=> "http://placehold.it/50x50.png/000" +Faker::Placeholdit.image('50x50') #=> "http://placehold.it/50x50.png/000" -Faker::Placeholdit.image("50x50", 'jpg') #=> "http://placehold.it/50x50.jpg/000" +Faker::Placeholdit.image('50x50', 'jpg') #=> "http://placehold.it/50x50.jpg/000" -Faker::Placeholdit.image("50x50", 'gif', 'ffffff') #=> "http://placehold.it/50x50.gif/ffffff" +Faker::Placeholdit.image('50x50', 'gif', 'ffffff') #=> "http://placehold.it/50x50.gif/ffffff" -Faker::Placeholdit.image("50x50", 'jpeg', :random) #=> "http://placehold.it/50x50.jpeg/39eba7" +Faker::Placeholdit.image('50x50', 'jpeg', :random) #=> "http://placehold.it/50x50.jpeg/39eba7" -Faker::Placeholdit.image("50x50", 'jpeg', 'ffffff', '000') #=> "http://placehold.it/50x50.jpeg/ffffff/000" +Faker::Placeholdit.image('50x50', 'jpeg', 'ffffff', '000') #=> "http://placehold.it/50x50.jpeg/ffffff/000" -Faker::Placeholdit.image("50x50", 'jpeg', :random, :random) #=> "http://placehold.it/50x50.jpeg/d39e44/888ba7" +Faker::Placeholdit.image('50x50', 'jpeg', :random, :random) #=> "http://placehold.it/50x50.jpeg/d39e44/888ba7" -Faker::Placeholdit.image("50x50", 'jpg', 'ffffff', '000', 'Some Custom Text') #=> "http://placehold.it/50x50.jpg/ffffff/000?text='Some Custom Text'" +Faker::Placeholdit.image('50x50', 'jpg', 'ffffff', '000', 'Some Custom Text') #=> "http://placehold.it/50x50.jpg/ffffff/000?text='Some Custom Text'" ``` ## Tips diff --git a/lib/faker/placeholdit.rb b/lib/faker/placeholdit.rb index bcc304648a..a798501151 100644 --- a/lib/faker/placeholdit.rb +++ b/lib/faker/placeholdit.rb @@ -11,8 +11,8 @@ def image(size = '300x300', format = 'png', background_color = nil, text_color = raise ArgumentError, 'Size should be specified in format 300x300' unless size =~ /^[0-9]+x[0-9]+$/ raise ArgumentError, "Supported formats are #{SUPPORTED_FORMATS.join(', ')}" unless SUPPORTED_FORMATS.include?(format) - raise ArgumentError, "background_color must be a hex value without '#'" unless background_color.nil? || background_color.match(/((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/) - raise ArgumentError, "text_color must be a hex value without '#'" unless text_color.nil? || text_color.match(/((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/) + raise ArgumentError, "background_color must be a hex value without '#'" unless background_color.nil? || background_color =~ /((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/ + raise ArgumentError, "text_color must be a hex value without '#'" unless text_color.nil? || text_color =~ /((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/ image_url = "https://placehold.it/#{size}.#{format}" image_url += "/#{background_color}" if background_color