her_cache_unmodified 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed59eaf5b09dbe51aebc2addc6f9981e88dfd5ef
4
+ data.tar.gz: 9aedc4e34ebcfa4de6485fa9364119833ee1ae18
5
+ SHA512:
6
+ metadata.gz: 0721716fb987d6eb29636034b3ed57115386ccf226815aadc93214782c0a52a5443a5a74580b1979b0e5e12e7e70296156d88e86970e64f18df62524320f04ed
7
+ data.tar.gz: 6d2fb771b5ead2d81458886a5e621b11d7ec6d4f5077af4d930ab02925d2572929fe09aee39715308a3c13e6f9323edd33212ad5fee19ebeee70d16e2811ba7d
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Kvokka
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/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # HerCacheUnmodified
2
+ Easy way to begin support 304 status, so on ther side if data was not changed
3
+ You will load it from local cache. Of course, other side must support
4
+ Of course, other side must support 304 status also.
5
+
6
+ ## Usage
7
+
8
+ At the very begonning you have to include this middleware to `Her`
9
+
10
+
11
+ ```
12
+ # config/initilizers/her.rb
13
+ ...
14
+ Her::API.new.setup do |connection|
15
+ ...
16
+ connection.use HerCacheUnmodified
17
+ end
18
+ ```
19
+
20
+ Here is Rails controller example with this support:
21
+
22
+ ```
23
+ class Post < ApplicationController
24
+ before_action :check_changes
25
+
26
+ private
27
+
28
+ def check_changes
29
+ return unless request.headers['HTTP_IF_MODIFIED_SINCE']
30
+ return if request.headers['HTTP_IF_MODIFIED_SINCE'].to_datetime > resource.updated_at
31
+ render body: nil, status: 304
32
+ end
33
+ end
34
+ ```
35
+
36
+ Feel free to update is as you like
37
+
38
+ ## Installation
39
+ Add this line to your application's Gemfile:
40
+
41
+ ```ruby
42
+ gem 'her_cache_unmodified'
43
+ ```
44
+
45
+ And then execute:
46
+ ```bash
47
+ $ bundle
48
+ ```
49
+
50
+ Or install it yourself as:
51
+ ```bash
52
+ $ gem install her_cache_unmodified
53
+ ```
54
+
55
+ ## Contributing
56
+ Contribution directions go here.
57
+
58
+ ## License
59
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'HerCacheUnmodified'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,3 @@
1
+ module HerCacheUnmodified
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,74 @@
1
+ # Easy way to begin support 304 status, so on ther side if data was not changed
2
+ # You will load it from local cache. Of course, other side must support
3
+ # Of course, other side must support 304 status also.
4
+ # Here is Rails controller example with this support:
5
+ #
6
+ # class Post < ApplicationController
7
+ # before_action :check_changes
8
+ #
9
+ # private
10
+ #
11
+ # def check_changes
12
+ # return unless request.headers['HTTP_IF_MODIFIED_SINCE']
13
+ # return if request.headers['HTTP_IF_MODIFIED_SINCE'].to_datetime > resource.updated_at
14
+ # render body: nil, status: 304
15
+ # end
16
+ # end
17
+ class HerCacheUnmodified < Faraday::Middleware
18
+ SAVE_METHODS = [:get, :head].freeze
19
+
20
+ attr_reader :cache, :options
21
+ attr_accessor :url
22
+
23
+ # apply with optinos:
24
+ # cache # default value is Rails.cache if Rails is used
25
+ # cache_key_prefix # defauilt is nil
26
+
27
+ def initialize(app, options={})
28
+ @app = app
29
+ @cache = options[:cache]
30
+ @cache ||= Rails.cache if defined?(Rails)
31
+ @options = options
32
+ end
33
+
34
+ def call(env)
35
+ return @app.call(env) unless SAVE_METHODS.include?(env[:method])
36
+ self.url = env.url.to_s
37
+ env[:request_headers]["If-Modified-Since"] ||= cached_time if cached_time
38
+
39
+ @app.call(env).on_complete do
40
+ if env[:status] == 304
41
+ if cached_body
42
+ env[:body] = cached_body
43
+ # with out this hack Her crushes
44
+ env[:status] = 200
45
+ end
46
+ elsif env[:status] == 200
47
+ cache.write cache_key_body, env[:body], expires_in: cache_key_ttl
48
+ cache.write cache_key_time, Time.zone.now.httpdate, expires_in: cache_key_ttl
49
+ end
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def cache_key_time
56
+ [options[:cache_key_prefix], "time", url].compact.join("/")
57
+ end
58
+
59
+ def cache_key_body
60
+ [options[:cache_key_prefix], "body", url].compact.join("/")
61
+ end
62
+
63
+ def cached_time
64
+ cache.read(cache_key_time)
65
+ end
66
+
67
+ def cached_body
68
+ cache.read(cache_key_body)
69
+ end
70
+
71
+ def cache_key_ttl
72
+ 1.week.freeze
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: her_cache_unmodified
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kvokka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: her
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.0
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.8.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.0
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.8.0
33
+ description: ''
34
+ email:
35
+ - root_p@mail.ru
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - MIT-LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - lib/her_cache_unmodified.rb
44
+ - lib/her_cache_unmodified/version.rb
45
+ homepage: http://github.com/kvokka/her_cache_unmodified
46
+ licenses:
47
+ - MIT
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.5.1
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Her middleware for 304 request status support
69
+ test_files: []