mime-types-mini 0.1.0

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.
@@ -0,0 +1,12 @@
1
+ %language=ANSI-C
2
+ %define slot-name extension
3
+ %define hash-function-name content_type_extension_hash
4
+ %define lookup-function-name lookup_content_type_by_extension
5
+ %enum
6
+ %switch=4
7
+ struct ContentTypeExtension {
8
+ const char * extension;
9
+ const char * content_type;
10
+ };
11
+ %%<?r extensions.each do |extension, content_type| ?>
12
+ #{extension}, #{content_type.inspect}<?r end ?>
@@ -0,0 +1,13 @@
1
+
2
+ extern const struct MimeType MIME_TYPES[] = {
3
+ <?r mime_types.each do |mime_type|
4
+ content_type, encoding, extensions, registered = mime_type.values_at('content-type', 'encoding', 'extensions', 'registered')
5
+ ?>
6
+ {
7
+ .content_type = #{content_type.inspect},
8
+ .encoding = #{encoding.inspect},
9
+ .extensions = #{extensions_struct(extensions)},
10
+ .registered = #{registered == true ? 1 : 0},
11
+ },
12
+ <?r end ?>
13
+ };
@@ -0,0 +1,31 @@
1
+ // Include the Ruby headers and goodies
2
+ #include "ruby.h"
3
+
4
+ #include "Extensions.h"
5
+
6
+ VALUE MimeTypesMiniDatabase = Qnil;
7
+
8
+ void Init_MimeTypesMiniDatabase();
9
+
10
+ static VALUE method_content_type_for_extension(VALUE self, VALUE extension) {
11
+ int len = RSTRING_LEN(extension);
12
+ char* str = RSTRING_PTR(extension);
13
+
14
+ const struct ContentTypeExtension * result = lookup_content_type_by_extension(str, len);
15
+
16
+ if (result) {
17
+ return rb_str_new_cstr(result->content_type);
18
+ } else {
19
+ return Qnil;
20
+ }
21
+ }
22
+
23
+ void Init_MimeTypesMiniDatabase() {
24
+ VALUE Mime = rb_define_module("Mime");
25
+ VALUE MimeTypes = rb_define_module_under(Mime, "Types");
26
+ VALUE MimeTypesMini = rb_define_module_under(MimeTypes, "Mini");
27
+
28
+ MimeTypesMiniDatabase = rb_define_module_under(MimeTypesMini, "Database");
29
+
30
+ rb_define_module_function(MimeTypesMiniDatabase, "content_type_for_extension", method_content_type_for_extension, 1);
31
+ }
@@ -0,0 +1,61 @@
1
+
2
+ require 'mime/types/data'
3
+ require 'trenni'
4
+ require 'json'
5
+ require 'ostruct'
6
+
7
+ class TemplateState < Struct.new(:mime_types)
8
+ NULL = 'NULL'
9
+
10
+ def extensions_struct(extensions)
11
+ if extensions
12
+ '(const char*[]){' + (extensions.collect(&:inspect) + [0]).join(', ') + '}'
13
+ else
14
+ 0
15
+ end
16
+ end
17
+ end
18
+
19
+ task :load_mime_types do
20
+ mime_types_json_path = File.join(MIME::Types::Data::PATH, "mime-types.json")
21
+ @mime_types = JSON::load(File.open mime_types_json_path)
22
+ end
23
+
24
+ task :generate_extensions_hashtable => :load_mime_types do
25
+ all_extensions = {}
26
+
27
+ @mime_types.each do |mime_type|
28
+ if extensions = mime_type['extensions']
29
+ extensions.each do |extension|
30
+ all_extensions[extension] = mime_type['content-type']
31
+ end
32
+ end
33
+ end
34
+
35
+ template_path = File.expand_path("Extensions.trenni", __dir__)
36
+ template = Trenni::Template.load_file(template_path)
37
+
38
+ state = OpenStruct.new(extensions: all_extensions)
39
+ result = template.to_string(state)
40
+
41
+ File.write("Extensions.gperf", result)
42
+ end
43
+
44
+ task :generate_extensions => :generate_extensions_hashtable do
45
+ sh 'gperf', '-CD', '-t', 'Extensions.gperf', '--output-file', 'Extensions.h'
46
+ end
47
+
48
+ task :generate_code do
49
+ mime_types_json_path = File.join(MIME::Types::Data::PATH, "mime-types.json")
50
+ mime_types = JSON::load(File.open mime_types_json_path)
51
+
52
+ template_path = File.expand_path("MimeTypes.trenni", __dir__)
53
+ template = Trenni::Template.load_file(template_path)
54
+
55
+ template_state = TemplateState.new(mime_types)
56
+ result = template.to_string(template_state)
57
+
58
+ File.write("MimeTypes.c", result)
59
+ end
60
+
61
+ task :default => :generate_code
@@ -0,0 +1,11 @@
1
+ # Loads mkmf which is used to make makefiles for Ruby extensions
2
+ require 'mkmf'
3
+
4
+ # Give it a name
5
+ extension_name = 'MimeTypesMiniDatabase'
6
+
7
+ # The destination
8
+ dir_config(extension_name)
9
+
10
+ # Do the work
11
+ create_makefile(extension_name)
@@ -0,0 +1,31 @@
1
+ # Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require "mime/types/mini/version"
22
+
23
+ # Load the compiled module:
24
+ require 'MimeTypesMiniDatabase'
25
+
26
+ module Mime
27
+ module Types
28
+ module Mini
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ # Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Mime
22
+ module Types
23
+ module Mini
24
+ VERSION = "0.1.0"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mime/types/mini/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mime-types-mini"
8
+ spec.version = Mime::Types::Mini::VERSION
9
+ spec.authors = ["Samuel Williams"]
10
+ spec.email = ["samuel.williams@oriontransfer.co.nz"]
11
+
12
+ spec.summary = %q{A native implementation of mime-types which improves both memory usage and performance.}
13
+ #spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ #spec.homepage = "TODO: Put your gem's website or public repo URL here."
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.extensions = %w[ext/MimeTypeMiniDatabase/extconf.rb]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.12"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mime-types-mini
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Williams
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - samuel.williams@oriontransfer.co.nz
58
+ executables: []
59
+ extensions:
60
+ - ext/MimeTypeMiniDatabase/extconf.rb
61
+ extra_rdoc_files: []
62
+ files:
63
+ - Gemfile
64
+ - Rakefile
65
+ - ext/MimeTypeMiniDatabase/Extensions.gperf
66
+ - ext/MimeTypeMiniDatabase/Extensions.h
67
+ - ext/MimeTypeMiniDatabase/Extensions.trenni
68
+ - ext/MimeTypeMiniDatabase/MimeTypes.trenni
69
+ - ext/MimeTypeMiniDatabase/MimeTypesMiniDatabase.c
70
+ - ext/MimeTypeMiniDatabase/Rakefile
71
+ - ext/MimeTypeMiniDatabase/extconf.rb
72
+ - lib/mime/types/mini.rb
73
+ - lib/mime/types/mini/version.rb
74
+ - mime-types-mini.gemspec
75
+ homepage:
76
+ licenses: []
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.5.1
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: A native implementation of mime-types which improves both memory usage and
98
+ performance.
99
+ test_files: []