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 lorempixel #759

Merged
merged 2 commits into from
Dec 17, 2016
Merged
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Contents
- [Faker::GameOfThrones](#fakergameofthrones)
- [Faker::Pokemon](#fakerpokemon)
- [Faker::Food](#fakerfood)
- [Faker::LoremPixel](#fakerlorempixel)
- [Faker::Ancient](#fakerancient)
- [Customization](#customization)
- [Contributing](#contributing)
Expand Down Expand Up @@ -897,6 +898,26 @@ 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"
```

###Faker::Ancient
----------------

Expand Down
1 change: 1 addition & 0 deletions lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 'faker/esport'
require 'faker/bank'
require 'faker/ancient'
Expand Down
21 changes: 21 additions & 0 deletions lib/faker/lorem_pixel.rb
Original file line number Diff line number Diff line change
@@ -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
66 changes: 66 additions & 0 deletions test/test_lorem_pixel.rb
Original file line number Diff line number Diff line change
@@ -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