Skip to content

Add explanation of MimeType.for's handling of argument types #68

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

Merged
merged 1 commit into from
Apr 27, 2022
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
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Marcel

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):

It's used like this:
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:

```ruby
Marcel::MimeType.for Pathname.new("example.gif")
Expand Down
15 changes: 15 additions & 0 deletions lib/marcel/mime_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ def extend(type, extensions: [], parents: [], magic: nil)
Magic.add(type, extensions: extensions, magic: magic, parents: parents, comment: comment)
end

# Returns the most appropriate content type for the given file.
#
# The first argument should be a +Pathname+ or an +IO+. If it is a +Pathname+, the specified
# file will be opened first.
#
# Optional parameters:
# * +name+: file name, if known
# * +extension+: file extension, if known
# * +declared_type+: MIME type, if known
#
# The most appropriate type is determined by the following:
# * type declared by binary magic number data
# * type declared by the first of file name, file extension, or declared MIME type
#
# If no type can be determined, then +application/octet-stream+ is returned.
def for(pathname_or_io = nil, name: nil, extension: nil, declared_type: nil)
type_from_data = for_data(pathname_or_io)
fallback_type = for_declared_type(declared_type) || for_name(name) || for_extension(extension) || BINARY
Expand Down