Skip to content

Commit

Permalink
Add the ability to generate a mime type of a specific media type (#2518)
Browse files Browse the repository at this point in the history
* Add the ability to get a mime type of a specific media type

* Update mime_type method version due to adding new features
  • Loading branch information
ric2b authored Aug 8, 2022
1 parent 97ba9dd commit 15a8bc3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Include [YARD] style docs for all methods that includes:
- The return type (`@return`)
- At least one example of the output (`@example`)
- The version that the method was added (`@faker.version`)
- Set as `next` for new methods
- Set as `next` for new methods or methods with new features

```ruby
##
Expand Down
2 changes: 2 additions & 0 deletions doc/default/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Available since version 1.6.4.
```ruby
Faker::File.extension #=> "mp3"

# Keyword arguments: media_type
Faker::File.mime_type #=> "application/pdf"
Faker::File.mime_type(media_type: 'image') #=> "image/png"

# Keyword arguments: dir, name, extension, directory_separator
Faker::File.file_name(dir: 'path/to') #=> "path/to/something_random.jpg"
Expand Down
6 changes: 3 additions & 3 deletions lib/faker/default/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ def extension
# @example
# Faker::File.mime_type #=> "application/pdf"
#
# @faker.version 1.6.4
def mime_type
fetch('file.mime_type')
# @faker.version next
def mime_type(media_type: nil)
media_type ? fetch("file.mime_type.#{media_type}") : sample(sample(translate('faker.file.mime_type').values))
end

# rubocop:disable Metrics/ParameterLists
Expand Down
10 changes: 9 additions & 1 deletion lib/locales/en/file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ en:
faker:
file:
extension: ["flac", "mp3", "wav", "bmp", "gif", "jpeg", "jpg", "png", "tiff", "css", "csv", "html", "js", "json", "txt", "mp4", "avi", "mov", "webm", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "odt", "ods", "odp", "pages", "numbers", "key", "pdf"]
mime_type: ["application/atom+xml", "application/ecmascript", "application/EDI-X12", "application/EDIFACT", "application/json", "application/javascript", "application/ogg", "application/pdf", "application/postscript", "application/rdf+xml", "application/rss+xml", "application/soap+xml", "application/font-woff", "application/xhtml+xml", "application/xml-dtd", "application/xop+xml", "application/zip", "application/gzip", "audio/basic", "audio/L24", "audio/mp4", "audio/mpeg", "audio/ogg", "audio/vorbis", "audio/vnd.rn-realaudio", "audio/vnd.wave", "audio/webm", "image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/svg+xml", "image/tiff", "image/vnd.microsoft.icon", "message/http", "message/imdn+xml", "message/partial", "message/rfc822", "model/example", "model/iges", "model/mesh", "model/vrml", "model/x3d+binary", "model/x3d+vrml", "model/x3d+xml", "multipart/mixed", "multipart/alternative", "multipart/related", "multipart/form-data", "multipart/signed", "multipart/encrypted", "text/cmd", "text/css", "text/csv", "text/html", "text/javascript", "text/plain", "text/vcard", "text/xml", "video/mpeg", "video/mp4", "video/ogg", "video/quicktime", "video/webm", "video/x-matroska", "video/x-ms-wmv", "video/x-flv"]
mime_type:
application: ["application/atom+xml", "application/ecmascript", "application/EDI-X12", "application/EDIFACT", "application/json", "application/javascript", "application/ogg", "application/pdf", "application/postscript", "application/rdf+xml", "application/rss+xml", "application/soap+xml", "application/font-woff", "application/xhtml+xml", "application/xml-dtd", "application/xop+xml", "application/zip", "application/gzip"]
audio: ["audio/basic", "audio/L24", "audio/mp4", "audio/mpeg", "audio/ogg", "audio/vorbis", "audio/vnd.rn-realaudio", "audio/vnd.wave", "audio/webm"]
image: ["image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/svg+xml", "image/tiff", "image/vnd.microsoft.icon"]
message: ["message/http", "message/imdn+xml", "message/partial", "message/rfc822"]
model: ["model/example", "model/iges", "model/mesh", "model/vrml", "model/x3d+binary", "model/x3d+vrml", "model/x3d+xml"]
multipart: ["multipart/mixed", "multipart/alternative", "multipart/related", "multipart/form-data", "multipart/signed", "multipart/encrypted"]
text: ["text/cmd", "text/css", "text/csv", "text/html", "text/javascript", "text/plain", "text/vcard", "text/xml"]
video: ["video/mpeg", "video/mp4", "video/ogg", "video/quicktime", "video/webm", "video/x-matroska", "video/x-ms-wmv", "video/x-flv"]
5 changes: 5 additions & 0 deletions test/faker/default/test_faker_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def test_mime_type_format
assert @tester.mime_type.match(%r{(.*)/(.*)+})
end

def test_mime_type_format_with_media_type
media_type = Faker::Base.translate('faker.file.mime_type').keys.sample
assert @tester.mime_type(media_type: media_type).match(%r{#{media_type}/(.*)+})
end

def test_file_name
assert @tester
.file_name
Expand Down

0 comments on commit 15a8bc3

Please sign in to comment.