Skip to content

Commit

Permalink
Add Faker::Internet.uuid (#1603)
Browse files Browse the repository at this point in the history
* Add Faker::Internet.uuid

* Make uuid v4 compatible
  • Loading branch information
ianks authored and vbrazo committed May 10, 2019
1 parent 76f8c76 commit 289c719
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
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"
```
8 changes: 8 additions & 0 deletions lib/faker/default/internet.rb
Expand Up @@ -192,6 +192,14 @@ def user_agent(vendor = nil)
sample(agents)
end

def uuid
# borrowed from: https://github.com/ruby/ruby/blob/d48783bb0236db505fe1205d1d9822309de53a36/lib/securerandom.rb#L250
ary = Faker::Config.random.bytes(16).unpack('NnnnnN')
ary[2] = (ary[2] & 0x0fff) | 0x4000
ary[3] = (ary[3] & 0x3fff) | 0x8000
'%08x-%04x-%04x-%04x-%04x%08x' % ary # rubocop:disable Style/FormatString
end

alias user_name username
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/faker/default/test_faker_internet.rb
Expand Up @@ -280,4 +280,10 @@ 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
uuid = @tester.uuid
assert_equal(36, uuid.size)
assert_match(/\A\h{8}-\h{4}-4\h{3}-\h{4}-\h{12}\z/, uuid)
end
end

0 comments on commit 289c719

Please sign in to comment.