From 076b623e30b73e5d91ce5e044d257351850f3a2e Mon Sep 17 00:00:00 2001 From: Denis Talakevich Date: Wed, 7 Dec 2016 15:51:04 +0200 Subject: [PATCH] add http://lorempixel.com --- README.md | 22 ++++++++++++++ lib/faker.rb | 1 + lib/faker/lorem_pixel.rb | 21 +++++++++++++ test/test_lorem_pixel.rb | 66 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 lib/faker/lorem_pixel.rb create mode 100644 test/test_lorem_pixel.rb diff --git a/README.md b/README.md index 5152fbb6fd..73c891e4b5 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Contents - [Faker::GameOfThrones](#fakergameofthrones) - [Faker::Pokemon](#fakerpokemon) - [Faker::Food](#fakerfood) + - [Faker::LoremPixel](#fakerlorempixel) - [Customization](#customization) - [Contributing](#contributing) - [Contact](#contact) @@ -878,6 +879,27 @@ Faker::Food.spice #=> "Caraway Seed" Faker::Food.measurement #=> "1/4 tablespoon" ``` +###Faker::LoremPixel +---------------- + +```ruby + +Faker::LoremPixel.image #=> "http://lorempixel.com/300/300" + +Faker::LoremPixel.image("50x60") #=> "http://lorempixel.com/50/60" + +Faker::LoremPixel.image("50x60", true) #=> "http://lorempixel.com/g/50/60" + +Faker::LoremPixel.image("50x60", false, 'sports') #=> "http://lorempixel.com/50/60/sports" + +Faker::LoremPixel.image("50x60", false, 'sports', 3) #=> "http://lorempixel.com/50/60/sports/3" + +Faker::LoremPixel.image("50x60", false, 'sports', 3, 'Dummy-text') #=> "http://lorempixel.com/50/60/sports/3/Dummy-text" + +Faker::LoremPixel.image("50x60", false, 'sports', nil, 'Dummy-text') #=> "http://lorempixel.com/50/60/sports/Dummy-text" + +``` + Customization ------------ Since you may want to make addresses and other types of data look different diff --git a/lib/faker.rb b/lib/faker.rb index 424200ddf3..458455b6ab 100644 --- a/lib/faker.rb +++ b/lib/faker.rb @@ -230,6 +230,7 @@ def rand_in_range(from, to) require 'faker/game_of_thrones' require 'faker/pokemon' require 'faker/food' +require 'faker/lorem_pixel' require 'extensions/array' require 'extensions/symbol' diff --git a/lib/faker/lorem_pixel.rb b/lib/faker/lorem_pixel.rb new file mode 100644 index 0000000000..a667522a00 --- /dev/null +++ b/lib/faker/lorem_pixel.rb @@ -0,0 +1,21 @@ +module Faker + class LoremPixel < Base + class << self + SUPPORTED_CATEGORIES = %w(abstract animals business cats city food nightlife fashion people nature sports technics transport) + + def image(size = '300x300', is_gray = false, category = nil, number = nil, text = nil) + raise ArgumentError, 'Size should be specified in format 300x300' unless size.match(/^[0-9]+x[0-9]+$/) + raise ArgumentError, "Supported categories are #{SUPPORTED_CATEGORIES.join(', ')}" unless category.nil? || SUPPORTED_CATEGORIES.include?(category) + raise ArgumentError, 'Category required when number is passed' if !number.nil? && category.nil? + raise ArgumentError, 'Number must be between 1 and 10' unless number.nil? || (1..10).include?(number) + raise ArgumentError, 'Category and number must be passed when text is passed' if !text.nil? && number.nil? && category.nil? + + url_parts = ['http://lorempixel.com'] + url_parts << 'g' if is_gray + url_parts += size.split('x') + url_parts += [category, number, text].compact + url_parts.join('/') + end + end + end +end diff --git a/test/test_lorem_pixel.rb b/test/test_lorem_pixel.rb new file mode 100644 index 0000000000..5558efc8c9 --- /dev/null +++ b/test/test_lorem_pixel.rb @@ -0,0 +1,66 @@ +require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb') + +class TestLoremPixel < Test::Unit::TestCase + def setup + @tester = Faker::LoremPixel + end + + def test_placeholdit + assert @tester.image.match(/http:\/\/lorempixel\.com\/(\d+\/\d+)/)[1] != nil + end + + def test_image_with_custom_size + assert @tester.image('3x3').match(/http:\/\/lorempixel\.com\/(\d+\/\d+)/)[1] == '3/3' + end + + def test_image_with_incorrect_size + assert_raise ArgumentError do + @tester.image('300x300s') + end + end + + def test_image_gray + assert @tester.image('300x300', true).match(/http:\/\/lorempixel\.com\/g\/\d+\/\d+/) + end + + def test_image_with_supported_category + assert @tester.image('300x300', false, 'animals').match(/http:\/\/lorempixel\.com\/\d+\/\d+\/(.*)/)[1] == 'animals' + end + + def test_image_with_incorrect_category + assert_raise ArgumentError do + @tester.image('300x300', false, 'wrong_category') + end + end + + def test_image_with_supported_category_and_correct_number + assert @tester.image('300x300', false, 'animals', 3).match(/http:\/\/lorempixel\.com\/\d+\/\d+\/.+\/(\d+)/)[1] == '3' + end + + def test_image_with_supported_category_and_incorrect_number + assert_raise ArgumentError do + @tester.image('300x300', false, 'animals', 11) + end + end + + def test_image_with_correct_number_and_without_category + assert_raise ArgumentError do + @tester.image('300x300', false, 'wrong_category', 3) + end + end + + def test_image_with_text_correct_number_and_supported_category + assert @tester.image('300x300', false, 'animals', 3, 'Dummy-text').match(/http:\/\/lorempixel\.com\/\d+\/\d+\/.+\/3\/(.+)/)[1] == 'Dummy-text' + end + + def test_image_with_text_supported_category_and_text_without_number + assert @tester.image('300x300', false, 'animals', nil, 'Dummy-text').match(/http:\/\/lorempixel\.com\/\d+\/\d+\/.+\/(.+)/)[1] == 'Dummy-text' + end + + def test_image_with_text_without_number_and_category + assert_raise ArgumentError do + @tester.image('300x300', false, nil, nil, 'Dummy-text') + end + end + +end