vcr_assistant 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bce3f6971903eeec4ad0cb9bbe40c6b671bf42cd
4
+ data.tar.gz: ccab9f45f747e919469e975728d8b2d840d2456a
5
+ SHA512:
6
+ metadata.gz: 9956fe2b6aec65b2d2d6dbb84cadb988cfa1c41e2006a8d99a8614eedb6623279482eaa47d55bd02e676c1229b54ba7030bee9993a7a0ea958a351c2e9513a4d
7
+ data.tar.gz: d9e34022fe676336afb3f6458418bee33e4a2cdc62b0f3dd9983c8ea6ba2c6c6c325f0957f9a03c74a39b24085ab70203f7077992d1bdee865942b2fed62ec3e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at pat@freelancing-gods.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vcr_assistant.gemspec
4
+ gemspec
data/LICENCE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 Pat Allan
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.md ADDED
@@ -0,0 +1,163 @@
1
+ # VCR Assistant
2
+
3
+ VCR Assistant is a tidy approach to managing VCR cassettes and setup/teardown logic. Currently it's built for use within RSpec v3 (but patches for other test frameworks are certainly welcome).
4
+
5
+ It will automatically name your VCR cassette for you (based on the current example file name and description). You can also configure your assistant to automatically perform setup and teardown code. Here's a quick example for use with Stripe's test sandbox:
6
+
7
+ ```ruby
8
+ class StripeAssistant
9
+ def initialize(vcr_cassette)
10
+ @vcr_cassette = vcr_cassette
11
+ end
12
+
13
+ def setup
14
+ # Clear out Stripe data at the beginning of a recording
15
+ # to ensure the sandbox environment is consistent.
16
+ Stripe::Customer.all.each &:delete
17
+ Stripe::Coupon.all.each &:delete
18
+ Stripe::Plan.all.each &:delete
19
+ end
20
+ end
21
+
22
+ VCRAssistant.assistant = lambda { |label, vcr_cassette|
23
+ StripeAssistant.new vcr_cassette
24
+ }
25
+
26
+ it 'automatically clears Stripe before tests' do |example|
27
+ assisted_cassette(example) do |assistant|
28
+ # assistant is an instance of StripeAssistant
29
+ # and the `setup` method has already been called.
30
+
31
+ # run test code with external HTTP calls
32
+ # assert expectations
33
+
34
+ # if assistant responded to a teardown method, that
35
+ # would automatically be invoked at the end of this block.
36
+ end
37
+ end
38
+ ```
39
+
40
+ ## Installation
41
+
42
+ Add this line to your application's Gemfile:
43
+
44
+ ```ruby
45
+ gem 'vcr_assistant', '~> 0.1.0', :group => :test
46
+ ```
47
+
48
+ To have the `assisted_cassette` helper to be available in all specs, you can require `'vcr_assistant/rspec'` either in your Gemfile:
49
+
50
+ ```ruby
51
+ gem 'vcr_assistant', '~> 0.1.0',
52
+ :group => :test,
53
+ :require => 'vcr_assistant/rspec'
54
+ ```
55
+
56
+ Or in your `spec_helper.rb` or `rails_helper.rb` file:
57
+
58
+ ```ruby
59
+ require 'vcr_assistant/rspec'
60
+ ```
61
+
62
+ ## Usage
63
+
64
+ Instead of calling `VCR.use_cassette`, instead use either the `assisted_cassette` helper method or `VCRAssistant::Cassette.call(example)` directly. Either way, you need to pass in the current RSpec
65
+
66
+ ```ruby
67
+ it 'should make external HTTP calls' do |example|
68
+ assisted_cassette example do |assistant|
69
+ # make API calls
70
+
71
+ # assert expectations
72
+ end
73
+ end
74
+
75
+ # or
76
+
77
+ it 'should make external HTTP calls' do |example|
78
+ VCRAssistant::Cassette.call example do |assistant|
79
+ # make API calls
80
+
81
+ # assert expectations
82
+ end
83
+ end
84
+ ```
85
+
86
+ ### Custom assistants
87
+
88
+ A standard assistant won't do anything much - you'll just get the advantage of automatically named cassettes. To have a custom assistant, you need to change the value of `VCRAssistant.assistant` to something that responds to `call` - perhaps a Proc or lambda - and then returns an object.
89
+
90
+ If this object responds to `setup`, that will be called before the cassette block is invoked, and same with `teardown` after the block. The arguments sent to the assistant generator are the label and the underlying VCR cassette. The label is useful to distinguish between different uses of VCR - it's the optional second argument for `assisted_cassette` / `VCRAssistant::Cassette.call`. If it's not provided, it has the value `:default`.
91
+
92
+ ```ruby
93
+ VCRAssistant.assistant = lambda { |label, vcr_cassette|
94
+ case label
95
+ when :stripe
96
+ StripeAssistant.new vcr_cassette
97
+ when :paypal
98
+ PayPalAssistant.new vcr_cassette
99
+ else
100
+ nil # An assistant object is not essential
101
+ end
102
+ }
103
+ ```
104
+
105
+ You may want to add further functionality to your assistant - such as preparing regularly used data. For the example of Stripe, you may want to prepare customer or plan objects in some tests:
106
+
107
+ ```ruby
108
+ # This is example code - it does not exist in VCR Assistant.
109
+ class StripeAssistant
110
+ def initialize(vcr_cassette)
111
+ @vcr_cassette = vcr_cassette
112
+ end
113
+
114
+ def setup
115
+ Stripe::Customer.all.each &:delete
116
+ Stripe::Coupon.all.each &:delete
117
+ Stripe::Plan.all.each &:delete
118
+ end
119
+
120
+ def customer(email)
121
+ Stripe::Customer.create :email => email
122
+ end
123
+ end
124
+
125
+ # and then in a test:
126
+ it 'should make external HTTP calls' do |example|
127
+ assisted_cassette example do |assistant|
128
+ customer = assistant.customer 'pat@example.com'
129
+
130
+ # run tests that may involve the new customer object
131
+
132
+ # assert expectations
133
+ end
134
+ end
135
+ ```
136
+
137
+ ### Custom cassette file names
138
+
139
+ The default approach to naming cassettes is to take the spec's file name (minus the _spec.rb suffix) as a folder name, and the specific example's description as the file name (lowercased and with underscores instead of spaces). Thus, a cassette in testing_stripe_spec.rb within an example description `'creates subscriptions'` would be named `testing_stripe/creates_subscriptions.yml`.
140
+
141
+ If you'd like to have your own name generator, you can change `VCRAssistant.namer` to be a callable object that returns the appropriate file name as a string.
142
+
143
+ ```ruby
144
+ VCRAssistant.namer = lambda { |example|
145
+ # Just use the spec's file name. Useless if you have
146
+ # more than one cassette in any given file.
147
+ example.metadata[:file_path].gsub(/_spec\.rb$/, '')
148
+ }
149
+ ```
150
+
151
+ ## Development
152
+
153
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
154
+
155
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
156
+
157
+ ## Contributing
158
+
159
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/vcr_assistant. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
160
+
161
+ ## Licence
162
+
163
+ Copyright (c) 2016, VCR Assistant is developed and maintained by Pat Allan, and is released under the open MIT Licence.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "vcr_assistant"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ require 'vcr'
2
+
3
+ module VCRAssistant
4
+ def self.assistant
5
+ @assistant ||= VCRAssistant::Assistant
6
+ end
7
+
8
+ def self.assistant=(assistant)
9
+ @assistant = assistant
10
+ end
11
+
12
+ def self.namer
13
+ @namer ||= VCRAssistant::FileName
14
+ end
15
+
16
+ def self.namer=(namer)
17
+ @namer = namer
18
+ end
19
+ end
20
+
21
+ require 'vcr_assistant/assistant'
22
+ require 'vcr_assistant/cassette'
23
+ require 'vcr_assistant/file_name'
24
+ require 'vcr_assistant/test_helpers'
@@ -0,0 +1,11 @@
1
+ class VCRAssistant::Assistant
2
+ attr_reader :label, :vcr_cassette
3
+
4
+ def self.call(label, vcr_cassette)
5
+ new label, vcr_cassette
6
+ end
7
+
8
+ def initialize(label, vcr_cassette)
9
+ @label, @vcr_cassette = label, vcr_cassette
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ class VCRAssistant::Cassette
2
+ def self.call(example, label = :default, &block)
3
+ new(example, label, &block).call
4
+ end
5
+
6
+ def initialize(example, label, &block)
7
+ @example, @label, @block = example, label, block
8
+ end
9
+
10
+ def call
11
+ VCR.use_cassette(file_name) do |vcr_cassette|
12
+ assistant = VCRAssistant.assistant.call label, vcr_cassette
13
+
14
+ assistant.setup if assistant.respond_to?(:setup)
15
+ block.call assistant
16
+ assistant.teardown if assistant.respond_to?(:teardown)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :example, :label, :block
23
+
24
+ def file_name
25
+ VCRAssistant.namer.call example
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ class VCRAssistant::FileName
2
+ def self.call(example)
3
+ new(example).call
4
+ end
5
+
6
+ def initialize(example)
7
+ @example = example
8
+ end
9
+
10
+ def call
11
+ "#{folder}/#{file}"
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :example
17
+
18
+ def file
19
+ example.metadata[:description].downcase.gsub(/\s+/, '_').gsub(/[\W]+/, '')
20
+ end
21
+
22
+ def folder
23
+ File.basename example.metadata[:file_path], '_spec.rb'
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ require 'vcr_assistant'
2
+
3
+ RSpec.configure do |config|
4
+ config.include VCRAssistant::TestHelpers
5
+ end
@@ -0,0 +1,5 @@
1
+ module VCRAssistant::TestHelpers
2
+ def assisted_cassette(example, &block)
3
+ VCRAssistant::Cassette.call example, &block
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = "vcr_assistant"
4
+ spec.version = "0.1.0"
5
+ spec.authors = ["Pat Allan"]
6
+ spec.email = ["pat@freelancing-gods.com"]
7
+
8
+ spec.summary = %q{Manages VCR cassettes and set-up logic.}
9
+ spec.homepage = "https://github.com/pat/vcr_assistant"
10
+
11
+ spec.files = `git ls-files -z`.split("\x0").reject { |file|
12
+ file.match(%r{^(test|spec|features)/})
13
+ }
14
+ spec.bindir = "exe"
15
+ spec.executables = spec.files.grep(%r{^exe/}) { |file| File.basename(file) }
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_runtime_dependency "vcr", "~> 3.0"
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.11"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.0"
23
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vcr_assistant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pat Allan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: vcr
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - pat@freelancing-gods.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENCE
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/vcr_assistant.rb
86
+ - lib/vcr_assistant/assistant.rb
87
+ - lib/vcr_assistant/cassette.rb
88
+ - lib/vcr_assistant/file_name.rb
89
+ - lib/vcr_assistant/rspec.rb
90
+ - lib/vcr_assistant/test_helpers.rb
91
+ - vcr_assistant.gemspec
92
+ homepage: https://github.com/pat/vcr_assistant
93
+ licenses: []
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.8
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Manages VCR cassettes and set-up logic.
115
+ test_files: []