typhoeus_spec_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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ typhoeus_spec_cache.gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 David Balatero <david@mediapiston.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = typhoeus_spec_cache
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 David Balatero. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "typhoeus_spec_cache"
8
+ gem.summary = %Q{A plugin to help you dynamically manage HTTP caching for your specs.}
9
+ gem.description = %Q{A plugin to help you dynamically manage HTTP caching for your specs.}
10
+ gem.email = "dbalatero@gmail.com"
11
+ gem.homepage = "http://github.com/dbalatero/typhoeus_spec_cache"
12
+ gem.authors = ["David Balatero"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "typhoeus_spec_cache #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,24 @@
1
+ require 'yaml'
2
+
3
+ module Typhoeus
4
+ class Response
5
+ def _dump(depth)
6
+ fields = {
7
+ :code => @code,
8
+ :headers => @headers,
9
+ :body => @body,
10
+ :time => @time,
11
+ :requested_url => @requested_url,
12
+ :requested_http_method => @requested_http_method,
13
+ :start_time => @start_time
14
+ }
15
+
16
+ YAML.dump(fields)
17
+ end
18
+
19
+ def self._load(string)
20
+ options = YAML.load(string)
21
+ Typhoeus::Response.new(options)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,87 @@
1
+ module Typhoeus
2
+ class SpecCache
3
+ attr_accessor :responses
4
+ attr_accessor :cache_path
5
+ attr_reader :hydra
6
+
7
+ def initialize(hydra, cache_path)
8
+ @responses = {}
9
+ @cache_path = cache_path
10
+
11
+ @hydra = hydra
12
+ @hydra.cache_setter do |request|
13
+ cache_setter_callback(request)
14
+ end
15
+ @hydra.cache_getter do |request|
16
+ cache_getter_callback(request)
17
+ end
18
+
19
+ read_cache_fixtures!
20
+ end
21
+
22
+ def cache_setter_callback(request)
23
+ responses[request.cache_key] = request.response
24
+ end
25
+
26
+ def cache_getter_callback(request)
27
+ responses[request.cache_key]
28
+ end
29
+
30
+ def filepath_for(cache_key)
31
+ "#{cache_path}/#{cache_key}.cache"
32
+ end
33
+
34
+ # Removes any cache files that aren't needed anymore
35
+ def remove_unnecessary_cache_files!
36
+ current_keys = cache_files.map do |file|
37
+ get_cache_key_from_filename(file)
38
+ end
39
+ inmemory_keys = responses.keys
40
+
41
+ unneeded_keys = current_keys - inmemory_keys
42
+ unneeded_keys.each do |key|
43
+ File.unlink(filepath_for(key))
44
+ end
45
+ end
46
+
47
+ # Reads in the cache fixture files to in-memory cache.
48
+ def read_cache_fixtures!
49
+ cache_files.each do |file|
50
+ cache_key = get_cache_key_from_filename(file)
51
+ responses[cache_key] = Marshal.load(File.read(file))
52
+ end
53
+ end
54
+
55
+ # Dumps out any cached items to disk.
56
+ def dump_cache_fixtures!
57
+ responses.each do |cache_key, response|
58
+ File.open(filepath_for(cache_key), "wb") do |fp|
59
+ fp.write(Marshal.dump(response))
60
+ end
61
+ end
62
+ end
63
+
64
+ def clear_hydra_callbacks!
65
+ if hydra.respond_to?(:clear_cache_callbacks)
66
+ hydra.clear_cache_callbacks
67
+ else
68
+ # you like that? do you?!?!
69
+ hydra.instance_variable_set("@cache_setter", nil)
70
+ hydra.instance_variable_set("@cache_getter", nil)
71
+ end
72
+ end
73
+
74
+ def clear_cache!
75
+ @responses = {}
76
+ end
77
+
78
+ def cache_files
79
+ Dir.glob("#{cache_path}/*.cache")
80
+ end
81
+
82
+ private
83
+ def get_cache_key_from_filename(file)
84
+ File.basename(file).gsub(/\.cache/, '')
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,25 @@
1
+ module Typhoeus
2
+ module SpecCacheMacros
3
+ module InstanceMethods
4
+ def stub_hydra(hydra)
5
+ Typhoeus::Hydra.stub(:new).
6
+ and_return(hydra)
7
+ end
8
+ end
9
+
10
+ module ClassMethods
11
+ def typhoeus_spec_cache(cache_path, &block)
12
+ hydra = Typhoeus::Hydra.new
13
+ cache = SpecCache.new(hydra, cache_path)
14
+
15
+ yield hydra
16
+
17
+ after(:all) do
18
+ cache.remove_unnecessary_cache_files!
19
+ cache.dump_cache_fixtures!
20
+ cache.clear_hydra_callbacks!
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ gem 'typhoeus'
3
+ require 'typhoeus'
4
+
5
+ Dir.glob(File.dirname(__FILE__) + "/**/*.rb").each do |f|
6
+ require f
7
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'typhoeus_spec_cache'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,31 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Typhoeus::SpecCacheMacros do
4
+ describe "#typhoeus_spec_cache" do
5
+ include Typhoeus::SpecCacheMacros::ClassMethods
6
+ before(:each) do
7
+ @path = "path/to/cache"
8
+ @cache = mock('spec cache')
9
+ Typhoeus::SpecCache.should_receive(:new).
10
+ with(an_instance_of(Typhoeus::Hydra), @path).and_return(@cache)
11
+ @cache.should_receive(:remove_unnecessary_cache_files!).ordered
12
+ @cache.should_receive(:dump_cache_fixtures!).ordered
13
+ @cache.should_receive(:clear_hydra_callbacks!)
14
+
15
+ self.should_receive(:after).
16
+ with(:all).
17
+ and_yield
18
+
19
+ @value_to_modify = nil
20
+ block_to_run = lambda { |hydra|
21
+ @value_to_modify = 50
22
+ }
23
+
24
+ typhoeus_spec_cache(@path, &block_to_run)
25
+ end
26
+
27
+ it "should run the block successfully" do
28
+ @value_to_modify.should == 50
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,104 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ def cache_path(name)
4
+ File.expand_path(File.dirname(__FILE__) + "/../fixtures/#{name}")
5
+ end
6
+
7
+ describe Typhoeus::SpecCache do
8
+ before(:each) do
9
+ @hydra = mock('hydra')
10
+ @hydra.should_receive(:cache_setter)
11
+ @hydra.should_receive(:cache_getter)
12
+ @cache = Typhoeus::SpecCache.new(@hydra,
13
+ cache_path('cache'))
14
+ @request = mock('typhoeus request',
15
+ :cache_key => 'mykey',
16
+ :response => mock('typhoeus response'))
17
+ end
18
+
19
+ describe "#cache_setter_callback" do
20
+ it "should cache a request" do
21
+ @cache.cache_setter_callback(@request)
22
+ @cache.responses['mykey'].should == @request.response
23
+ end
24
+ end
25
+
26
+ describe "#cache_getter_callback" do
27
+ it "should return nil if there is no cache hit" do
28
+ result = @cache.cache_getter_callback(@request)
29
+ result.should be_nil
30
+ end
31
+
32
+ it "should return the response if there is a cache hit" do
33
+ @cache.cache_setter_callback(@request)
34
+ result = @cache.cache_getter_callback(@request)
35
+ result.should == @request.response
36
+ end
37
+ end
38
+
39
+ describe "#clear_hydra_callbacks!" do
40
+ it "should not completely die" do
41
+ @cache.clear_hydra_callbacks!
42
+ end
43
+ end
44
+
45
+ describe "#read_cache_fixtures!" do
46
+ it "should read in cache fixtures that are dumped out" do
47
+ @cache.cache_setter_callback(@request)
48
+ @cache.dump_cache_fixtures!
49
+ @cache.clear_cache!
50
+ @cache.responses.should have(0).things
51
+
52
+ @cache.read_cache_fixtures!
53
+ @cache.responses.should have(1).things
54
+ @cache.responses[@request.cache_key].should be_an_instance_of(Spec::Mocks::Mock)
55
+ end
56
+ end
57
+
58
+ describe "#remove_unnecessary_cache_files!" do
59
+ it "should remove any .cache files that are in the cache directory, but not needed" do
60
+ @cache.cache_setter_callback(@request)
61
+ @cache.dump_cache_fixtures!
62
+
63
+ old_keys = ['oldkey', 'oldkey2']
64
+ old_keys.each do |key|
65
+ File.open(@cache.filepath_for(key), "wb") do |f|
66
+ f.write "blah blah blah"
67
+ end
68
+ end
69
+
70
+ @cache.cache_files.should have(3).things
71
+ @cache.remove_unnecessary_cache_files!
72
+ @cache.cache_files.should have(1).things
73
+ end
74
+ end
75
+
76
+ describe "#clear_cache!" do
77
+ it "should clear the in-memory cache" do
78
+ @cache.cache_setter_callback(@request)
79
+ @cache.responses.should have(1).thing
80
+ @cache.clear_cache!
81
+ @cache.responses.should be_empty
82
+ end
83
+ end
84
+
85
+ describe "#dump_cache_fixtures!" do
86
+ it "should dump out the responses to disk" do
87
+ @cache.cache_setter_callback(@request)
88
+ @cache.responses.should have(1).thing
89
+ @cache.dump_cache_fixtures!
90
+ @cache.responses.each do |key, response|
91
+ path = @cache.filepath_for(key)
92
+ File.exist?(path).should be_true
93
+
94
+ # cleanup
95
+ File.unlink(path)
96
+ File.exist?(path).should be_false
97
+ end
98
+ end
99
+ end
100
+
101
+ after(:each) do
102
+ @cache.cache_files.each { |f| File.unlink(f) }
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typhoeus_spec_cache
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - David Balatero
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-06 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: A plugin to help you dynamically manage HTTP caching for your specs.
35
+ email: dbalatero@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - lib/typhoeus/response_marshalling.rb
51
+ - lib/typhoeus/spec_cache.rb
52
+ - lib/typhoeus/spec_cache_macros.rb
53
+ - lib/typhoeus_spec_cache.rb
54
+ - spec/spec.opts
55
+ - spec/spec_helper.rb
56
+ - spec/typhoeus/spec_cache_macros_spec.rb
57
+ - spec/typhoeus/spec_cache_spec.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/dbalatero/typhoeus_spec_cache
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.6
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: A plugin to help you dynamically manage HTTP caching for your specs.
88
+ test_files:
89
+ - spec/spec_helper.rb
90
+ - spec/typhoeus/spec_cache_macros_spec.rb
91
+ - spec/typhoeus/spec_cache_spec.rb