Coder Social home page Coder Social logo

best_type's Introduction

best_type

A pure-ruby library for selecting the best MIME type for a file name or DC type (http://dublincore.org/2012/06/14/dctype) for a file name / MIME type.

Installation

gem install best_type

Usage

# require the gem
require 'best_type'

# detect mime type for file names
BestType.mime_type.for_file_name('myfile.jpg') # 'image/jpeg'

# detect mime type for file names (including full file path)
BestType.mime_type.for_file_name('/path/to/some/file.jpg') # 'image/jpeg'

# detect dc type for file names
BestType.dc_type.for_file_name('myfile.jpg') # 'StillImage'

# detect dc type for file names (including full file path)
BestType.dc_type.for_file_name('/path/to/some/file.jpg') # 'StillImage'

# detect dc type for mime types
BestType.dc_type.for_mime_type('image/jpeg') # 'StillImage'

# detect dc type for file names (including full file path)
BestType.pcdm_type.for_file_name('/path/to/some/file.jpg') # 'Image'

# detect dc type for mime types
BestType.pcdm_type.for_mime_type('image/jpeg') # 'Image'

Add Custom Overrides

BestType.configure({
  extension_to_mime_type_overrides:
    'custom': 'custom/type'
  mime_type_to_dc_type_overrides:
    'custom/type': 'CustomDC'
  mime_type_to_pcdm_type_overrides:
    'custom/type': 'CustomPCDM'
})

BestType.mime_type.for_file_name('myfile.custom') # 'custom/type'
BestType.dc_type.for_file_name('myfile.custom') # 'CustomDC'
BestType.pcdm_type.for_mime_type('custom/type') # 'CustomPCDM'

Recommended Setup For Rails

Add best_type to your Gemfile:

gem 'best_type'

And then call it from anywhere!

If you want to set custom overrides, the best place to do so is in a Rails initializer:

# config/initializers/best_type.rb

BestType.configure({
  extension_to_mime_type_overrides:
    'custom': 'custom/type'
  mime_type_to_dc_type_overrides:
    'custom/type': 'Custom'
  mime_type_to_pcdm_type_overrides:
    'custom/type': 'Custom'
})

You may also want to consider using a YAML file for configuration:

# config/initializers/best_type.rb

BestType.configure(YAML.load_file(File.join(Rails.root, 'config/best_type.yml'))[Rails.env])
# config/initializers/best_type.rb

development:
  extension_to_mime_type_overrides:
    'good': 'good/type'
  mime_type_to_dc_type_overrides:
    'good/type': 'Good'
  mime_type_to_pcdm_type_overrides:
    'good/type': 'Goodly'
    
test:
  extension_to_mime_type_overrides:
    'better': 'better/type'
  mime_type_to_dc_type_overrides:
    'better/type': 'Better'
  mime_type_to_pcdm_type_overrides:
    'best/type': 'Betterly'

production:
  extension_to_mime_type_overrides:
    'best': 'best/type'
  mime_type_to_dc_type_overrides:
    'best/type': 'Best'
  mime_type_to_pcdm_type_overrides:
    'best/type': 'Bestly'

Running Tests (for developers):

Tests are great and we should run them. Here's how:

bundle exec rake best_type:ci

Building the Gem and Pushing to RubyGems (for developers):

bundle exec rake release

best_type's People

Contributors

elohanlon avatar barmintor avatar

Watchers

 avatar James Cloos avatar  avatar ttp2113 avatar YIchao Zhang avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.