best_type 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +46 -0
- data/config/internal_config_options.yml +6 -0
- data/lib/best_type/config.rb +42 -0
- data/lib/best_type/dc_type_lookup.rb +35 -0
- data/lib/best_type/mime_type_lookup.rb +30 -0
- data/lib/best_type/version.rb +9 -0
- data/lib/best_type.rb +33 -0
- data/lib/tasks/best_type/ci.rake +38 -0
- data/lib/tasks/best_type.rake +8 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0bc9100b57430bc5ccb10f8d42d7a1741aef2bd4
|
4
|
+
data.tar.gz: c80b5b3f2a2d08839878dc5d17fd8cbd22f4c057
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c9c8e54c2295b513cdc768a495acb11c90747455700438c943aa709f8707094273d80dede61b0c6b3e5a8d828ca493f5d2955d72b0b3f824765e57e5652393cc
|
7
|
+
data.tar.gz: bbdde1c4253881cadc2f6a58b041fabe4d9f3ae2805437e34a58b93bc4f1c058a93837a6ab18d85d0b62f3d503830c79bc143183dc7f76103f345bc4405afadd
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# best_type
|
2
|
+
|
3
|
+
A pure-ruby library for selecting the best MIME type for a file name, or DC type for a file name / MIME type.
|
4
|
+
|
5
|
+
### Installation
|
6
|
+
|
7
|
+
```bash
|
8
|
+
gem install best_type
|
9
|
+
```
|
10
|
+
|
11
|
+
### Standalone Usage
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require 'best_type'
|
15
|
+
|
16
|
+
TODO: Provide usage examples
|
17
|
+
```
|
18
|
+
|
19
|
+
### Rails Usage
|
20
|
+
|
21
|
+
Gemfile:
|
22
|
+
```ruby
|
23
|
+
gem 'best_type'
|
24
|
+
```
|
25
|
+
|
26
|
+
Adding Custom Overrides via initializer (config/initializers/best_type.rb):
|
27
|
+
```ruby
|
28
|
+
# via hash
|
29
|
+
BestType.configure({
|
30
|
+
extension_to_mime_type_overrides:
|
31
|
+
'ext': 'mime/type'
|
32
|
+
mime_type_to_dc_type_overrides:
|
33
|
+
'mime/type': 'GreatType'
|
34
|
+
})
|
35
|
+
|
36
|
+
# via YAML file, based on Rails environment:
|
37
|
+
BestType.configure(YAML.load_file('config/best_type.yml')[Rails.env])
|
38
|
+
```
|
39
|
+
|
40
|
+
### Running Tests (for developers):
|
41
|
+
|
42
|
+
Tests are great and we should run them. Here's how:
|
43
|
+
|
44
|
+
```sh
|
45
|
+
bundle exec rake best_type:ci
|
46
|
+
```
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module BestType
|
2
|
+
class Config
|
3
|
+
|
4
|
+
attr_reader :extension_to_mime_type_overrides, :mime_type_to_dc_type_overrides
|
5
|
+
|
6
|
+
def initialize(user_config_options = {})
|
7
|
+
# Get defaults from internal_custom_mapping.yml in gem
|
8
|
+
gem_dir = Gem::Specification.find_by_name('best_type').gem_dir
|
9
|
+
internal_config_file_path = File.join(gem_dir, 'config/internal_config_options.yml')
|
10
|
+
internal_config_options = YAML.load_file(internal_config_file_path)
|
11
|
+
|
12
|
+
@extension_to_mime_type_overrides = internal_config_options['extension_to_mime_type_overrides']
|
13
|
+
@mime_type_to_dc_type_overrides = internal_config_options['mime_type_to_dc_type_overrides']
|
14
|
+
|
15
|
+
stringify_user_config_options_keys!(user_config_options)
|
16
|
+
add_extension_to_mime_type_overrides(user_config_options['extension_to_mime_type_overrides']) if user_config_options.key?('extension_to_mime_type_overrides')
|
17
|
+
add_mime_type_to_dc_type_overrides(user_config_options['mime_type_to_dc_type_overrides']) if user_config_options.key?('mime_type_to_dc_type_overrides')
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def add_extension_to_mime_type_overrides(overrides)
|
23
|
+
@extension_to_mime_type_overrides.merge!(overrides)
|
24
|
+
end
|
25
|
+
|
26
|
+
def add_mime_type_to_dc_type_overrides(overrides)
|
27
|
+
@mime_type_to_dc_type_overrides.merge!(overrides)
|
28
|
+
end
|
29
|
+
|
30
|
+
def stringify_user_config_options_keys!(user_config_options)
|
31
|
+
user_config_options_keys = user_config_options.keys
|
32
|
+
user_config_options_keys.each do |key|
|
33
|
+
if key.is_a?(Symbol)
|
34
|
+
val = user_config_options.delete(key)
|
35
|
+
user_config_options[key.to_s] = val
|
36
|
+
end
|
37
|
+
end
|
38
|
+
user_config_options_keys
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module BestType
|
2
|
+
class DcTypeLookup
|
3
|
+
|
4
|
+
attr_reader :config
|
5
|
+
|
6
|
+
def initialize(mime_type_lookup_instance)
|
7
|
+
@mime_type_lookup = mime_type_lookup_instance
|
8
|
+
@config = @mime_type_lookup.config
|
9
|
+
end
|
10
|
+
|
11
|
+
def for_file_name(file_name_or_path)
|
12
|
+
for_mime_type(@mime_type_lookup.for_file_name(file_name_or_path))
|
13
|
+
end
|
14
|
+
|
15
|
+
def for_mime_type(mime_type)
|
16
|
+
# Check config overrides first
|
17
|
+
dc_type = @config.mime_type_to_dc_type_overrides.fetch(mime_type, nil)
|
18
|
+
return dc_type unless dc_type.nil?
|
19
|
+
|
20
|
+
mimes_to_dc = {
|
21
|
+
/^image/ => 'StillImage',
|
22
|
+
/^video/ => 'MovingImage',
|
23
|
+
/^audio/ => 'Sound',
|
24
|
+
/^text/ => 'Text',
|
25
|
+
/^application\/(pdf|msword)/ => 'Text',
|
26
|
+
/excel|spreadsheet|xls|application\/sql/ => 'Dataset',
|
27
|
+
/^application/ => 'Software'
|
28
|
+
}
|
29
|
+
|
30
|
+
dc_type = mimes_to_dc.find { |pattern, _type_val| mime_type =~ pattern }
|
31
|
+
dc_type.last
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'mime-types'
|
2
|
+
|
3
|
+
module BestType
|
4
|
+
class MimeTypeLookup
|
5
|
+
|
6
|
+
attr_reader :config
|
7
|
+
|
8
|
+
FALLBACK_MIME_TYPE_VALUE = 'application/octet-stream'.freeze
|
9
|
+
|
10
|
+
def initialize(config)
|
11
|
+
@config = config
|
12
|
+
end
|
13
|
+
|
14
|
+
def for_file_name(file_name_or_path)
|
15
|
+
extension = File.extname(file_name_or_path)
|
16
|
+
extension = extension[1..-1] unless extension.empty?
|
17
|
+
|
18
|
+
# Check config overrides first
|
19
|
+
unless extension.empty?
|
20
|
+
mime_type = @config.extension_to_mime_type_overrides.fetch(extension, nil)
|
21
|
+
return mime_type unless mime_type.nil?
|
22
|
+
end
|
23
|
+
|
24
|
+
# Fall back to regular lookup
|
25
|
+
detected_mime_types = MIME::Types.of(file_name_or_path)
|
26
|
+
detected_mime_types.empty? ? FALLBACK_MIME_TYPE_VALUE : detected_mime_types.first.content_type
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
data/lib/best_type.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'best_type/version'
|
2
|
+
require 'best_type/config'
|
3
|
+
require 'best_type/mime_type_lookup'
|
4
|
+
require 'best_type/dc_type_lookup'
|
5
|
+
require 'yaml'
|
6
|
+
|
7
|
+
module BestType
|
8
|
+
@semaphore = Mutex.new
|
9
|
+
|
10
|
+
def self.mime_type
|
11
|
+
@mime_type ||= BestType::MimeTypeLookup.new(config)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.dc_type
|
15
|
+
@dc_type ||= BestType::DcTypeLookup.new(mime_type)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.config(reload = false, user_config_options = {})
|
19
|
+
if @config.nil? || reload
|
20
|
+
@semaphore.synchronize do
|
21
|
+
@config = BestType::Config.new(user_config_options)
|
22
|
+
@mime_type = nil
|
23
|
+
@dc_type = nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
@config
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.configure(opts = {})
|
30
|
+
config(true, opts)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'best_type'
|
2
|
+
|
3
|
+
namespace :best_type do
|
4
|
+
|
5
|
+
begin
|
6
|
+
# This code is in a begin/rescue block so that the Rakefile is usable
|
7
|
+
# in an environment where RSpec is unavailable (i.e. production).
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
|
10
|
+
RSpec::Core::RakeTask.new(:rspec) do |spec|
|
11
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
12
|
+
spec.pattern += FileList['spec/*_spec.rb']
|
13
|
+
spec.rspec_opts = ['--backtrace'] if ENV['CI']
|
14
|
+
end
|
15
|
+
|
16
|
+
require 'rubocop/rake_task'
|
17
|
+
|
18
|
+
desc 'Run style checker'
|
19
|
+
RuboCop::RakeTask.new(:rubocop) do |task|
|
20
|
+
task.requires << 'rubocop-rspec'
|
21
|
+
task.fail_on_error = true
|
22
|
+
end
|
23
|
+
rescue LoadError => e
|
24
|
+
puts "[Warning] Exception creating rspec rake tasks. This message can be ignored in environments that intentionally do not pull in the RSpec gem (i.e. production)."
|
25
|
+
puts e
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "CI build"
|
29
|
+
task ci: ['best_type:rubocop'] do
|
30
|
+
Rake::Task["best_type:rspec"].invoke
|
31
|
+
end
|
32
|
+
|
33
|
+
desc "CI build"
|
34
|
+
task :ci_nocop do
|
35
|
+
Rake::Task["best_type:rspec"].invoke
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: best_type
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eric O'Hanlon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mime-types
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.51.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.51.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.20.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.20.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.15.1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.15.1
|
97
|
+
description: A library for selecting the best mime type or dc type for a file.
|
98
|
+
email: elo2112@columbia.edu
|
99
|
+
executables: []
|
100
|
+
extensions: []
|
101
|
+
extra_rdoc_files: []
|
102
|
+
files:
|
103
|
+
- README.md
|
104
|
+
- config/internal_config_options.yml
|
105
|
+
- lib/best_type.rb
|
106
|
+
- lib/best_type/config.rb
|
107
|
+
- lib/best_type/dc_type_lookup.rb
|
108
|
+
- lib/best_type/mime_type_lookup.rb
|
109
|
+
- lib/best_type/version.rb
|
110
|
+
- lib/tasks/best_type.rake
|
111
|
+
- lib/tasks/best_type/ci.rake
|
112
|
+
homepage: https://github.com/cul/best_type
|
113
|
+
licenses:
|
114
|
+
- MIT
|
115
|
+
metadata: {}
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 2.6.14
|
133
|
+
signing_key:
|
134
|
+
specification_version: 4
|
135
|
+
summary: A library for selecting the best mime type or dc type for a file.
|
136
|
+
test_files: []
|