hydra_cache 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +9 -0
- data/LICENSE +7 -0
- data/Rakefile +11 -0
- data/Readme.md +34 -0
- data/hydra_cache.gemspec +26 -0
- data/lib/hydra_cache.rb +66 -0
- data/lib/hydra_cache/version.rb +3 -0
- data/test/test_helper.rb +9 -0
- data/test/unit/hydra_cache_test.rb +79 -0
- metadata +133 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2011 Jorge Dias, Albert Llop
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
This is a gem to provide caching for typhoeus that works with Test Unit
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Simply install with:
|
6
|
+
|
7
|
+
gem install hydra_cache
|
8
|
+
|
9
|
+
## Basic usage
|
10
|
+
|
11
|
+
You need to configure your hydra instance:
|
12
|
+
|
13
|
+
hydra.cache_setter &HydraCache.method(:setter)
|
14
|
+
hydra.cache_getter &HydraCache.method(:getter)
|
15
|
+
|
16
|
+
There is a parameter called revision. This parameter is useful to generate unique identifier for your requests, so when you change the revision the fixtures would get regenerated.
|
17
|
+
|
18
|
+
You may as well configure the path for your fixture files. Put something like this in your test helper.
|
19
|
+
|
20
|
+
HydraCache.fixtures_path = 'test/fixtures'
|
21
|
+
|
22
|
+
## Prefixes
|
23
|
+
|
24
|
+
Prefixes allow you to configure a sub-directory where to put your fixtures, Eg.
|
25
|
+
|
26
|
+
class Test
|
27
|
+
def setup
|
28
|
+
HydraCache.prefix = 'Something'
|
29
|
+
end
|
30
|
+
|
31
|
+
def teardown
|
32
|
+
HydraCache.prefix = nil # Reset the original value
|
33
|
+
end
|
34
|
+
end
|
data/hydra_cache.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "hydra_cache/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "hydra_cache"
|
7
|
+
s.version = HydraCache::VERSION
|
8
|
+
s.authors = ["Jorge Dias", "Albert Llop"]
|
9
|
+
s.email = ["jorge.dias@xing.com", "albert.llop@xing.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Cache support for Hydra}
|
12
|
+
s.description = %q{This gem provides a cache mechanism for Hydra to avoid hitting the same request multiple times}
|
13
|
+
|
14
|
+
s.rubyforge_project = "hydra_cache"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "rake"
|
22
|
+
|
23
|
+
s.add_development_dependency "minitest"
|
24
|
+
s.add_development_dependency "typhoeus"
|
25
|
+
s.add_development_dependency "test-construct"
|
26
|
+
end
|
data/lib/hydra_cache.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require "hydra_cache/version"
|
2
|
+
|
3
|
+
module HydraCache
|
4
|
+
extend self
|
5
|
+
|
6
|
+
attr_accessor :prefix
|
7
|
+
attr_accessor :revision
|
8
|
+
attr_writer :fixtures_path
|
9
|
+
|
10
|
+
def setter(request)
|
11
|
+
set_cache request
|
12
|
+
end
|
13
|
+
|
14
|
+
def getter(request)
|
15
|
+
if response = get_cache(request)
|
16
|
+
response.request = request
|
17
|
+
else
|
18
|
+
request.cache_timeout = 1
|
19
|
+
end
|
20
|
+
|
21
|
+
response
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_cache(request)
|
25
|
+
filename = cache_filename(request)
|
26
|
+
|
27
|
+
if File.exist?(filename)
|
28
|
+
YAML.load_file(filename)
|
29
|
+
else
|
30
|
+
false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def set_cache(request)
|
35
|
+
filename = cache_filename(request)
|
36
|
+
response = request.response
|
37
|
+
response.request = request.inspect
|
38
|
+
|
39
|
+
FileUtils.mkdir_p(File.dirname(filename))
|
40
|
+
File.open(filename, 'w') do |f|
|
41
|
+
f << response.to_yaml
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def cache_filename(request)
|
46
|
+
uri = URI.parse(request.url)
|
47
|
+
uri.query = uri.query.split('&').sort.join('&') if uri.query
|
48
|
+
|
49
|
+
body = request.body.to_s.split(/\n/).map do |line|
|
50
|
+
Digest::SHA1.hexdigest(line)
|
51
|
+
end.sort.join
|
52
|
+
|
53
|
+
params = Array(request.params).map do |k,v|
|
54
|
+
Digest::SHA1.hexdigest("#{k},#{v}")
|
55
|
+
end.sort.join
|
56
|
+
|
57
|
+
digest = Digest::MD5.hexdigest("#{uri}|#{body}|#{params}|#{request.method}")
|
58
|
+
|
59
|
+
File.join(fixtures_path, prefix.to_s.gsub(/\W+/,'_'), "#{digest}.yml")
|
60
|
+
end
|
61
|
+
|
62
|
+
def fixtures_path
|
63
|
+
@fixtures_path || File.expand_path('fixtures')
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe HydraCache do
|
4
|
+
include Construct::Helpers
|
5
|
+
|
6
|
+
after { HydraCache.fixtures_path = nil }
|
7
|
+
|
8
|
+
let(:hydra) do
|
9
|
+
hydra = Typhoeus::Hydra.new
|
10
|
+
hydra.cache_setter &HydraCache.method(:setter)
|
11
|
+
hydra.cache_getter &HydraCache.method(:getter)
|
12
|
+
hydra
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should create a fixture file after the hit' do
|
16
|
+
r = Typhoeus::Request.new("http://example.com")
|
17
|
+
hydra.queue r
|
18
|
+
|
19
|
+
within_construct do |c|
|
20
|
+
HydraCache.fixtures_path = Dir.pwd
|
21
|
+
Dir["*.yml"].must_be_empty
|
22
|
+
hydra.run
|
23
|
+
Dir["*.yml"].wont_be_empty
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should handle different order of uri query' do
|
28
|
+
r1 = Typhoeus::Request.new("http://example.com?p1=1&p2=2")
|
29
|
+
hydra.queue r1
|
30
|
+
r2 = Typhoeus::Request.new("http://example.com?p2=2&p1=1")
|
31
|
+
hydra.queue r2
|
32
|
+
|
33
|
+
within_construct do |c|
|
34
|
+
HydraCache.fixtures_path = Dir.pwd
|
35
|
+
hydra.run
|
36
|
+
Dir["*.yml"].size.must_equal 1
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should handle different order of params' do
|
41
|
+
r1 = Typhoeus::Request.new("http://example.com", :params => {"p1" => 1, "p2" => 2})
|
42
|
+
hydra.queue r1
|
43
|
+
r2 = Typhoeus::Request.new("http://example.com", :params => {"p2" => 2, "p1" => 1})
|
44
|
+
hydra.queue r2
|
45
|
+
|
46
|
+
within_construct do |c|
|
47
|
+
HydraCache.fixtures_path = Dir.pwd
|
48
|
+
hydra.run
|
49
|
+
Dir["*.yml"].size.must_equal 1
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should handle different order of body' do
|
54
|
+
r1 = Typhoeus::Request.new("http://example.com", :body => "foo=bar\nbaz=wadus", :method => :post)
|
55
|
+
hydra.queue r1
|
56
|
+
r2 = Typhoeus::Request.new("http://example.com", :body => "baz=wadus\nfoo=bar", :method => :post)
|
57
|
+
hydra.queue r2
|
58
|
+
|
59
|
+
within_construct do |c|
|
60
|
+
HydraCache.fixtures_path = Dir.pwd
|
61
|
+
hydra.run
|
62
|
+
Dir["*.yml"].size.must_equal 1
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should handle different methods for same url' do
|
67
|
+
r1 = Typhoeus::Request.new("http://example.com")
|
68
|
+
hydra.queue r1
|
69
|
+
r2 = Typhoeus::Request.new("http://example.com", :method => :post)
|
70
|
+
hydra.queue r2
|
71
|
+
|
72
|
+
within_construct do |c|
|
73
|
+
HydraCache.fixtures_path = Dir.pwd
|
74
|
+
Dir["*.yml"].must_be_empty
|
75
|
+
hydra.run
|
76
|
+
Dir["*.yml"].size.must_equal 2
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hydra_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jorge Dias
|
14
|
+
- Albert Llop
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2012-06-18 00:00:00 Z
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
version_requirements: *id001
|
32
|
+
prerelease: false
|
33
|
+
type: :runtime
|
34
|
+
name: rake
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
hash: 3
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
version: "0"
|
45
|
+
version_requirements: *id002
|
46
|
+
prerelease: false
|
47
|
+
type: :development
|
48
|
+
name: minitest
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
version_requirements: *id003
|
60
|
+
prerelease: false
|
61
|
+
type: :development
|
62
|
+
name: typhoeus
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
version_requirements: *id004
|
74
|
+
prerelease: false
|
75
|
+
type: :development
|
76
|
+
name: test-construct
|
77
|
+
description: This gem provides a cache mechanism for Hydra to avoid hitting the same request multiple times
|
78
|
+
email:
|
79
|
+
- jorge.dias@xing.com
|
80
|
+
- albert.llop@xing.com
|
81
|
+
executables: []
|
82
|
+
|
83
|
+
extensions: []
|
84
|
+
|
85
|
+
extra_rdoc_files: []
|
86
|
+
|
87
|
+
files:
|
88
|
+
- .gitignore
|
89
|
+
- Gemfile
|
90
|
+
- LICENSE
|
91
|
+
- Rakefile
|
92
|
+
- Readme.md
|
93
|
+
- hydra_cache.gemspec
|
94
|
+
- lib/hydra_cache.rb
|
95
|
+
- lib/hydra_cache/version.rb
|
96
|
+
- test/test_helper.rb
|
97
|
+
- test/unit/hydra_cache_test.rb
|
98
|
+
homepage: ""
|
99
|
+
licenses: []
|
100
|
+
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
hash: 3
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
version: "0"
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
hash: 3
|
121
|
+
segments:
|
122
|
+
- 0
|
123
|
+
version: "0"
|
124
|
+
requirements: []
|
125
|
+
|
126
|
+
rubyforge_project: hydra_cache
|
127
|
+
rubygems_version: 1.8.24
|
128
|
+
signing_key:
|
129
|
+
specification_version: 3
|
130
|
+
summary: Cache support for Hydra
|
131
|
+
test_files:
|
132
|
+
- test/test_helper.rb
|
133
|
+
- test/unit/hydra_cache_test.rb
|