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 Faker::Internet.uuid #1603

Merged
merged 2 commits into from May 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/unreleased/default/internet.md
Expand Up @@ -79,4 +79,6 @@ Faker::Internet.slug('foo bar', '-') #=> "foo-bar"
# Optional argument: vendor=nil
Faker::Internet.user_agent #=> "Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7; AOLBuild 4343.19; Windows NT 6.1; WOW64; Trident/5.0; FunWebProducts)"
Faker::Internet.user_agent(:firefox) #=> "Mozilla/5.0 (Windows NT x.y; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0"

Faker::Internet.uuid #=> "929ef6ef-b11f-38c9-111b-accd67a258b2"
```
4 changes: 4 additions & 0 deletions lib/faker/default/internet.rb
Expand Up @@ -192,6 +192,10 @@ def user_agent(vendor = nil)
sample(agents)
end

def uuid
Faker::Config.random.bytes(16).unpack('H8H4H4H4H12').join('-')
Copy link
Contributor

Choose a reason for hiding this comment

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

probably best to generate a valid version4 UUID incase there's a library out there that does something special based on the version. For Faker's purposes, that just means that we need to make sure the first digit in the 3rd block is always a 4:
xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great catch. Done.

end

alias user_name username
end
end
Expand Down
4 changes: 4 additions & 0 deletions test/faker/default/test_faker_internet.rb
Expand Up @@ -280,4 +280,8 @@ def test_user_agent_with_invalid_argument
assert @tester.user_agent(nil).match(/Mozilla|Opera/)
assert @tester.user_agent(1).match(/Mozilla|Opera/)
end

def test_uuid
assert @tester.uuid.match(/\h{8}-\h{4}-\h{4}-\h{4}-\h{12}/)
end
end