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

Random color with Placeholdit #1179

Merged
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Change Log

## HEAD Unreleased
### Latest update: 2018-05-17
### Latest update: 2018-05-18

**Additions**

Expand Down Expand Up @@ -37,6 +37,7 @@
- [PR #1173](https://github.com/stympy/faker/pull/1173) Fix tests warning [@vbrazo](https://github.com/vbrazo)
- [PR #1193](https://github.com/stympy/faker/pull/1193) Add Faker::MichaelScott API [@snayrouz](https://github.com/snayrouz)
- [PR #818](https://github.com/stympy/faker/pull/818) LoremFlickr support [@mrstebo](https://github.com/mrstebo)
- [PR #1179](https://github.com/stympy/faker/pull/1179) Random color with Placeholdit [@nicolas-brousse](https://github.com/nicolas-brousse)
- New collaborator - Vitor Oliveira [@vbrazo](https://github.com/vbrazo)

## [v1.8.7](https://github.com/stympy/faker/tree/v1.8.7) (2017-12-22)
Expand Down
4 changes: 4 additions & 0 deletions doc/placeholdit.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ 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", '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', :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'"
```
9 changes: 9 additions & 0 deletions lib/faker/placeholdit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class << self
SUPPORTED_FORMATS = %w[png jpg gif jpeg].freeze

def image(size = '300x300', format = 'png', background_color = nil, text_color = nil, text = nil)
background_color = generate_color if background_color == :random
text_color = generate_color if text_color == :random

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)/)
Expand All @@ -15,6 +18,12 @@ def image(size = '300x300', format = 'png', background_color = nil, text_color =
image_url += "?text=#{text}" if text
image_url
end

private

def generate_color
format('%06x', (rand * 0xffffff))
end
Copy link
Member

@vbrazo vbrazo May 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why we need this private method since only one part of the system is using it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it because I need this peace of code twice in image method. But I could call it directly in the main method.
It's also to have a lighter method.

Let me know if you want I move it inside image method directly.

Copy link
Member

@vbrazo vbrazo May 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave it how it looks and we can refactor in the future if needed.

end
end
end
8 changes: 8 additions & 0 deletions test/test_placeholdit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def test_avatar_background_with_correct_three_char_hex
assert @tester.image('300x300', 'jpg', 'fff').match(%r{https:\/\/placehold\.it\/(.+)(jpg?)\/fff})
end

def test_avatar_background_with_random_color
assert @tester.image('300x300', 'jpg', :random).match(%r{https:\/\/placehold\.it\/(.+)(jpg?)\/[a-f0-9]{6}})
end

def test_avatar_background_with_wrong_six_char_hex
assert_raise ArgumentError do
@tester.image('300x300', 'jpg', 'fffffz')
Expand All @@ -63,6 +67,10 @@ def test_avatar_font_color_with_correct_three_char_hex
assert @tester.image('300x300', 'jpg', 'fff', '000').match(%r{https:\/\/placehold\.it\/(.+)(jpg?)\/fff})
end

def test_avatar_font_color_with_random_color
assert @tester.image('300x300', 'jpg', 'fff', :random).match(%r{https:\/\/placehold\.it\/(.+)(jpg?)\/fff\/[a-f0-9]{6}})
end

def test_avatar_font_color_with_wrong_six_char_hex
assert_raise ArgumentError do
@tester.image('300x300', 'jpg', 'ffffff', '900F0z')
Expand Down