faraday-http-cache 0.1.0 → 0.1.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.
- data/lib/faraday/http_cache/response.rb +4 -3
- data/lib/faraday/http_cache.rb +16 -6
- data/spec/json_spec.rb +24 -0
- data/spec/middleware_spec.rb +0 -1
- data/spec/response_spec.rb +37 -32
- data/spec/spec_helper.rb +2 -1
- data/spec/support/test_app.rb +5 -0
- metadata +10 -83
@@ -109,11 +109,12 @@ module Faraday
|
|
109
109
|
(expires && (expires - date))
|
110
110
|
end
|
111
111
|
|
112
|
-
# Internal: Creates a new 'Faraday::Response'
|
112
|
+
# Internal: Creates a new 'Faraday::Response', merging the stored
|
113
|
+
# response with the supplied 'env' object.
|
113
114
|
#
|
114
115
|
# Returns a new instance of a 'Faraday::Response' with the payload.
|
115
|
-
def to_response
|
116
|
-
Faraday::Response.new(@payload)
|
116
|
+
def to_response(env)
|
117
|
+
Faraday::Response.new(env.merge(@payload))
|
117
118
|
end
|
118
119
|
|
119
120
|
private
|
data/lib/faraday/http_cache.rb
CHANGED
@@ -115,14 +115,14 @@ module Faraday
|
|
115
115
|
#
|
116
116
|
# env - the environment 'Hash' provided from the 'Faraday' stack.
|
117
117
|
#
|
118
|
-
# Returns the
|
118
|
+
# Returns the 'Faraday::Response' instance to be served.
|
119
119
|
def process(env)
|
120
120
|
entry = @storage.read(@request)
|
121
121
|
|
122
122
|
return fetch(env) if entry.nil?
|
123
123
|
|
124
124
|
if entry.fresh?
|
125
|
-
response = entry.to_response
|
125
|
+
response = entry.to_response(env)
|
126
126
|
trace :fresh
|
127
127
|
else
|
128
128
|
response = validate(entry, env)
|
@@ -192,11 +192,21 @@ module Faraday
|
|
192
192
|
def fetch(env)
|
193
193
|
trace :miss
|
194
194
|
@app.call(env).on_complete do |env|
|
195
|
-
response = Response.new(env)
|
195
|
+
response = Response.new(create_response(env))
|
196
196
|
store(response)
|
197
197
|
end
|
198
198
|
end
|
199
199
|
|
200
|
+
# Internal: Creates a new 'Hash' containing the response information.
|
201
|
+
#
|
202
|
+
# env - the environment 'Hash' from the Faraday stack.
|
203
|
+
#
|
204
|
+
# Returns a 'Hash' containing the ':status', ':body' and 'response_headers'
|
205
|
+
# entries.
|
206
|
+
def create_response(env)
|
207
|
+
env.slice(:status, :body, :response_headers)
|
208
|
+
end
|
209
|
+
|
200
210
|
# Internal: Creates a new 'Hash' containing the request information.
|
201
211
|
#
|
202
212
|
# env - the environment 'Hash' from the Faraday stack.
|
@@ -204,9 +214,9 @@ module Faraday
|
|
204
214
|
# Returns a 'Hash' containing the ':method', ':url' and 'request_headers'
|
205
215
|
# entries.
|
206
216
|
def create_request(env)
|
207
|
-
|
208
|
-
|
209
|
-
|
217
|
+
request = env.slice(:method, :url)
|
218
|
+
request[:request_headers] = env[:request_headers].dup
|
219
|
+
request
|
210
220
|
end
|
211
221
|
|
212
222
|
# Internal: Logs the trace info about the incoming request
|
data/spec/json_spec.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Faraday::HttpCache do
|
4
|
+
let(:logger) { double('a Logger object', :debug => nil) }
|
5
|
+
|
6
|
+
let(:client) do
|
7
|
+
Faraday.new(:url => ENV['FARADAY_SERVER']) do |stack|
|
8
|
+
stack.response :json, :content_type => /\bjson$/
|
9
|
+
stack.use Faraday::HttpCache, :logger => logger
|
10
|
+
adapter = ENV['FARADAY_ADAPTER']
|
11
|
+
stack.headers['X-Faraday-Adapter'] = adapter
|
12
|
+
stack.adapter adapter.to_sym
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
before do
|
17
|
+
client.get('clear')
|
18
|
+
end
|
19
|
+
|
20
|
+
it "works fine with other middlewares" do
|
21
|
+
client.get('json').body['count'].should == 1
|
22
|
+
client.get('json').body['count'].should == 1
|
23
|
+
end
|
24
|
+
end
|
data/spec/middleware_spec.rb
CHANGED
@@ -135,7 +135,6 @@ describe Faraday::HttpCache do
|
|
135
135
|
end
|
136
136
|
|
137
137
|
it "consumes the 'logger' key" do
|
138
|
-
logger = double('a logger object')
|
139
138
|
ActiveSupport::Cache.should_receive(:lookup_store).with(:memory_store, {})
|
140
139
|
Faraday::HttpCache.new(app, :memory_store, :logger => logger)
|
141
140
|
end
|
data/spec/response_spec.rb
CHANGED
@@ -1,30 +1,30 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe Faraday::HttpCache::Response do
|
4
|
-
describe
|
5
|
-
it "the response isn't
|
6
|
-
headers = {
|
4
|
+
describe "cacheable?" do
|
5
|
+
it "the response isn't cacheable if the response is marked as private" do
|
6
|
+
headers = { "Cache-Control" => "private" }
|
7
7
|
response = Faraday::HttpCache::Response.new(:response_headers => headers)
|
8
8
|
|
9
9
|
response.should_not be_cacheable
|
10
10
|
end
|
11
11
|
|
12
|
-
it "the response isn't
|
13
|
-
headers = {
|
12
|
+
it "the response isn't cacheable if it shouldn't be stored" do
|
13
|
+
headers = { "Cache-Control" => "no-store" }
|
14
14
|
response = Faraday::HttpCache::Response.new(:response_headers => headers)
|
15
15
|
|
16
16
|
response.should_not be_cacheable
|
17
17
|
end
|
18
18
|
|
19
19
|
it "the response isn't cacheable when the status code isn't acceptable" do
|
20
|
-
headers = {
|
20
|
+
headers = { "Cache-Control" => "max-age=400" }
|
21
21
|
response = Faraday::HttpCache::Response.new(:status => 503, :response_headers => headers)
|
22
22
|
response.should_not be_cacheable
|
23
23
|
end
|
24
24
|
|
25
25
|
[200, 203, 300, 301, 302, 404, 410].each do |status|
|
26
26
|
it "the response is cacheable if the status code is #{status} and the response is fresh" do
|
27
|
-
headers = {
|
27
|
+
headers = { "Cache-Control" => "max-age=400" }
|
28
28
|
response = Faraday::HttpCache::Response.new(:status => status, :response_headers => headers)
|
29
29
|
|
30
30
|
response.should be_cacheable
|
@@ -32,10 +32,10 @@ describe Faraday::HttpCache::Response do
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
describe
|
35
|
+
describe "freshness" do
|
36
36
|
it "is fresh if the response still has some time to live" do
|
37
37
|
date = 200.seconds.ago.httpdate
|
38
|
-
headers = {
|
38
|
+
headers = { "Cache-Control" => "max-age=400", 'Date' => date }
|
39
39
|
response = Faraday::HttpCache::Response.new(:response_headers => headers)
|
40
40
|
|
41
41
|
response.should be_fresh
|
@@ -43,7 +43,7 @@ describe Faraday::HttpCache::Response do
|
|
43
43
|
|
44
44
|
it "isn't fresh when the ttl has expired" do
|
45
45
|
date = 500.seconds.ago.httpdate
|
46
|
-
headers = {
|
46
|
+
headers = { "Cache-Control" => "max-age=400", 'Date' => date }
|
47
47
|
response = Faraday::HttpCache::Response.new(:response_headers => headers)
|
48
48
|
|
49
49
|
response.should_not be_fresh
|
@@ -63,33 +63,32 @@ describe Faraday::HttpCache::Response do
|
|
63
63
|
end
|
64
64
|
|
65
65
|
it "returns the 'Last-Modified' header on the #last_modified method" do
|
66
|
-
headers = {
|
66
|
+
headers = { "Last-Modified" => "123"}
|
67
67
|
response = Faraday::HttpCache::Response.new(:response_headers => headers)
|
68
|
-
response.last_modified.should ==
|
68
|
+
response.last_modified.should == "123"
|
69
69
|
end
|
70
70
|
|
71
71
|
it "returns the 'ETag' header on the #etag method" do
|
72
|
-
headers = {
|
72
|
+
headers = { "ETag" => "tag"}
|
73
73
|
response = Faraday::HttpCache::Response.new(:response_headers => headers)
|
74
|
-
response.etag.should ==
|
74
|
+
response.etag.should == "tag"
|
75
75
|
end
|
76
76
|
|
77
|
-
describe
|
78
|
-
|
79
|
-
|
80
|
-
headers = { 'Cache-Control' => 's-maxage=200, max-age=0'}
|
77
|
+
describe "max age calculation" do
|
78
|
+
it "uses the shared max age directive when present" do
|
79
|
+
headers = { "Cache-Control" => "s-maxage=200, max-age=0"}
|
81
80
|
response = Faraday::HttpCache::Response.new(:response_headers => headers)
|
82
81
|
response.max_age.should == 200
|
83
82
|
end
|
84
83
|
|
85
|
-
it
|
86
|
-
headers = {
|
84
|
+
it "uses the max age directive when present" do
|
85
|
+
headers = { "Cache-Control" => "max-age=200"}
|
87
86
|
response = Faraday::HttpCache::Response.new(:response_headers => headers)
|
88
87
|
response.max_age.should == 200
|
89
88
|
end
|
90
89
|
|
91
90
|
it "fallsback to the expiration date leftovers" do
|
92
|
-
headers = {
|
91
|
+
headers = { "Expires" => (Time.now + 100).httpdate, 'Date' => Time.now.httpdate }
|
93
92
|
response = Faraday::HttpCache::Response.new(:response_headers => headers)
|
94
93
|
response.max_age.should == 100
|
95
94
|
end
|
@@ -100,9 +99,9 @@ describe Faraday::HttpCache::Response do
|
|
100
99
|
end
|
101
100
|
end
|
102
101
|
|
103
|
-
describe
|
102
|
+
describe "age calculation" do
|
104
103
|
it "uses the 'Age' header if it's present" do
|
105
|
-
response = Faraday::HttpCache::Response.new(:response_headers => {
|
104
|
+
response = Faraday::HttpCache::Response.new(:response_headers => { "Age" => "3" })
|
106
105
|
response.age.should == 3
|
107
106
|
end
|
108
107
|
|
@@ -118,32 +117,38 @@ describe Faraday::HttpCache::Response do
|
|
118
117
|
end
|
119
118
|
end
|
120
119
|
|
121
|
-
describe
|
120
|
+
describe "time to live calculation" do
|
122
121
|
it "returns the time to live based on the max age limit" do
|
123
122
|
date = 200.seconds.ago.httpdate
|
124
|
-
headers = {
|
123
|
+
headers = { "Cache-Control" => "max-age=400", 'Date' => date }
|
125
124
|
response = Faraday::HttpCache::Response.new(:response_headers => headers)
|
126
125
|
response.ttl.should == 200
|
127
126
|
end
|
128
127
|
end
|
129
128
|
|
130
129
|
describe "response unboxing" do
|
131
|
-
subject { described_class.new(:status => 200, :response_headers => {}, :body =>
|
132
|
-
let(:
|
130
|
+
subject { described_class.new(:status => 200, :response_headers => {}, :body => "Hi!") }
|
131
|
+
let(:env) { { :resquest => mock } }
|
132
|
+
|
133
|
+
let(:response) { subject.to_response(env) }
|
134
|
+
|
135
|
+
it "merges the supplied env object with the response data" do
|
136
|
+
response.env[:resquest].should be
|
137
|
+
end
|
133
138
|
|
134
|
-
it
|
139
|
+
it "returns a Faraday::Response" do
|
135
140
|
response.should be_a Faraday::Response
|
136
141
|
end
|
137
142
|
|
138
|
-
it
|
143
|
+
it "merges the status code" do
|
139
144
|
response.status.should == 200
|
140
145
|
end
|
141
146
|
|
142
|
-
it
|
147
|
+
it "merges the headers" do
|
143
148
|
response.headers.should be_a Faraday::Utils::Headers
|
144
149
|
end
|
145
150
|
|
146
|
-
it
|
151
|
+
it "merges the body" do
|
147
152
|
response.body.should == "Hi!"
|
148
153
|
end
|
149
154
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,9 +2,10 @@ require 'uri'
|
|
2
2
|
require 'socket'
|
3
3
|
|
4
4
|
require 'faraday-http-cache'
|
5
|
+
require 'faraday_middleware'
|
5
6
|
require 'active_support/core_ext/date/calculations'
|
6
7
|
require 'active_support/core_ext/numeric/time'
|
7
|
-
require '
|
8
|
+
require 'json'
|
8
9
|
|
9
10
|
require 'support/test_app'
|
10
11
|
require 'support/test_server'
|
data/spec/support/test_app.rb
CHANGED
@@ -20,6 +20,11 @@ class TestApp < Sinatra::Base
|
|
20
20
|
status 204
|
21
21
|
end
|
22
22
|
|
23
|
+
get '/json' do
|
24
|
+
json = MultiJson.encode(:count => settings.requests += 1)
|
25
|
+
[200, { 'Cache-Control' => 'max-age=400', 'Content-Type' => 'application/json' }, json]
|
26
|
+
end
|
27
|
+
|
23
28
|
post '/post' do
|
24
29
|
[200, { 'Cache-Control' => 'max-age=400' }, "#{settings.requests += 1}"]
|
25
30
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday-http-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -59,86 +59,6 @@ dependencies:
|
|
59
59
|
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '1.3'
|
62
|
-
- !ruby/object:Gem::Dependency
|
63
|
-
name: rake
|
64
|
-
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
|
-
requirements:
|
67
|
-
- - ! '>='
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version: '0'
|
70
|
-
type: :development
|
71
|
-
prerelease: false
|
72
|
-
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
|
-
requirements:
|
75
|
-
- - ! '>='
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
version: '0'
|
78
|
-
- !ruby/object:Gem::Dependency
|
79
|
-
name: rspec
|
80
|
-
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
|
-
requirements:
|
83
|
-
- - ~>
|
84
|
-
- !ruby/object:Gem::Version
|
85
|
-
version: '2.0'
|
86
|
-
type: :development
|
87
|
-
prerelease: false
|
88
|
-
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
|
-
requirements:
|
91
|
-
- - ~>
|
92
|
-
- !ruby/object:Gem::Version
|
93
|
-
version: '2.0'
|
94
|
-
- !ruby/object:Gem::Dependency
|
95
|
-
name: em-http-request
|
96
|
-
requirement: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
|
-
requirements:
|
99
|
-
- - ~>
|
100
|
-
- !ruby/object:Gem::Version
|
101
|
-
version: 1.0.2
|
102
|
-
type: :development
|
103
|
-
prerelease: false
|
104
|
-
version_requirements: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
|
-
requirements:
|
107
|
-
- - ~>
|
108
|
-
- !ruby/object:Gem::Version
|
109
|
-
version: 1.0.2
|
110
|
-
- !ruby/object:Gem::Dependency
|
111
|
-
name: sinatra
|
112
|
-
requirement: !ruby/object:Gem::Requirement
|
113
|
-
none: false
|
114
|
-
requirements:
|
115
|
-
- - ! '>='
|
116
|
-
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
118
|
-
type: :development
|
119
|
-
prerelease: false
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
none: false
|
122
|
-
requirements:
|
123
|
-
- - ! '>='
|
124
|
-
- !ruby/object:Gem::Version
|
125
|
-
version: '0'
|
126
|
-
- !ruby/object:Gem::Dependency
|
127
|
-
name: yajl-ruby
|
128
|
-
requirement: !ruby/object:Gem::Requirement
|
129
|
-
none: false
|
130
|
-
requirements:
|
131
|
-
- - ! '>='
|
132
|
-
- !ruby/object:Gem::Version
|
133
|
-
version: '0'
|
134
|
-
type: :development
|
135
|
-
prerelease: false
|
136
|
-
version_requirements: !ruby/object:Gem::Requirement
|
137
|
-
none: false
|
138
|
-
requirements:
|
139
|
-
- - ! '>='
|
140
|
-
- !ruby/object:Gem::Version
|
141
|
-
version: '0'
|
142
62
|
description: Middleware to handle HTTP caching
|
143
63
|
email:
|
144
64
|
- contact@plataformatec.com.br
|
@@ -154,6 +74,7 @@ files:
|
|
154
74
|
- lib/faraday/http_cache.rb
|
155
75
|
- lib/faraday-http-cache.rb
|
156
76
|
- spec/cache_control_spec.rb
|
77
|
+
- spec/json_spec.rb
|
157
78
|
- spec/middleware_spec.rb
|
158
79
|
- spec/response_spec.rb
|
159
80
|
- spec/spec_helper.rb
|
@@ -172,12 +93,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
172
93
|
- - ! '>='
|
173
94
|
- !ruby/object:Gem::Version
|
174
95
|
version: '0'
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
hash: -4216884502397664395
|
175
99
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
176
100
|
none: false
|
177
101
|
requirements:
|
178
102
|
- - ! '>='
|
179
103
|
- !ruby/object:Gem::Version
|
180
104
|
version: '0'
|
105
|
+
segments:
|
106
|
+
- 0
|
107
|
+
hash: -4216884502397664395
|
181
108
|
requirements: []
|
182
109
|
rubyforge_project:
|
183
110
|
rubygems_version: 1.8.24
|
@@ -186,10 +113,10 @@ specification_version: 3
|
|
186
113
|
summary: A Faraday middleware that stores and validates cache expiration.
|
187
114
|
test_files:
|
188
115
|
- spec/cache_control_spec.rb
|
116
|
+
- spec/json_spec.rb
|
189
117
|
- spec/middleware_spec.rb
|
190
118
|
- spec/response_spec.rb
|
191
119
|
- spec/spec_helper.rb
|
192
120
|
- spec/storage_spec.rb
|
193
121
|
- spec/support/test_app.rb
|
194
122
|
- spec/support/test_server.rb
|
195
|
-
has_rdoc:
|