middleman-caching-proxy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cefacd267c25aa55daa8c3aab6adba286294d201
4
+ data.tar.gz: b02aa54ec0e117b924dedf78d0647d994f1d3d4e
5
+ SHA512:
6
+ metadata.gz: dc4feea092dfe37bf0067a8faf6bd5d5f9289c305c6f10570d4f5e421083a8b0326c448b3bd38c9df79e39a5347de28350670b14e2717515a296d3c4e3414999
7
+ data.tar.gz: 24be5f3a39fa8d3d8e10b5f3b27a1d27c83f747bd16996a818a123509fa61951479ad5eeceaf83b2377d2daf9767eb8acf1151e9906bdb256fc31b3c4d6ede0f
@@ -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,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.5
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at joe.g.yates@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in middleman-caching-proxy.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Joe Yates
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,41 @@
1
+ # Middleman::Caching::Proxy
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/middleman/caching/proxy`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'middleman-caching-proxy'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install middleman-caching-proxy
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/middleman-caching-proxy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "middleman/caching/proxy"
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 @@
1
+ require "middleman/caching_proxy"
@@ -0,0 +1,179 @@
1
+ require "middleman-core"
2
+ require "middleman-core/sitemap/resource"
3
+ require "semantic"
4
+
5
+ class CacheItem
6
+ attr_reader :path
7
+ attr_reader :template
8
+ attr_reader :proxy_options
9
+ attr_reader :fingerprint
10
+
11
+ def initialize(path:, template:, proxy_options:, fingerprint:)
12
+ @path = path
13
+ @template = template
14
+ @proxy_options = proxy_options
15
+ @fingerprint = fingerprint
16
+ end
17
+ end
18
+
19
+ class CachedResource
20
+ attr_reader :path
21
+ attr_reader :cached_path
22
+ attr_reader :build_path
23
+
24
+ def initialize(path:, cached_path:, build_path:)
25
+ @path = path
26
+ @cached_path = cached_path
27
+ @build_path = build_path
28
+ end
29
+
30
+ def destination_path
31
+ path[1..-1]
32
+ end
33
+
34
+ def ext
35
+ ""
36
+ end
37
+
38
+ def ignored?
39
+ false # Seems to mean "actualyy include in build?"
40
+ end
41
+
42
+ def binary?
43
+ true # Seems to mean "do a binary copy or make a rack request?"
44
+ end
45
+
46
+ def source_file
47
+ cached_path
48
+ end
49
+
50
+ def content_type
51
+ "text/html"
52
+ end
53
+ end
54
+
55
+ class MiddlemanExtension < ::Middleman::Extension
56
+ option :cache_directory,
57
+ "tmp/proxy_cache",
58
+ "The directory where cache files will be stored"
59
+
60
+ if Semantic::Version.new(Middleman::VERSION).major >= 4
61
+ expose_to_config :proxy_with_cache
62
+ end
63
+
64
+ attr_reader :copy_from_cache
65
+ attr_reader :copy_to_cache
66
+
67
+ def initialize(app, options_hash = {}, &block)
68
+ super
69
+
70
+ require "fileutils"
71
+
72
+ if Semantic::Version.new(Middleman::VERSION).major <= 3
73
+ app.send :include, InstanceMethods
74
+ end
75
+
76
+ @cache_manifest = nil
77
+ @copy_from_cache = []
78
+ @copy_to_cache = []
79
+ end
80
+
81
+ def after_configuration
82
+ ensure_cache_directory
83
+ end
84
+
85
+ def after_build(builder)
86
+ copy_proxied_files_to_cache
87
+ File.open(cache_manifest_path, "w") do |f|
88
+ f.write cache.to_json
89
+ end
90
+ end
91
+
92
+ def add(item)
93
+ if matches_cached?(item)
94
+ copy_from_cache << item
95
+ true
96
+ else
97
+ cache[item.path] = item.fingerprint
98
+ copy_to_cache << item
99
+ false
100
+ end
101
+ end
102
+
103
+ def manipulate_resource_list(resources)
104
+ resources + cached_resources
105
+ end
106
+
107
+ module InstanceMethods
108
+ def proxy_with_cache(path:, template:, proxy_options:, fingerprint:)
109
+ item = CacheItem.new(
110
+ path: path,
111
+ template: template,
112
+ proxy_options: proxy_options,
113
+ fingerprint: fingerprint
114
+ )
115
+ will_use_cache = extensions[:caching_proxy].add(item)
116
+ if !will_use_cache
117
+ proxy item.path, item.template, item.proxy_options
118
+ end
119
+ end
120
+ end
121
+
122
+ private
123
+
124
+ def matches_cached?(item)
125
+ cached_path = cache_path(item.path)
126
+ exists = File.exist?(cached_path)
127
+ return false if !exists
128
+ return false if cache[item.path].nil?
129
+ return false if cache[item.path] != item.fingerprint
130
+ true
131
+ end
132
+
133
+ def copy_proxied_files_to_cache
134
+ copy_to_cache.each do |item|
135
+ cached_path = cache_path(item.path)
136
+ build = build_path(item.path)
137
+ cache_subdirectory = File.dirname(cached_path)
138
+ FileUtils.mkdir_p cache_subdirectory
139
+ FileUtils.cp build, cached_path
140
+ end
141
+ end
142
+
143
+ def cached_resources
144
+ copy_from_cache.map do |item|
145
+ cached_path = cache_path(item.path)
146
+ build = build_path(item.path)
147
+ CachedResource.new(
148
+ path: item.path, cached_path: cached_path, build_path: build
149
+ )
150
+ end
151
+ end
152
+
153
+ def cache
154
+ return @cache_manifest if @cache_manifest
155
+ if File.exist?(cache_manifest_path)
156
+ @cache_manifest = JSON.load(File.read(cache_manifest_path))
157
+ else
158
+ @cache_manifest = {}
159
+ end
160
+ end
161
+
162
+ def cache_manifest_path
163
+ cache_path(".cache.json")
164
+ end
165
+
166
+ def cache_path(path)
167
+ ::File.join(options.cache_directory, path)
168
+ end
169
+
170
+ def build_path(path)
171
+ File.join("build", path)
172
+ end
173
+
174
+ def ensure_cache_directory
175
+ FileUtils.mkdir_p options.cache_directory
176
+ end
177
+ end
178
+
179
+ ::Middleman::Extensions.register(:caching_proxy, MiddlemanExtension)
@@ -0,0 +1,7 @@
1
+ module Middleman; end
2
+
3
+ module Middleman
4
+ module CachingProxy
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'middleman/caching_proxy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "middleman-caching-proxy"
8
+ spec.version = Middleman::CachingProxy::VERSION
9
+ spec.authors = ["Joe Yates"]
10
+ spec.email = ["joe.g.yates@gmail.com"]
11
+
12
+ spec.summary = %q{Speed up Middleman builds via a cache}
13
+ spec.description = %q{Keeps a cache of proxy pages and uses the cached copy if the proxied object hasn't changed}
14
+ spec.homepage = "https://github.com/joeyates/middleman-caching-proxy"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
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_runtime_dependency "middleman", ">= 3.0.0", "< 4.0.0"
23
+ spec.add_runtime_dependency "semantic"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.13"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe Middleman::Caching::Proxy do
4
+ it "has a version number" do
5
+ expect(Middleman::Caching::Proxy::VERSION).not_to be nil
6
+ end
7
+
8
+ it "does something useful" do
9
+ expect(false).to eq(true)
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require "middleman/caching/proxy"
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: middleman-caching-proxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joe Yates
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: middleman
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 4.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 4.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: semantic
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.13'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.13'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ description: Keeps a cache of proxy pages and uses the cached copy if the proxied
90
+ object hasn't changed
91
+ email:
92
+ - joe.g.yates@gmail.com
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files: []
96
+ files:
97
+ - ".gitignore"
98
+ - ".rspec"
99
+ - ".travis.yml"
100
+ - CODE_OF_CONDUCT.md
101
+ - Gemfile
102
+ - LICENSE.txt
103
+ - README.md
104
+ - Rakefile
105
+ - bin/console
106
+ - bin/setup
107
+ - lib/middleman-caching-proxy.rb
108
+ - lib/middleman/caching_proxy.rb
109
+ - lib/middleman/caching_proxy/version.rb
110
+ - middleman-caching-proxy.gemspec
111
+ - spec/middleman/caching/proxy_spec.rb
112
+ - spec/spec_helper.rb
113
+ homepage: https://github.com/joeyates/middleman-caching-proxy
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.5.1
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Speed up Middleman builds via a cache
137
+ test_files: []