multigiri 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -7,3 +7,11 @@
7
7
 
8
8
  = Version 0.0.3
9
9
  * Added middleware for HTML minification
10
+
11
+ = Version 0.0.4
12
+ * Fixed NoMethodError in multigiri.rb
13
+
14
+ = Version 0.0.5
15
+ * Nokogiri was divided into Nokogiri::HTML & Nokogiri::XML
16
+ * Added content_types option which can take an array of strings or regexps for matching if multigiri will run for current content type
17
+ * Improved performance
@@ -11,23 +11,29 @@ require "multigiri/google_analytics"
11
11
  # nokogiri, document so instead of [status, headers, body] you'll get
12
12
  # [status, headers, document]. After the multigiri block everything
13
13
  # will get to normal, so middlewares whichrun later won't be affected.
14
- use Multigiri do
15
- use HTML5::Forms
16
- use HTML5::Hidden
17
- use EmailObfuscator
18
- use GoogleAnalytics, :my_tracking_code
14
+ use Multigiri::HTML do
15
+ use Multigiri::HTML5::Forms
16
+ use Multigiri::HTML5::Hidden
17
+ use Multigiri::EmailObfuscator
18
+ use Multigiri::GoogleAnalytics, :my_tracking_code
19
19
  end
20
20
 
21
- # It's especially important to run these middlewares after multigiri,
22
- # because in multigiri middleware you probably change the resulted HTML
23
- use ContentType
24
- use ContentLength
21
+ # RSS / Atom feeds processing
22
+ use Multigiri::XML, content_types: ["application/rss+xml", "application/atom+xml"] do
23
+ # TODO
24
+ end
25
25
 
26
- # We can't use the trick with __END__ as is used in sinatra, since Rack
27
- # authors are stupid idiots and the content of the rackup file is eval-ed.
28
- # This crappy design leads to more problems than just this one.
29
- # template = ::File.read(__FILE__).split("__END__").last
26
+ # Generic XML processing
27
+ use Multigiri::XML, content_types: [/\/xml$/] do
28
+ # TODO
29
+ end
30
30
 
31
+ use Rack::ContentType, "text/html"
32
+
33
+ # We can't use the trick with __END__ as is used in sinatra,
34
+ # since the content of the rackup file is eval-ed. This
35
+ # crappy design leads to more problems than just this one.
36
+ # template = ::File.read(__FILE__).split("__END__").last
31
37
  template = ::File.read(__FILE__).match(/=begin template\n(.+)\n=end/m)[1]
32
38
 
33
39
  run lambda { |env| [200, Hash.new, [template]] }
@@ -10,34 +10,81 @@
10
10
  require "nokogiri"
11
11
 
12
12
  # TODO: args for nokogiri, export format, indentation etc
13
- class Multigiri
14
- attr_reader :options, :block
15
- def initialize(app, options = Hash.new, &block)
16
- @app, @options, @block = app, options, block
17
- @options[:indentation] ||= 2
18
- end
13
+ module Multigiri
14
+ class HTML
15
+ # Ruby GC is weak
16
+ CONTENT_TYPE = "Content-Type"
17
+ CONTENT_LENGTH = "Content-Length"
18
+ DEFAULT_TYPES = ["text/html"]
19
+
20
+ attr_reader :options, :block
21
+ def initialize(app, options = Hash.new, &block)
22
+ @app, @options, @block = app, options, block
23
+ @options[:indentation] ||= 2
24
+ @options[:content_types] ||= self.class::DEFAULT_TYPES
25
+ end
26
+
27
+ def call(env)
28
+ status, headers, chunks = @app.call(env)
29
+ check_content_type(headers)
30
+ if options[:content_types].empty? || options[:content_types].any? { |type| headers[CONTENT_TYPE].match(type) }
31
+ run(env, status, headers, chunks)
32
+ else
33
+ [status, headers, chunks]
34
+ end
35
+ end
19
36
 
20
- def call(env)
21
37
  # convert to a Nokogiri document
22
- status, headers, chunks = @app.call(env)
23
- # the only what we know about body is that it has to respond to #each
24
- body = String.new
25
- chunks.each { |chunk| body += chunk }
26
- document = Nokogiri::HTML(body) # before
27
- @stack = lambda { |env| [status, headers, document] }
28
-
29
- # get middlewares
30
- self.instance_eval(&@block) if @block
31
-
32
- # convert back to a [String]
33
- status, headers, document = @stack.call(env)
34
- body = document.to_html(indentation: options[:indentation]) # TODO
35
- headers["Content-Length"] = body.bytesize.to_s
36
- [status, headers, [body]]
38
+ def run(env, status, headers, chunks)
39
+ # the only what we know about body is that it has to respond to #each
40
+ body = String.new
41
+ chunks.each { |chunk| body += chunk }
42
+ document = Nokogiri::HTML(body) # before
43
+ @stack = lambda { |env| [status, headers, document] }
44
+
45
+ # get middlewares
46
+ self.instance_eval(&@block) if @block
47
+
48
+ # convert back to a [String]
49
+ status, headers, document = @stack.call(env)
50
+ body = document.to_html(indentation: options[:indentation]) # TODO
51
+ headers[CONTENT_LENGTH] = body.bytesize.to_s
52
+ return [status, headers, [body]]
53
+ end
54
+
55
+ # GoogleAnalytics.new(Other.new(self), tracking_code)
56
+ def use(klass, *args, &block)
57
+ @stack = klass.new(@stack, *args, &block)
58
+ end
59
+
60
+ protected
61
+ def check_content_type(headers)
62
+ if headers[CONTENT_TYPE].nil? && !options[:content_types].empty?
63
+ raise "Content-Type has to be set before you call multigiri, so multigiri can decide if it will parse the document or not!"
64
+ end
65
+ end
37
66
  end
38
67
 
39
- # GoogleAnalytics.new(Other.new(self), tracking_code)
40
- def use(klass, *args, &block)
41
- @stack = klass.new(@stack, *args, &block)
68
+ class XML < HTML
69
+ # XML has many MIME types, if you want to work with all posible XML MIME types, you can use
70
+ # use Multigiri, mime_type: /xml/
71
+ DEFAULT_TYPES = ["text/xml"]
72
+
73
+ def run(env, status, headers, chunks)
74
+ # the only what we know about body is that it has to respond to #each
75
+ body = String.new
76
+ chunks.each { |chunk| body += chunk }
77
+ document = Nokogiri::XML(body) # before
78
+ @stack = lambda { |env| [status, headers, document] }
79
+
80
+ # get middlewares
81
+ self.instance_eval(&@block) if @block
82
+
83
+ # convert back to a [String]
84
+ status, headers, document = @stack.call(env)
85
+ body = document.to_xml(indentation: options[:indentation]) # TODO
86
+ headers[CONTENT_LENGTH] = body.bytesize.to_s
87
+ return [status, headers, [body]]
88
+ end
42
89
  end
43
90
  end
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- class Multigiri
3
+ module Multigiri
4
4
  class DefaultAttributes
5
5
  def initialize(app, defaults = nil)
6
6
  @app, @defaults = app, defaults
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- class Multigiri
3
+ module Multigiri
4
4
  class EmailObfuscator
5
5
  def initialize(app)
6
6
  @app = app
@@ -7,7 +7,7 @@
7
7
  # use GoogleAnalytics, :my_tracking_code if Rango.production?
8
8
  # end
9
9
 
10
- class Multigiri
10
+ module Multigiri
11
11
  class GoogleAnalytics
12
12
  attr_accessor :tracking_code
13
13
  def initialize(app, tracking_code)
@@ -27,7 +27,7 @@ class Multigiri
27
27
 
28
28
  protected
29
29
  def script(body, content)
30
- script = Nokogiri::XML::Node.new(:script, body.document)
30
+ script = Nokogiri::XML::Node.new("script", body.document)
31
31
  script.inner_html = content
32
32
  body.add_child(script)
33
33
  end
@@ -4,7 +4,7 @@
4
4
 
5
5
  # In future it can detect if the browser is capable to deal with given HTML 5 feature and
6
6
  # if yes, then there will be no transformation required. But then carefully with caching :)
7
- class Multigiri
7
+ module Multigiri
8
8
  module HTML5
9
9
  # Browsers supporting HTML 5 should can submit forms through PUT or DELETE natively.
10
10
  class Forms
@@ -8,7 +8,7 @@
8
8
  # end
9
9
  # end
10
10
  # end
11
- class Multigiri
11
+ module Multigiri
12
12
  class LinkChecker
13
13
  def initialize(app, logger, patterns = nil, &block)
14
14
  @app, @logger, @patterns = app, logger, patterns
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- class Multigiri
3
+ module Multigiri
4
4
  class Minify
5
5
  def initialize(app)
6
6
  @app = app
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
- class Multigiri
4
- VERSION = "0.0.4"
3
+ module Multigiri
4
+ VERSION = "0.0.5"
5
5
  end
metadata CHANGED
@@ -1,26 +1,33 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multigiri
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 5
9
+ version: 0.0.5
5
10
  platform: ruby
6
11
  authors:
7
12
  - Jakub Stastny aka Botanicus
8
13
  autorequire:
9
14
  bindir: bin
10
15
  cert_chain:
11
- date: 2010-03-14 00:00:00 +00:00
16
+ date: 2010-03-24 00:00:00 +00:00
12
17
  default_executable: multigiri
13
18
  dependencies:
14
19
  - !ruby/object:Gem::Dependency
15
20
  name: nokogiri
16
- type: :runtime
17
- version_requirement:
18
- version_requirements: !ruby/object:Gem::Requirement
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
19
23
  requirements:
20
24
  - - ">="
21
25
  - !ruby/object:Gem::Version
26
+ segments:
27
+ - 0
22
28
  version: "0"
23
- version:
29
+ type: :runtime
30
+ version_requirements: *id001
24
31
  description: ""
25
32
  email: stastny@101ideas.cz
26
33
  executables: []
@@ -57,7 +64,9 @@ has_rdoc: true
57
64
  homepage: http://github.com/botanicus/multigiri
58
65
  licenses: []
59
66
 
60
- post_install_message: "[\e[32mVersion 0.0.3\e[0m] Added middleware for HTML minification\n"
67
+ post_install_message: "[\e[32mVersion 0.0.5\e[0m] Nokogiri was divided into Nokogiri::HTML & Nokogiri::XML\n\
68
+ [\e[32mVersion 0.0.5\e[0m] Added content_types option which can take an array of strings or regexps for matching if multigiri will run for current content type\n\
69
+ [\e[32mVersion 0.0.5\e[0m] Improved performance\n"
61
70
  rdoc_options: []
62
71
 
63
72
  require_paths:
@@ -66,18 +75,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
66
75
  requirements:
67
76
  - - ">="
68
77
  - !ruby/object:Gem::Version
78
+ segments:
79
+ - 1
80
+ - 9
69
81
  version: "1.9"
70
- version:
71
82
  required_rubygems_version: !ruby/object:Gem::Requirement
72
83
  requirements:
73
84
  - - ">="
74
85
  - !ruby/object:Gem::Version
86
+ segments:
87
+ - 0
75
88
  version: "0"
76
- version:
77
89
  requirements: []
78
90
 
79
91
  rubyforge_project: multigiri
80
- rubygems_version: 1.3.5
92
+ rubygems_version: 1.3.6
81
93
  signing_key:
82
94
  specification_version: 3
83
95
  summary: ""