faraday-cache-advanced 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/faraday-cache-advanced.gemspec +27 -0
- data/lib/faraday-cache-advanced.rb +1 -0
- data/lib/faraday/cache-advanced/cache-advanced.rb +45 -0
- data/lib/faraday/cache-advanced/version.rb +5 -0
- data/spec/cache_advanced_spec.rb +99 -0
- data/spec/spec_helper.rb +8 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6d8eb1e2bb332beca7a2bfaf2a504257efbb316c
|
4
|
+
data.tar.gz: b00b9aa578854b93cf7b083d99fddc7a50f5c0b1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ba8a706c3b0cb360a0c31dd86bb2118b4c2a8fcad34c2a44bf30007787811a3973bcc09b4533335f0bc2e1ab0eb565515bbd3d3c1ecfe244c86987164f4fac1c
|
7
|
+
data.tar.gz: c97cf2e015722276f90c78b261cb5210b069b935a00918cfdfdddb0ab504fc6726a93c79e94bad04be0cd6c039a75637669758928abf4b409707360271927581
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 John Stewart
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Cache-Advanced
|
2
|
+
|
3
|
+
A simple mechanism to cache POST requests via a faraday middleware.
|
4
|
+
|
5
|
+
Cache Advanced is a tool for when you REALLY, REALLY need to cache POST HTTP requests via Faraday.
|
6
|
+
In general you SHOULD NOT cache POST calls, as they are not supposed to be idempotent, but yeah
|
7
|
+
that doesnt always hold in the real world.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'faraday-cache-advanced'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install faraday-cache-advanced
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
CacheAdvanced is designed to drop in to your Faraday middleware stack in the same way you'd use the standard faraday-middleware caching implementation. The difference being CacheAdvanced doesnt give you opinion about what should/shouldnt be cached, it caches every call that comes thru that returns a :success.
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it ( https://github.com/[my-github-username]/faraday-cache-advanced/fork )
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'faraday/cache-advanced/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "faraday-cache-advanced"
|
8
|
+
spec.version = Faraday::CacheAdvanced::VERSION
|
9
|
+
spec.authors = ["John Stewart"]
|
10
|
+
spec.email = ["johnsinco@icloud.com"]
|
11
|
+
spec.summary = %q{Simple caching middleware to cache ALL Requests including POST requests in faraday}
|
12
|
+
spec.description = %q{Sometimes you gotta cache POST requests. This gem gives you a Faraday middleware to cache POST requests.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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 "faraday"
|
22
|
+
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'faraday/cache-advanced'
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Faraday
|
2
|
+
class CacheAdvanced < Faraday::Middleware
|
3
|
+
|
4
|
+
attr_accessor :store
|
5
|
+
|
6
|
+
register_middleware :post_cache => CacheAdvanced
|
7
|
+
|
8
|
+
def initialize(app, cache = nil, opts = {})
|
9
|
+
@app = app
|
10
|
+
if(storename = opts.delete(:store))
|
11
|
+
@store = lookup_store(storename)
|
12
|
+
else
|
13
|
+
@store = cache || yield
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
key = cache_key(env)
|
19
|
+
response = store.read(key)
|
20
|
+
|
21
|
+
if(response.nil? || env[:request_headers].delete(:must_revalidate))
|
22
|
+
response = @app.call(env)
|
23
|
+
store.write(key, response) if response.success?
|
24
|
+
end
|
25
|
+
|
26
|
+
env[:response] = response
|
27
|
+
env.update response.env unless env[:response_headers]
|
28
|
+
response.env[:method] = env[:method]
|
29
|
+
response.env[:url] = env[:url]
|
30
|
+
|
31
|
+
response
|
32
|
+
end
|
33
|
+
|
34
|
+
def cache_key(env)
|
35
|
+
body = Hash(env[:body])
|
36
|
+
body_string = body.keys.sort.map {|key| "#{key}=#{body[key]}"}.join("&")
|
37
|
+
"#{env[:url]}/#{body_string}"
|
38
|
+
end
|
39
|
+
|
40
|
+
def lookup_store(name, opts)
|
41
|
+
ActiveSupport::Cache.lookup_store(name.to_sym, opts)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday/cache-advanced/cache-advanced'
|
3
|
+
|
4
|
+
describe Faraday::CacheAdvanced do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@cache = TestCache.new
|
8
|
+
request_count = 0
|
9
|
+
response = lambda { |env|
|
10
|
+
[200, {'Content-Type' => 'text/plain'}, "request:#{request_count+=1}"]
|
11
|
+
}
|
12
|
+
broken = lambda { |env|
|
13
|
+
[500, {'Content-Type' => 'text/plain'}, "request:#{request_count+=1}"]
|
14
|
+
}
|
15
|
+
@conn = Faraday.new do |b|
|
16
|
+
b.use CachingLint
|
17
|
+
b.use Faraday::CacheAdvanced, @cache, options
|
18
|
+
b.adapter :test do |stub|
|
19
|
+
stub.get('/', &response)
|
20
|
+
stub.get('/?foo=bar', &response)
|
21
|
+
stub.post('/', {foo: 'bar'}, &response)
|
22
|
+
stub.post('/', {foo: 'bar', baz: 'meh'}, &response)
|
23
|
+
stub.get('/other', &response)
|
24
|
+
stub.get('/broken', &broken)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
let(:options) { {} }
|
30
|
+
|
31
|
+
extend Forwardable
|
32
|
+
def_delegators :@conn, :get, :post
|
33
|
+
|
34
|
+
it 'caches get requests' do
|
35
|
+
expect(get('/').body).to eq('request:1')
|
36
|
+
expect(get('/').body).to eq('request:1')
|
37
|
+
expect(get('/other').body).to eq('request:2')
|
38
|
+
expect(get('/other').body).to eq('request:2')
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'includes request params in the response' do
|
42
|
+
get('/') # make cache
|
43
|
+
response = get('/')
|
44
|
+
puts response.to_hash
|
45
|
+
expect(response.env[:method]).to eq(:get)
|
46
|
+
expect(response.env[:url].request_uri).to eq('/')
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'caches requests with query params' do
|
50
|
+
expect(get('/').body).to eq('request:1')
|
51
|
+
expect(get('/?foo=bar').body).to eq('request:2')
|
52
|
+
expect(get('/?foo=bar').body).to eq('request:2')
|
53
|
+
expect(get('/').body).to eq('request:1')
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'caches POST requests by the BODY' do
|
57
|
+
expect(post('/', {foo: 'bar'}).body).to eq('request:1')
|
58
|
+
expect(post('/', {foo: 'bar'}).body).to eq('request:1')
|
59
|
+
expect(post('/', {foo: 'bar'}).body).to eq('request:1')
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'caches posts requests by the alphabatized body params' do
|
63
|
+
expect(post('/', {foo: 'bar', baz: 'meh'}).body).to eq('request:1')
|
64
|
+
expect(post('/', {baz: 'meh', foo: 'bar'}).body).to eq('request:1')
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'does not cache responses with invalid status code' do
|
68
|
+
expect(get('/broken').body).to eq('request:1')
|
69
|
+
expect(get('/broken').body).to eq('request:2')
|
70
|
+
end
|
71
|
+
|
72
|
+
class TestCache < Hash
|
73
|
+
def read(key)
|
74
|
+
if cached = self[key]
|
75
|
+
Marshal.load(cached)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def write(key, data)
|
80
|
+
self[key] = Marshal.dump(data)
|
81
|
+
end
|
82
|
+
|
83
|
+
def fetch(key)
|
84
|
+
read(key) || yield.tap { |data| write(key, data) }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
class CachingLint < Struct.new(:app)
|
89
|
+
def call(env)
|
90
|
+
app.call(env).on_complete do
|
91
|
+
raise "no headers" unless env[:response_headers].is_a? Hash
|
92
|
+
raise "no response" unless env[:response].is_a? Faraday::Response
|
93
|
+
# raise "env not identical" unless env[:response].env.object_id == env.object_id
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: faraday-cache-advanced
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Stewart
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
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: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: Sometimes you gotta cache POST requests. This gem gives you a Faraday
|
70
|
+
middleware to cache POST requests.
|
71
|
+
email:
|
72
|
+
- johnsinco@icloud.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- faraday-cache-advanced.gemspec
|
83
|
+
- lib/faraday-cache-advanced.rb
|
84
|
+
- lib/faraday/cache-advanced/cache-advanced.rb
|
85
|
+
- lib/faraday/cache-advanced/version.rb
|
86
|
+
- spec/cache_advanced_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: ''
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.4.3
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Simple caching middleware to cache ALL Requests including POST requests in
|
112
|
+
faraday
|
113
|
+
test_files:
|
114
|
+
- spec/cache_advanced_spec.rb
|
115
|
+
- spec/spec_helper.rb
|
116
|
+
has_rdoc:
|