fastly-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +32 -0
- data/lib/fastly-rails.rb +29 -0
- data/lib/fastly-rails/action_controller/cache_control_headers.rb +24 -0
- data/lib/fastly-rails/action_controller/surrogate_control_headers.rb +20 -0
- data/lib/fastly-rails/active_record/surrogate_key.rb +43 -0
- data/lib/fastly-rails/client.rb +17 -0
- data/lib/fastly-rails/configuration.rb +47 -0
- data/lib/fastly-rails/engine.rb +24 -0
- data/lib/fastly-rails/errors.rb +5 -0
- data/lib/fastly-rails/version.rb +3 -0
- data/lib/fastly/purgeable.rb +55 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/controllers/books_controller.rb +66 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/book.rb +12 -0
- data/test/dummy/app/views/books/index.html.erb +2 -0
- data/test/dummy/app/views/books/show.html.erb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +29 -0
- data/test/dummy/config/environments/production.rb +80 -0
- data/test/dummy/config/environments/test.rb +36 -0
- data/test/dummy/config/initializers/_rails_version_check.rb +3 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +16 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +5 -0
- data/test/dummy/db/migrate/20140407202136_create_books.rb +9 -0
- data/test/dummy/db/schema.rb +22 -0
- data/test/dummy/public/404.html +58 -0
- data/test/dummy/public/422.html +58 -0
- data/test/dummy/public/500.html +57 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/test/controllers/books_controller_test.rb +38 -0
- data/test/dummy/test/factories/books.rb +7 -0
- data/test/dummy/test/fixtures/books.yml +7 -0
- data/test/dummy/test/integration/fastly_headers_test.rb +91 -0
- data/test/dummy/test/models/book_test.rb +41 -0
- data/test/fastly-rails/cache_control_headers_test.rb +8 -0
- data/test/fastly-rails/client_test.rb +36 -0
- data/test/fastly-rails/configuration_test.rb +84 -0
- data/test/fastly-rails_test.rb +57 -0
- data/test/test_helper.rb +67 -0
- metadata +270 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e1341f356aab912800b0e0898c11e6b4427b4455
|
4
|
+
data.tar.gz: 31233d8c36f272493f625630131171d89c50a249
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b462f2fcb9e5b89c0ae24274c6e7532fbe56c454d6db265fecd7bb1b8b68430e4a8315c4e243a038a7f59e685a74298650684fd89d4e5755038b1bb4ebc130bd
|
7
|
+
data.tar.gz: 964a2af61358129576c0e41363ba0cffdf9a51d83010691e6aee65480bef2ca8b63ba8aee4af8da7b58115253ab9df2bf3c1f8f0991fcf8045064b349e8a6b5c
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 Fastly, Inc
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
begin
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'rdoc/task'
|
9
|
+
|
10
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
11
|
+
rdoc.rdoc_dir = 'rdoc'
|
12
|
+
rdoc.title = 'FastlyRails'
|
13
|
+
rdoc.options << '--line-numbers'
|
14
|
+
rdoc.rdoc_files.include('README.rdoc')
|
15
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
16
|
+
end
|
17
|
+
|
18
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
t.verbose = false
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
task default: :test
|
data/lib/fastly-rails.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "fastly-rails/engine"
|
2
|
+
require "fastly-rails/client"
|
3
|
+
require "fastly-rails/configuration"
|
4
|
+
require "fastly-rails/errors"
|
5
|
+
|
6
|
+
module FastlyRails
|
7
|
+
|
8
|
+
attr_reader :client, :configuration
|
9
|
+
|
10
|
+
def configuration
|
11
|
+
@configuration ||= Configuration.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def configure
|
15
|
+
yield configuration if block_given?
|
16
|
+
end
|
17
|
+
|
18
|
+
def client
|
19
|
+
raise NoAuthCredentialsProvidedError unless configuration.authenticatable?
|
20
|
+
@client ||= Client.new(
|
21
|
+
:api_key => configuration.api_key,
|
22
|
+
:user => configuration.user,
|
23
|
+
:password => configuration.password,
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
extend self
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module FastlyRails
|
2
|
+
module CacheControlHeaders
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
before_filter :set_cache_control_headers, only: [:index, :show]
|
7
|
+
end
|
8
|
+
|
9
|
+
# Sets Cache-Control and Surrogate-Control headers
|
10
|
+
# Surrogate-Control is stripped at the cache, Cache-Control persists (in case of other caches in front of fastly)
|
11
|
+
# Defaults are:
|
12
|
+
# Cache-Control: 'public, no-cache, s-maxage: 30 days'
|
13
|
+
# Surrogate-Control: 'max-age: 30 days
|
14
|
+
# custom config example:
|
15
|
+
# {cache_control: 'public, no-cache, maxage=xyz', surrogate_control: 'max-age: blah'}
|
16
|
+
def set_cache_control_headers(max_age = FastlyRails.configuration.max_age, opts = {})
|
17
|
+
request.session_options[:skip] = true # no cookies
|
18
|
+
response.headers['Cache-Control'] = opts[:cache_control] || "public, no-cache"
|
19
|
+
response.headers['Surrogate-Control'] = opts[:surrogate_control] || "max-age=#{max_age}"
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module FastlyRails
|
2
|
+
module SurrogateControlHeaders
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
end
|
7
|
+
|
8
|
+
# Sets Surrogate-Control header
|
9
|
+
# Defaults are:
|
10
|
+
# Cache-Control: 'public, no-cache, s-maxage: 30 days'
|
11
|
+
# Surrogate-Control: 'max-age: 30 days
|
12
|
+
# custom config example:
|
13
|
+
# {cache_control: 'blah, blah, blah', surrogate_control: 'max-age: blah'}
|
14
|
+
def set_surrogate_key_header(surrogate_key)
|
15
|
+
request.session_options[:skip] = true # no cookies
|
16
|
+
response.headers['Surrogate-Key'] = surrogate_key
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# adds surrogate key instance methods to the models that inherit from activerecord
|
2
|
+
module FastlyRails
|
3
|
+
module SurrogateKey
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
include InstanceMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def purge_all
|
13
|
+
FastlyRails.client.purge(table_key)
|
14
|
+
end
|
15
|
+
|
16
|
+
def table_key
|
17
|
+
table_name
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
module InstanceMethods
|
23
|
+
|
24
|
+
def record_key
|
25
|
+
"#{table_key}/#{id}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def table_key
|
29
|
+
self.class.table_key
|
30
|
+
end
|
31
|
+
|
32
|
+
def purge
|
33
|
+
FastlyRails.client.purge(record_key)
|
34
|
+
end
|
35
|
+
|
36
|
+
def purge_all
|
37
|
+
self.class.purge_all
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'fastly'
|
2
|
+
|
3
|
+
module FastlyRails
|
4
|
+
|
5
|
+
### For all intents and purposes
|
6
|
+
### This is a wrapper around the
|
7
|
+
### Fastly-ruby client
|
8
|
+
|
9
|
+
class Client < DelegateClass(Fastly)
|
10
|
+
|
11
|
+
def initialize(opts={})
|
12
|
+
super(Fastly.new(opts))
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module FastlyRails
|
2
|
+
|
3
|
+
class Configuration
|
4
|
+
|
5
|
+
attr_reader :api_key, :user, :password, :max_age
|
6
|
+
|
7
|
+
MAX_AGE_DEFAULT = '2592000'
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def max_age_default
|
12
|
+
MAX_AGE_DEFAULT
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@api_key = nil
|
19
|
+
@user = nil
|
20
|
+
@password = nil
|
21
|
+
@max_age = self.class.max_age_default
|
22
|
+
end
|
23
|
+
|
24
|
+
def api_key=(val)
|
25
|
+
@api_key = val
|
26
|
+
end
|
27
|
+
|
28
|
+
def user=(val)
|
29
|
+
@user = val
|
30
|
+
end
|
31
|
+
|
32
|
+
def password=(val)
|
33
|
+
@password = val
|
34
|
+
end
|
35
|
+
|
36
|
+
def max_age=(val)
|
37
|
+
@max_age = val
|
38
|
+
end
|
39
|
+
|
40
|
+
def authenticatable?
|
41
|
+
!api_key.nil? || (!user.nil? && !password.nil?)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'fastly-rails/active_record/surrogate_key'
|
2
|
+
require 'fastly-rails/action_controller/cache_control_headers'
|
3
|
+
require 'fastly-rails/action_controller/surrogate_control_headers'
|
4
|
+
|
5
|
+
module FastlyRails
|
6
|
+
class Engine < ::Rails::Engine
|
7
|
+
isolate_namespace FastlyRails
|
8
|
+
|
9
|
+
config.generators do |g|
|
10
|
+
g.test_framework :mini_test, :spec => true, :fixture => false
|
11
|
+
g.fixture_replacement :factory_girl, :dir => 'test/factories'
|
12
|
+
end
|
13
|
+
|
14
|
+
ActiveSupport.on_load :action_controller do
|
15
|
+
ActionController::Base.send :include, FastlyRails::CacheControlHeaders
|
16
|
+
ActionController::Base.send :include, FastlyRails::SurrogateControlHeaders
|
17
|
+
end
|
18
|
+
|
19
|
+
ActiveSupport.on_load :active_record do
|
20
|
+
ActiveRecord::Base.send :include, FastlyRails::SurrogateKey
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# Object that follows the observer pattern
|
2
|
+
# usage:
|
3
|
+
# Fastly::Purgeable.new(@article).save!
|
4
|
+
# Fastly::Purgeable.new(@article).destroy!
|
5
|
+
# This calls save on the @article and then makes a call to fastly to purge from cache
|
6
|
+
module Fastly
|
7
|
+
class Purgeable
|
8
|
+
|
9
|
+
def self.initialize()
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
# These methods need to hook into activerecord
|
14
|
+
# Calls super
|
15
|
+
# Purges the object from the fastly cache
|
16
|
+
def destroy
|
17
|
+
super
|
18
|
+
@queue << self.resource_key
|
19
|
+
end
|
20
|
+
|
21
|
+
def destroy!
|
22
|
+
super
|
23
|
+
Fastly.purge(self.resource_key)
|
24
|
+
end
|
25
|
+
|
26
|
+
def save
|
27
|
+
super
|
28
|
+
Fastly.purge(self.resource_key)
|
29
|
+
end
|
30
|
+
|
31
|
+
def save!
|
32
|
+
super
|
33
|
+
Fastly.purge(self.resource_key)
|
34
|
+
end
|
35
|
+
|
36
|
+
def update
|
37
|
+
super
|
38
|
+
Fastly.purge(self.resource_key)
|
39
|
+
end
|
40
|
+
|
41
|
+
# updates all records in the table
|
42
|
+
# purge table key
|
43
|
+
def update_all
|
44
|
+
super
|
45
|
+
Fastly.purge(self.table_key)
|
46
|
+
end
|
47
|
+
|
48
|
+
def update_attribute
|
49
|
+
end
|
50
|
+
|
51
|
+
def update_attributes
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/test/dummy/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
9
|
+
* compiled file, but it's generally better to create a new file per style scope.
|
10
|
+
*
|
11
|
+
*= require_self
|
12
|
+
*= require_tree .
|
13
|
+
*/
|
@@ -0,0 +1,66 @@
|
|
1
|
+
class BooksController < ApplicationController
|
2
|
+
|
3
|
+
before_filter :find_book, :only => [:show, :edit, :update, :destroy]
|
4
|
+
|
5
|
+
def index
|
6
|
+
@books = Book.all
|
7
|
+
end
|
8
|
+
|
9
|
+
def show
|
10
|
+
end
|
11
|
+
|
12
|
+
def new
|
13
|
+
@book = Book.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def create
|
17
|
+
@book = Book.new(books_params)
|
18
|
+
if @book.save
|
19
|
+
redirect_to books_path
|
20
|
+
else
|
21
|
+
flash[:notice] = "failed to create book"
|
22
|
+
render :new
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def edit
|
27
|
+
end
|
28
|
+
|
29
|
+
def update
|
30
|
+
if rails_4?
|
31
|
+
method = :update
|
32
|
+
else
|
33
|
+
method = :update_attributes
|
34
|
+
end
|
35
|
+
if @book.send(method, books_params)
|
36
|
+
redirect_to book_path(@book)
|
37
|
+
else
|
38
|
+
flash[:notice] = "failed to update book"
|
39
|
+
render :edit
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def destroy
|
44
|
+
if @book.destroy
|
45
|
+
redirect_to books_path
|
46
|
+
else
|
47
|
+
flash[:notice] = "failed to destroy book"
|
48
|
+
redirect_to book_path(@book)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def books_params
|
55
|
+
if rails_4?
|
56
|
+
params.require(:book).permit!
|
57
|
+
else
|
58
|
+
params[:book]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def find_book
|
63
|
+
@book = Book.find(params[:id])
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|