rattlecache 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/Rakefile +8 -0
- data/lib/backends/filesystem.rb +20 -1
- data/lib/rattlecache.rb +12 -1
- data/rattlecache.gemspec +1 -1
- data/test/test_Backend_Filesystem.rb +87 -0
- data/test/test_cache.rb +66 -0
- metadata +8 -4
data/.gemtest
ADDED
File without changes
|
data/Rakefile
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
require 'bundler'
|
2
2
|
require 'rspec/core/rake_task'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.libs << 'test'
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Run tests"
|
10
|
+
task :default => :test
|
3
11
|
|
4
12
|
Bundler::GemHelper.install_tasks
|
5
13
|
RSpec::Core::RakeTask.new(:spec) do |t|
|
data/lib/backends/filesystem.rb
CHANGED
@@ -40,9 +40,17 @@ module Rattlecache
|
|
40
40
|
#puts "Debug: filesystem posted #{object[:key]}"
|
41
41
|
end
|
42
42
|
|
43
|
+
def delete(object_key)
|
44
|
+
begin
|
45
|
+
File.delete(@prefix+object_key)
|
46
|
+
rescue Errno::ENOENT
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
43
51
|
def open_file(objectKey,how="r")
|
44
52
|
begin
|
45
|
-
|
53
|
+
make_sure_dir_exists()
|
46
54
|
File.open(@prefix+objectKey,how)
|
47
55
|
rescue
|
48
56
|
# raise this to the caller
|
@@ -50,6 +58,17 @@ module Rattlecache
|
|
50
58
|
end
|
51
59
|
end
|
52
60
|
|
61
|
+
def make_sure_dir_exists()
|
62
|
+
Dir.mkdir(@prefix) unless File.directory?(@prefix)
|
63
|
+
end
|
64
|
+
|
65
|
+
def flush()
|
66
|
+
make_sure_dir_exists()
|
67
|
+
Dir.foreach(@prefix) do |file|
|
68
|
+
File.delete(@prefix+file) unless File.directory?(file)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
53
72
|
end
|
54
73
|
end
|
55
74
|
end
|
data/lib/rattlecache.rb
CHANGED
@@ -25,7 +25,7 @@ module Rattlecache
|
|
25
25
|
# @param header [Hash]
|
26
26
|
def get(url, header = nil)
|
27
27
|
@header = header
|
28
|
-
@header['User-Agent'] = @header['User-Agent'] << " (with rattlecache)"
|
28
|
+
@header['User-Agent'] = @header['User-Agent'] << " (with rattlecache)" unless header.nil?
|
29
29
|
#puts "Cache class gets you: #{objectKey}"
|
30
30
|
|
31
31
|
# what to do with this request?
|
@@ -71,6 +71,14 @@ module Rattlecache
|
|
71
71
|
@backend.post({:key => sanitize(object[:key]), :header => object[:header], :data => object[:data]})
|
72
72
|
end
|
73
73
|
|
74
|
+
def delete(url)
|
75
|
+
@backend.delete(sanitize(url))
|
76
|
+
end
|
77
|
+
|
78
|
+
def flush
|
79
|
+
@backend.flush()
|
80
|
+
end
|
81
|
+
|
74
82
|
# @param headerline [Hash]
|
75
83
|
# @param mtime [Time]
|
76
84
|
# @return [TrueClass|FalseClass]
|
@@ -110,6 +118,9 @@ module Rattlecache
|
|
110
118
|
# @param query [String]
|
111
119
|
# @return [String]
|
112
120
|
def sort_params(query)
|
121
|
+
if query.nil? or query.empty?
|
122
|
+
return ""
|
123
|
+
end
|
113
124
|
q = Hash.new
|
114
125
|
query.split("&").each do |parampair|
|
115
126
|
q[parampair.split("=")[0]] = parampair.split("=")[1]
|
data/rattlecache.gemspec
CHANGED
@@ -0,0 +1,87 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "rattlecache"
|
3
|
+
|
4
|
+
class MyTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
# Called before every test method runs. Can be used
|
7
|
+
# to set up fixture information.
|
8
|
+
def setup
|
9
|
+
@cache = Rattlecache::Cache.new(:filesystem)
|
10
|
+
@prefix = "/tmp/rattlecache/"
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
# Called after every test method runs. Can be used to tear
|
15
|
+
# down fixture information.
|
16
|
+
|
17
|
+
def teardown
|
18
|
+
# Do nothing
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_flush()
|
22
|
+
@cache.flush
|
23
|
+
assert(File.directory?(@prefix))
|
24
|
+
assert(Dir.entries(@prefix) == [".",".."])
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def test_get_nonexisting_object()
|
29
|
+
@cache.flush
|
30
|
+
res = @cache.get("http://example.com/some/path/some.file")
|
31
|
+
assert_instance_of(Hash,res)
|
32
|
+
assert_equal(404,res[:status])
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_post()
|
36
|
+
@cache.flush
|
37
|
+
testobject = {
|
38
|
+
:key => "http://example.com/some/path/some.file",
|
39
|
+
:header => {"foo" => "bar-thishastobethere-baz", "date" => Time.now(), "content-type" => "text/plain"},
|
40
|
+
:data => %{Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
|
41
|
+
sed diam nonumy eirmod tempor invidunt ut labore et dolore
|
42
|
+
magna aliquyam erat, sed diam voluptua. At vero eos et accusam
|
43
|
+
et justo duo dolores et ea rebum. Stet clita kasd gubergren,
|
44
|
+
no sea takimata sanctus est Lorem ipsum dolor sit amet.}
|
45
|
+
}
|
46
|
+
testobject_key = @cache.sanitize(testobject[:key])
|
47
|
+
@cache.post(testobject)
|
48
|
+
assert(Dir.entries(@prefix).include?(testobject_key))
|
49
|
+
|
50
|
+
# posting this a second time. What happens than?
|
51
|
+
@cache.post(testobject)
|
52
|
+
assert(Dir.entries(@prefix).include?(testobject_key))
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_get()
|
57
|
+
testobject = {
|
58
|
+
:key => "http://example.com/some/path/some.file",
|
59
|
+
:header => {"foo" => "bar-thishastobethere-baz", "date" => Time.now(), "content-type" => "text/plain"},
|
60
|
+
:data => %{Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
|
61
|
+
sed diam nonumy eirmod tempor invidunt ut labore et dolore
|
62
|
+
magna aliquyam erat, sed diam voluptua. At vero eos et accusam
|
63
|
+
et justo duo dolores et ea rebum. Stet clita kasd gubergren,
|
64
|
+
no sea takimata sanctus est Lorem ipsum dolor sit amet.}
|
65
|
+
}
|
66
|
+
@cache.post(testobject)
|
67
|
+
res = @cache.get(testobject[:key])
|
68
|
+
assert_instance_of(Hash,res)
|
69
|
+
# assert that this is 404, cause the object has no validation information
|
70
|
+
assert_equal(404,res[:status])
|
71
|
+
|
72
|
+
testobject[:header] = {
|
73
|
+
"foo" => ["bar-thishastobethere-baz"],
|
74
|
+
"date" => [Time.now()],
|
75
|
+
"content-type" => ["text/plain"],
|
76
|
+
"cache-control" => ["max-age=2592000"]
|
77
|
+
}
|
78
|
+
|
79
|
+
@cache.post(testobject)
|
80
|
+
res = @cache.get(testobject[:key])
|
81
|
+
assert_instance_of(Hash,res)
|
82
|
+
# assert that this is 200, cause the object has "cache-control" => "max-age=2592000"
|
83
|
+
assert_equal(200,res[:status])
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
data/test/test_cache.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "rattlecache"
|
3
|
+
|
4
|
+
class CacheTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
# Called before every test method runs. Can be used
|
7
|
+
# to set up fixture information.
|
8
|
+
def setup
|
9
|
+
@cache = Rattlecache::Cache.new()
|
10
|
+
end
|
11
|
+
|
12
|
+
# Called after every test method runs. Can be used to tear
|
13
|
+
# down fixture information.
|
14
|
+
|
15
|
+
def teardown
|
16
|
+
# Do nothing
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_sort_params()
|
20
|
+
params = "a=1&z=zzz&car=a,b,or,c&bar=2&1=firstofall"
|
21
|
+
sorted_params = "?1=firstofall&a=1&bar=2&car=a,b,or,c&z=zzz"
|
22
|
+
assert_equal(sorted_params,@cache.sort_params(params))
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_request_type()
|
26
|
+
input = "http://eu.battle.net/api/wow/item/25"
|
27
|
+
type = "item"
|
28
|
+
assert_equal(type,@cache.request_type(input))
|
29
|
+
|
30
|
+
input = "http://eu.battle.net/api/wow/guild/nathrezim/Badeverein%20Orgrimmar?fields=members"
|
31
|
+
type = "guild"
|
32
|
+
assert_equal(type,@cache.request_type(input))
|
33
|
+
|
34
|
+
input = "http://eu.battle.net/api/wow/auction/data/nathrezim"
|
35
|
+
type = /auction.*/
|
36
|
+
assert_match(type,@cache.request_type(input))
|
37
|
+
|
38
|
+
input = "http://eu.battle.net/auction-data/nathrezim/auctions.json"
|
39
|
+
type = /auction.*/
|
40
|
+
assert_match(type,@cache.request_type(input))
|
41
|
+
|
42
|
+
input = "http://eu.battle.net/api/wow/character/nathrezim/schnecke?fields=items"
|
43
|
+
type = "character"
|
44
|
+
assert_equal(type,@cache.request_type(input))
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_needs_request_with_given_time?()
|
48
|
+
# an our ago + 30 minutes should be O.K.
|
49
|
+
assert(@cache.needs_request_with_given_time?(60*30,Time.now-(60*60)))
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_sanitize()
|
53
|
+
input = "http://eu.battle.net/api/wow/character/nathrezim/schnecke?fields=items&locale=en_GB"
|
54
|
+
exp = "0d85213fdb08da5936e1783f5dde60dbb159164a2d084b157773e3067bcdac88"
|
55
|
+
assert_equal(exp,@cache.sanitize(input))
|
56
|
+
|
57
|
+
input_with_unsorted_params = "http://eu.battle.net/api/wow/character/nathrezim/schnecke?locale=en_GB&fields=items"
|
58
|
+
assert_equal(@cache.sanitize(input_with_unsorted_params),@cache.sanitize(input))
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_has_fields?()
|
62
|
+
assert(@cache.has_fields?("http://eu.battle.net/api/wow/character/nathrezim/schnecke?fields=items&locale=en_GB"))
|
63
|
+
assert_instance_of(FalseClass,@cache.has_fields?("http://eu.battle.net/api/wow/character/nathrezim/schnecke?locale=en_GB"))
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
metadata
CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
version: "0.
|
7
|
+
- 2
|
8
|
+
version: "0.2"
|
9
9
|
platform: ruby
|
10
10
|
authors:
|
11
11
|
- Marv Cool
|
@@ -38,6 +38,7 @@ extensions: []
|
|
38
38
|
extra_rdoc_files: []
|
39
39
|
|
40
40
|
files:
|
41
|
+
- .gemtest
|
41
42
|
- .gitignore
|
42
43
|
- Gemfile
|
43
44
|
- LICENSE
|
@@ -49,6 +50,8 @@ files:
|
|
49
50
|
- lib/caches/Fieldsrequestcache.rb
|
50
51
|
- lib/rattlecache.rb
|
51
52
|
- rattlecache.gemspec
|
53
|
+
- test/test_Backend_Filesystem.rb
|
54
|
+
- test/test_cache.rb
|
52
55
|
has_rdoc: true
|
53
56
|
homepage: https://github.com/MrMarvin/rattlcache/wiki
|
54
57
|
licenses: []
|
@@ -81,5 +84,6 @@ rubygems_version: 1.3.7
|
|
81
84
|
signing_key:
|
82
85
|
specification_version: 3
|
83
86
|
summary: A smart caching system for battlenet API Requests.
|
84
|
-
test_files:
|
85
|
-
|
87
|
+
test_files:
|
88
|
+
- test/test_Backend_Filesystem.rb
|
89
|
+
- test/test_cache.rb
|