Skip to content

Commit

Permalink
Merge pull request #68 from Cofense/readme-explain-for-pathname-argument
Browse files Browse the repository at this point in the history
Add explanation of MimeType.for's handling of argument types
  • Loading branch information
gmcgibbon authored Apr 27, 2022
2 parents 7dc54ca + d2b6e39 commit 64fe97d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
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

0 comments on commit 64fe97d

Please sign in to comment.