best_type 0.0.2 → 0.0.3
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 +4 -4
- data/README.md +70 -12
- data/lib/best_type/dc_type_lookup.rb +3 -1
- data/lib/best_type/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b2371da6f225b1bd22acd81086ea29b9e219f02
|
4
|
+
data.tar.gz: 27b8760b8632f7716eece5a0889327ae57c3da9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d8a536b778ade2b05994d6e958975a5672ffd6953946e8f677faa801d014c1ab4c13399a3b4678d2fd9c8c3477716b14867f2dd5620fc3e5d14e5cf8650da6e
|
7
|
+
data.tar.gz: a6f9920f32d467b5f9f18e5598da3363cbc85e62981639ec99e64ea98b7068146f7729207c588d35a2a419250f9834dce1cd11b1b16ca2711b35580df06f14bd
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# best_type
|
2
2
|
|
3
|
-
A pure-ruby library for selecting the best MIME type for a file name
|
3
|
+
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.
|
4
4
|
|
5
5
|
### Installation
|
6
6
|
|
@@ -8,33 +8,91 @@ A pure-ruby library for selecting the best MIME type for a file name, or DC type
|
|
8
8
|
gem install best_type
|
9
9
|
```
|
10
10
|
|
11
|
-
###
|
11
|
+
### Usage
|
12
12
|
|
13
13
|
```ruby
|
14
|
+
# require the gem
|
14
15
|
require 'best_type'
|
15
16
|
|
16
|
-
|
17
|
+
# detect mime type for file names
|
18
|
+
BestType.mime_type.for_file_name('myfile.jpg') # 'image/jpeg'
|
19
|
+
|
20
|
+
# detect mime type for file names (including full file path)
|
21
|
+
BestType.mime_type.for_file_name('/path/to/some/file.jpg') # 'image/jpeg'
|
22
|
+
|
23
|
+
# detect dc type for file names
|
24
|
+
BestType.dc_type.for_file_name('myfile.jpg') # 'StillImage'
|
25
|
+
|
26
|
+
# detect dc type for file names (including full file path)
|
27
|
+
BestType.dc_type.for_file_name('/path/to/some/file.jpg') # 'StillImage'
|
28
|
+
|
29
|
+
# detect dc type for mime types
|
30
|
+
BestType.dc_type.for_mime_type('image/jpeg') # 'StillImage'
|
17
31
|
```
|
18
32
|
|
19
|
-
###
|
33
|
+
### Add Custom Overrides
|
34
|
+
```ruby
|
35
|
+
BestType.configure({
|
36
|
+
extension_to_mime_type_overrides:
|
37
|
+
'custom': 'custom/type'
|
38
|
+
mime_type_to_dc_type_overrides:
|
39
|
+
'custom/type': 'Custom'
|
40
|
+
})
|
41
|
+
|
42
|
+
BestType.mime_type.for_file_name('myfile.custom') # 'custom/type'
|
43
|
+
BestType.dc_type.for_file_name('myfile.custom') # 'Custom'
|
44
|
+
BestType.dc_type.for_mime_type('custom/type') # 'Custom'
|
45
|
+
|
46
|
+
```
|
20
47
|
|
21
|
-
|
48
|
+
### Recommended Setup For Rails
|
49
|
+
|
50
|
+
Add best_type to your Gemfile:
|
22
51
|
```ruby
|
23
52
|
gem 'best_type'
|
24
53
|
```
|
25
54
|
|
26
|
-
|
55
|
+
And then call it from anywhere!
|
56
|
+
|
57
|
+
If you want to set custom overrides, the best place to do so is in a Rails initializer:
|
27
58
|
```ruby
|
28
|
-
#
|
59
|
+
# config/initializers/best_type.rb
|
60
|
+
|
29
61
|
BestType.configure({
|
30
62
|
extension_to_mime_type_overrides:
|
31
|
-
'
|
63
|
+
'custom': 'custom/type'
|
32
64
|
mime_type_to_dc_type_overrides:
|
33
|
-
'
|
65
|
+
'custom/type': 'Custom'
|
34
66
|
})
|
67
|
+
```
|
68
|
+
|
69
|
+
You may also want to consider using a YAML file for configuration:
|
70
|
+
```ruby
|
71
|
+
# config/initializers/best_type.rb
|
72
|
+
|
73
|
+
BestType.configure(YAML.load_file(File.join(Rails.root, 'config/best_type.yml'))[Rails.env])
|
74
|
+
```
|
75
|
+
|
76
|
+
```yaml
|
77
|
+
# config/initializers/best_type.rb
|
78
|
+
|
79
|
+
development:
|
80
|
+
extension_to_mime_type_overrides:
|
81
|
+
'good': 'good/type'
|
82
|
+
mime_type_to_dc_type_overrides:
|
83
|
+
'good/type': 'Good'
|
84
|
+
|
85
|
+
test:
|
86
|
+
extension_to_mime_type_overrides:
|
87
|
+
'better': 'better/type'
|
88
|
+
mime_type_to_dc_type_overrides:
|
89
|
+
'better/type': 'Better'
|
35
90
|
|
36
|
-
|
37
|
-
|
91
|
+
production:
|
92
|
+
extension_to_mime_type_overrides:
|
93
|
+
'best': 'best/type'
|
94
|
+
mime_type_to_dc_type_overrides:
|
95
|
+
'best/type': 'Best'
|
38
96
|
```
|
39
97
|
|
40
98
|
### Running Tests (for developers):
|
@@ -49,4 +107,4 @@ bundle exec rake best_type:ci
|
|
49
107
|
|
50
108
|
```sh
|
51
109
|
bundle exec rake release
|
52
|
-
```
|
110
|
+
```
|
@@ -3,6 +3,8 @@ module BestType
|
|
3
3
|
|
4
4
|
attr_reader :config
|
5
5
|
|
6
|
+
FALLBACK_DC_TYPE = 'Software'.freeze
|
7
|
+
|
6
8
|
def initialize(mime_type_lookup_instance)
|
7
9
|
@mime_type_lookup = mime_type_lookup_instance
|
8
10
|
@config = @mime_type_lookup.config
|
@@ -28,7 +30,7 @@ module BestType
|
|
28
30
|
}
|
29
31
|
|
30
32
|
dc_type = mimes_to_dc.find { |pattern, _type_val| mime_type =~ pattern }
|
31
|
-
dc_type.last
|
33
|
+
dc_type.nil? ? FALLBACK_DC_TYPE : dc_type.last
|
32
34
|
end
|
33
35
|
|
34
36
|
end
|
data/lib/best_type/version.rb
CHANGED