Skip to content

Commit d2b6e39

Browse files
committed
Document Marcel::MimeType.for
on-behalf-of: @Cofense <[email protected]>
1 parent 7f71ab9 commit d2b6e39

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Marcel
22

3-
Marcel attempts to choose the most appropriate content type for a given file by looking at the binary data, the filename, and any declared type (perhaps passed as a request header):
4-
5-
It's used like this:
3+
Marcel attempts to choose the most appropriate content type for a given file by looking at the binary data, the filename, and any declared type (perhaps passed as a request header). This is done via the `Marcel::MimeType.for` method, and is used like this:
64

75
```ruby
86
Marcel::MimeType.for Pathname.new("example.gif")

lib/marcel/mime_type.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ def extend(type, extensions: [], parents: [], magic: nil)
1313
Magic.add(type, extensions: extensions, magic: magic, parents: parents, comment: comment)
1414
end
1515

16+
# Returns the most appropriate content type for the given file.
17+
#
18+
# The first argument should be a +Pathname+ or an +IO+. If it is a +Pathname+, the specified
19+
# file will be opened first.
20+
#
21+
# Optional parameters:
22+
# * +name+: file name, if known
23+
# * +extension+: file extension, if known
24+
# * +declared_type+: MIME type, if known
25+
#
26+
# The most appropriate type is determined by the following:
27+
# * type declared by binary magic number data
28+
# * type declared by the first of file name, file extension, or declared MIME type
29+
#
30+
# If no type can be determined, then +application/octet-stream+ is returned.
1631
def for(pathname_or_io = nil, name: nil, extension: nil, declared_type: nil)
1732
type_from_data = for_data(pathname_or_io)
1833
fallback_type = for_declared_type(declared_type) || for_name(name) || for_extension(extension) || BINARY

0 commit comments

Comments
 (0)