grape-rails-cache 0.1.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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9ed6145ae0b11da1d0d35a9611c67572a3ebd2c6
4
+ data.tar.gz: c9e5405e3375107967457509aa4051a17363311b
5
+ SHA512:
6
+ metadata.gz: 37d20990ba391b0c4b7e628f673da0c89285362bc66711366934324948a908ba4968828d3bbd09b631fc31fd5e2157367b1f474f6d0469de4167fdcaa1636bf5
7
+ data.tar.gz: 35457c02458efced04c9aae8b29887b64c76f0096c72a859aecfcbf40f7af594729a71c600b091f6814c00612a15c05328b2633eaef291c24238ae450b2e3a9b
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in grape-rails-cache.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Monterail.com
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # HTTP and server side cache integration for Grape and Rails
2
+
3
+ ## Features
4
+
5
+ - HTTP Headers cache, ETag, Cache-Control, If-None-Match
6
+ - Server side cache for response body
7
+
8
+
9
+ ## Installation
10
+
11
+ Add this line to your rails application's Gemfile:
12
+
13
+ gem 'grape-rails-cache'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ module MyApi < Grape::API
23
+ format :json
24
+
25
+ include Grape::Rails::Cache
26
+
27
+ resources :posts do
28
+ desc "Return a post"
29
+ get ":id" do
30
+ post = Post.find(params[:id])
31
+ cache(key: "api:posts:#{post.id}", etag: post.updated_at, expires_in: 2.hours) do
32
+ post # post.extend(PostRepresenter) etc, any code that renders response
33
+ end
34
+ end
35
+ end
36
+ end
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'grape/rails/cache/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "grape-rails-cache"
8
+ spec.version = Grape::Rails::Cache::VERSION
9
+ spec.authors = ["Tymon Tobolski"]
10
+ spec.email = ["tymon.tobolski@monterail.com"]
11
+ spec.description = %q{HTTP and server side cache integration for Grape and Rails}
12
+ spec.summary = %q{HTTP and server side cache integration for Grape and Rails}
13
+ spec.homepage = "https://github.com/monterail/grape-rails-cache"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "grape"
22
+ spec.add_dependency "activesupport"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
@@ -0,0 +1 @@
1
+ require "grape/rails/cache"
@@ -0,0 +1,63 @@
1
+ require "grape/rails/cache/version"
2
+ require "grape/rails/cache/formatter"
3
+
4
+ module Grape
5
+ module Rails
6
+ module Cache
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ formatter :json, Grape::Rails::Cache::JsonFormatter
11
+
12
+ helpers do
13
+ def compare_etag(etag)
14
+ etag = Digest::SHA1.hexdigest(etag.to_s)
15
+ error!("Not Modified", 304) if request.headers["If-None-Match"] == etag
16
+
17
+ header "ETag", etag
18
+ end
19
+
20
+ # Based on actionpack/lib/action_controller/base.rb, line 1216
21
+ def expires_in(seconds, options = {})
22
+ cache_control = []
23
+ cache_control << "max-age=#{seconds}"
24
+ cache_control.delete("no-cache")
25
+ if options[:public]
26
+ cache_control.delete("private")
27
+ cache_control << "public"
28
+ else
29
+ cache_control << "private"
30
+ end
31
+
32
+ # This allows for additional headers to be passed through like 'max-stale' => 5.hours
33
+ cache_control += options.symbolize_keys.reject{|k,v| k == :public || k == :private }.map{ |k,v| v == true ? k.to_s : "#{k.to_s}=#{v.to_s}"}
34
+
35
+ header "Cache-Control", cache_control.join(', ')
36
+ end
37
+
38
+ def default_expire_time
39
+ 2.hours
40
+ end
41
+
42
+ def cache(opts = {}, &block)
43
+ # HTTP Cache
44
+ cache_key = opts[:key]
45
+
46
+ if opts[:etag]
47
+ cache_key += ActiveSupport::Cache.expand_cache_key(opts[:etag])
48
+ compare_etag(opts[:etag]) # Check if client has fresh version
49
+ end
50
+
51
+ # Set Cache-Control
52
+ expires_in(opts[:expires_in] || default_expire_time, public: true)
53
+
54
+ # Try to fetch from server side cache
55
+ ::Rails.cache.fetch(cache_key, raw: true, expires_in: opts[:expires_in]) do
56
+ block.call.to_json
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,12 @@
1
+ module Grape
2
+ module Rails
3
+ module Cache
4
+ # For maximum performance we are fetching string from redis and return them with no parsing at all
5
+ module JsonFormatter
6
+ def self.call(object, env)
7
+ object.is_a?(String) ? object : MultiJson.dump(object)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module Grape
2
+ module Rails
3
+ module Cache
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-rails-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tymon Tobolski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grape
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: HTTP and server side cache integration for Grape and Rails
70
+ email:
71
+ - tymon.tobolski@monterail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - grape-rails-cache.gemspec
82
+ - lib/grape-rails-cache.rb
83
+ - lib/grape/rails/cache.rb
84
+ - lib/grape/rails/cache/formatter.rb
85
+ - lib/grape/rails/cache/version.rb
86
+ homepage: https://github.com/monterail/grape-rails-cache
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.1.3
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: HTTP and server side cache integration for Grape and Rails
110
+ test_files: []
111
+ has_rdoc: