best_type 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d0a354dd631cb8c8c051dcb86a5ce63b8921dec
4
- data.tar.gz: 57fe2ddc45928bbd3d18f9d3350f414d4de30147
3
+ metadata.gz: 9b2371da6f225b1bd22acd81086ea29b9e219f02
4
+ data.tar.gz: 27b8760b8632f7716eece5a0889327ae57c3da9a
5
5
  SHA512:
6
- metadata.gz: 72876496600f945fbbf2f6faf8145592dfb4eefe856afc8587feb51f29b3aa920e9477489e25d6b78600edaaffb4fe9ff01e04f97b782013e187a88829239184
7
- data.tar.gz: 93534fa26253ee1d95940af36f884e7f1856fd690812c41dfba399d2a62d54e3dc4812d4f35ad607527a72dbb7277534680de5833802626dbf012fe15c5f0782
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, or DC type for a file name / MIME type.
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
- ### Standalone Usage
11
+ ### Usage
12
12
 
13
13
  ```ruby
14
+ # require the gem
14
15
  require 'best_type'
15
16
 
16
- TODO: Provide usage examples
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
- ### Rails Usage
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
- Gemfile:
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
- Adding Custom Overrides via initializer (config/initializers/best_type.rb):
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
- # via hash
59
+ # config/initializers/best_type.rb
60
+
29
61
  BestType.configure({
30
62
  extension_to_mime_type_overrides:
31
- 'ext': 'mime/type'
63
+ 'custom': 'custom/type'
32
64
  mime_type_to_dc_type_overrides:
33
- 'mime/type': 'GreatType'
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
- # via YAML file, based on Rails environment:
37
- BestType.configure(YAML.load_file('config/best_type.yml')[Rails.env])
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
@@ -1,6 +1,6 @@
1
1
  module BestType
2
2
 
3
- VERSION = '0.0.2'.freeze
3
+ VERSION = '0.0.3'.freeze
4
4
 
5
5
  def self.version
6
6
  VERSION
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: best_type
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric O'Hanlon