vcr-remote-controller 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.
Files changed (4) hide show
  1. data/LICENSE +18 -0
  2. data/README.md +21 -0
  3. data/lib/vcr_remote_controller.rb +124 -0
  4. metadata +69 -0
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2011 Charles Barbier <http://github.com/unixcharles>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ VCR Remote Controller
2
+ =====================
3
+
4
+ VCR Remote Controller allow you to manage your VCR cassettes in development mode.
5
+ This Rack middleware let you select existing cassettes, create a new one, insert, eject
6
+ and see newly recorded request.
7
+
8
+ Based on the idea behind [Avdi Grimm](https://github.com/avdi) [screencast](http://avdi.org/devblog/2011/04/11/screencast-taping-api-interactions-with-vcr/) about VCR.
9
+
10
+ Usage
11
+ =====
12
+
13
+ group :development do
14
+ # ...
15
+ gem 'vcr-remote-controller'
16
+ end
17
+
18
+ require 'vcr_remote_controller'
19
+ use Rack::VcrRemoteController
20
+
21
+ And open your browser to /vcr-remote
@@ -0,0 +1,124 @@
1
+ module Rack
2
+ class VcrRemoteController
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ if env["PATH_INFO"] == "/vcr-remote" and VCR
9
+ controller env
10
+ else
11
+ @app.call env
12
+ end
13
+ end
14
+
15
+ protected
16
+ def controller(env)
17
+ req = Rack::Request.new(env)
18
+ if req.post?
19
+ VCR.eject_cassette if cassette?
20
+ if req.params['cassette'] == 'create_new_cassette'
21
+ VCR.insert_cassette(req.params["new_cassette_name"], :record => req.params['record_mode'].to_sym)
22
+ else
23
+ VCR.insert_cassette(req.params['cassette'], :record => req.params['record_mode'].to_sym)
24
+ end if req.params['submit'] != 'Eject'
25
+ end
26
+ response
27
+ end
28
+
29
+ def cassettes
30
+ Dir["#{VCR::Config.cassette_library_dir}/**/*.yml"].map do |f|
31
+ f.match(/^#{Regexp.escape(VCR::Config.cassette_library_dir)}\/(.+)\.yml/)[1]
32
+ end
33
+ end
34
+
35
+ def current_cassette
36
+ VCR.current_cassette ? VCR.current_cassette.name : nil
37
+ end
38
+
39
+ def current_cassette_new_recorded_interactions
40
+ VCR.current_cassette.new_recorded_interactions.map(&:to_yaml).join("\n\n") if cassette?
41
+ end
42
+
43
+ def cassette?
44
+ !(VCR.current_cassette == nil)
45
+ end
46
+
47
+ def current_cassette_empty?
48
+ VCR.current_cassette.new_recorded_interactions.size == 0 if cassette?
49
+ end
50
+
51
+ def current_cassette_record_mode
52
+ VCR.current_cassette.record_mode if cassette?
53
+ end
54
+
55
+ def default_record_mode
56
+ VCR::Config.default_cassette_options[:record]
57
+ end
58
+
59
+ def current_cassette_status
60
+ if cassette?
61
+ %Q{<p>Current cassette: <b>#{current_cassette}</b> #{ '- (empty)' if current_cassette_empty? }</p>
62
+ <p>Record mode: <b>:#{current_cassette_record_mode}</b></p>}
63
+ else
64
+ '<p>No cassette in the VCR</p>'
65
+ end
66
+ end
67
+
68
+ def cassettes_radio_fields
69
+ cassettes.map do |cassette|
70
+ cassette_name = CGI::escapeHTML(cassette)
71
+ selected = current_cassette == cassette
72
+ %Q{<p><label><input type="radio" name="cassette" value="#{cassette_name}"#{' checked' if selected}>#{cassette_name}</label></p>}
73
+ end.join("\n")
74
+ end
75
+
76
+ def record_modes_fields
77
+ [:once, :new_episodes, :none, :all].map do |record_mode|
78
+ %Q{<label><input type="radio" name="record_mode" value="#{record_mode}"#{ ' checked' if record_mode == default_record_mode}>:#{record_mode}</label>}
79
+ end.join("\n")
80
+ end
81
+
82
+ def new_recored_information
83
+ %Q{<p>New recorded interactions</p>
84
+ <hr/>
85
+ <pre><code>
86
+ #{ CGI::escapeHTML current_cassette_new_recorded_interactions }
87
+ </code></pre>} if cassette? and !(current_cassette_empty?)
88
+ end
89
+
90
+ def response
91
+ body = <<-EOF
92
+ <!DOCTYPE html>
93
+ <html>
94
+ <head>
95
+ <title>VCR Remote Controller</title>
96
+ </head>
97
+ <body>
98
+ <h1>VCR Remote Controller</h1>
99
+ #{current_cassette_status}
100
+ <hr/>
101
+ <form method="post">
102
+ <p>Select a cassettes:</p>
103
+ #{cassettes_radio_fields}
104
+ <p><label><input type="radio" name="cassette" value="create_new_cassette"#{' selected' if current_cassette_empty? or !(cassettes.include?(current_cassette)) }>Create a new cassette</label></p>
105
+ <p><label>New cassette name<input type="text" name="new_cassette_name"></label>
106
+ </p>
107
+ <p>Record mode:</p>
108
+ <p>
109
+ #{record_modes_fields}
110
+ </p>
111
+ <p>
112
+ <input type="submit" name="submit" value="#{'Eject and ' if cassette?}Insert cassette">
113
+ <input type="submit" name="submit" value="Eject">
114
+ </p>
115
+ </form>
116
+ #{new_recored_information}
117
+ </body>
118
+ </html>
119
+ EOF
120
+
121
+ [200, {"Content-Type" => "text/html"}, body.gsub(/^ {5}/, "")]
122
+ end
123
+ end
124
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vcr-remote-controller
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Charles Barbier
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-16 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: unixcharles@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - README.md
32
+ - LICENSE
33
+ - lib/vcr_remote_controller.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/unixcharles/vcr-remote-controller
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ hash: 3
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.6.2
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: A remote control for your VCR cassettes
68
+ test_files: []
69
+