nokogiri-cache 1.0.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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MThmNjkyMTVjYjA0MDgzOWNiOTVjMTc3ZjY2NWFmZGJhNDE1NDNhOA==
5
+ data.tar.gz: !binary |-
6
+ ZDk2OWVkM2Y4YWY1MjRkZjcwOWFlNzg5ZTFkY2MyOTgxZDZhYTliYw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MmI2NDAzOGM4Nzk5YjEwMzExYmE5YWE1NDIzOWNlMjdlNTg4NzFkN2Y3OGYx
10
+ OTQzZGJjMGNlYTE4MDhkZWIxZjdmZDRlOTU2ZGEzYjUxNmIwOWJkODczMDZi
11
+ YWRhMGE0ZmJjMzcwNzZkYjg5NTUwMmE0NDlmODlhYmI1ZjkwY2U=
12
+ data.tar.gz: !binary |-
13
+ ZGEwOTY0NmFiOTI1OGQ2YWQzOTRlZDZjY2E1NGU2NGIzMjUxN2UwZTQ1OWIy
14
+ MTIwNWI5NjU1MzE3ZDllYjM5YjE4MjViMmI5MmIxYWViYWU1OWVkYWQ0MjY4
15
+ YzMyNjM5ZGY2YThiYTA0YmUwOGU5MThiOGMzZWU1MmRlZTczZjE=
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0
5
+ - 2.1
6
+ - 2.2
7
+ - 2.3
8
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nokogiri-cache.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Daniel Vandersluis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # Nokogiri::Cache
2
+
3
+ Add fragment caching when using `Nokogiri::XML::Builder` to build XML files in a Rails application.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'nokogiri-cache'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install nokogiri-cache
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ Nokogiri::XML::Builder.new do |xml|
25
+ xml.cache 'cache_key' do |xml|
26
+ xml....
27
+ end
28
+ end
29
+ ```
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dvandersluis/nokogiri-cache.
40
+
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
45
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,73 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'benchmark/ips'
4
+ require 'action_controller'
5
+ require 'nokogiri/cache'
6
+
7
+ def controller
8
+ ActionController::Base.new
9
+ end
10
+ public :controller
11
+
12
+ Benchmark.ips do |x|
13
+ x.report('uncached') do
14
+ Nokogiri::XML::Builder.new do |xml|
15
+ xml.countries do
16
+ xml.country(name: 'Canada') do
17
+ xml.languages do
18
+ xml.language 'English'
19
+ xml.language 'French'
20
+ end
21
+ end
22
+
23
+ xml.country(name: 'United States') do
24
+ xml.abbreviation 'USA'
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ x.report('cached') do
31
+ ActionController::Base.perform_caching = true
32
+
33
+ Nokogiri::XML::Builder.new do |xml|
34
+ xml.cache 'countries' do |xml|
35
+ xml.countries do
36
+ xml.country(name: 'Canada') do
37
+ xml.languages do
38
+ xml.language 'English'
39
+ xml.language 'French'
40
+ end
41
+ end
42
+
43
+ xml.country(name: 'United States') do
44
+ xml.abbreviation 'USA'
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ x.report('Rails.cache disabled') do
52
+ ActionController::Base.perform_caching = false
53
+
54
+ Nokogiri::XML::Builder.new do |xml|
55
+ xml.cache 'countries' do |xml|
56
+ xml.countries do
57
+ xml.country(name: 'Canada') do
58
+ xml.languages do
59
+ xml.language 'English'
60
+ xml.language 'French'
61
+ end
62
+ end
63
+
64
+ xml.country(name: 'United States') do
65
+ xml.abbreviation 'USA'
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ x.compare!
73
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nokogiri/cache"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,66 @@
1
+ require "nokogiri/cache/version"
2
+ require 'nokogiri'
3
+
4
+ module Nokogiri
5
+ module Cache
6
+ # Allow nokogiri templates to do fragment caching
7
+ # We can't just use Rails' built in cache helper because the document is not built as we go along so
8
+ # the output buffer has no data
9
+ # Instead, we can override caching to be able to put XML fragments into the cache and pull them out
10
+ # to be inserted into a document later
11
+ # NOTE: This code mostly mirrors action_view/helpers/cache_helper.rb
12
+ def cache(name, options = nil, &block)
13
+ @context = eval('self', block.binding)
14
+ controller = @context.controller
15
+
16
+ if controller.perform_caching
17
+ get_fragment(controller, name, options, &block)
18
+ else
19
+ yield self
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def get_fragment(controller, name, options, &block)
26
+ if fragment = read_fragment(name, options)
27
+ self << fragment.strip
28
+ else
29
+ # Create an XML fragment to insert the cached XML into
30
+ # A root node is needed in case there are multiple nodes being inserted at the same level
31
+ fragment = Nokogiri::XML::Builder.new { |xml| xml.root }
32
+ Nokogiri::XML::Builder.with(fragment.doc.children.first, &block)
33
+
34
+ # Grab the XML within the root node and render it to a string (we don't want to include the root node as it was
35
+ # just added for processing)
36
+ xml = fragment.doc.root.children.to_xml(encoding: 'UTF-8', save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
37
+
38
+ # Save the fragment XML to the cache
39
+ write_fragment(name, xml, options)
40
+ self << xml.strip
41
+ end
42
+ end
43
+
44
+ def read_fragment(key, options)
45
+ controller.read_fragment(key, options)
46
+
47
+ rescue Redis::CannotConnectError, Redis::ConnectionError, Redis::TimeoutError => e
48
+ handle_cache_error(e)
49
+ nil
50
+ end
51
+
52
+ def write_fragment(key, value, options)
53
+ controller.write_fragment(key, value, options)
54
+
55
+ rescue Redis::CannotConnectError, Redis::ConnectionError, Redis::TimeoutError => e
56
+ handle_cache_error(e)
57
+ nil
58
+ end
59
+
60
+ def handle_cache_error(e)
61
+ Rails.logger.error 'Cache is not available'
62
+ end
63
+ end
64
+ end
65
+
66
+ Nokogiri::XML::Builder.send(:include, Nokogiri::Cache)
@@ -0,0 +1,5 @@
1
+ module Nokogiri
2
+ module Cache
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nokogiri/cache/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'nokogiri-cache'
8
+ spec.version = Nokogiri::Cache::VERSION
9
+ spec.authors = ['Daniel Vandersluis']
10
+ spec.email = ['daniel.vandersluis@gmail.com']
11
+
12
+ spec.summary = %q{Allow Rails to cache fragments of XML built by nokogiri}
13
+ spec.description = %q{Allow Rails to cache fragments of XML built by nokogiri}
14
+ spec.homepage = 'https://www.github.com/dvandersluis/nokogiri-cache'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.11'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.0'
25
+ spec.add_development_dependency 'benchmark-ips'
26
+
27
+ spec.add_dependency 'rails', '~> 3.0'
28
+ spec.add_dependency 'nokogiri', '~> 1.6.7'
29
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nokogiri-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Vandersluis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ type: :development
15
+ prerelease: false
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.11'
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ type: :development
29
+ prerelease: false
30
+ name: rake
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: '10.0'
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
+ type: :development
43
+ prerelease: false
44
+ name: rspec
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ~>
48
+ - !ruby/object:Gem::Version
49
+ version: '3.0'
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ type: :development
57
+ prerelease: false
58
+ name: benchmark-ips
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ type: :runtime
71
+ prerelease: false
72
+ name: rails
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '3.0'
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ type: :runtime
85
+ prerelease: false
86
+ name: nokogiri
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ version: 1.6.7
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 1.6.7
97
+ description: Allow Rails to cache fragments of XML built by nokogiri
98
+ email:
99
+ - daniel.vandersluis@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .rspec
106
+ - .travis.yml
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - benchmarks/cache_benchmark.rb
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/nokogiri/cache.rb
115
+ - lib/nokogiri/cache/version.rb
116
+ - nokogiri-cache.gemspec
117
+ homepage: https://www.github.com/dvandersluis/nokogiri-cache
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.1.5
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Allow Rails to cache fragments of XML built by nokogiri
141
+ test_files: []
142
+ has_rdoc: