Skip to content

Commit f503254

Browse files
authored
Remove direct dependency on Rails, keep ActiveSupport (#19)
* starting point, stubbing out the things we're borrowing from Rails * everything working except no-rails * make it work without rails but with activesupport * clean up * added some notes to the readme * missing docs * ensure top-level * switch to railtie * it actually loads rails * remove silly env vars * restore version constraint for activesupport * update readme * cleaner rails OR not-rails impl * add sinatra example
1 parent 8bf66b0 commit f503254

30 files changed

+126
-62
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ gemfile:
77
- gemfiles/rails_4_2.gemfile
88
- gemfiles/rails_5_0.gemfile
99
- gemfiles/rails_5_1.gemfile
10+
- gemfiles/ruby.gemfile
1011
before_install:
1112
- gem update --system
1213
- gem update bundler

Appraisals

+11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
appraise 'rails-4-2' do
22
gem 'rails', '~> 4.2.0'
3+
gem 'jdbc-sqlite3', platforms: :jruby
34
gem 'activerecord-jdbcsqlite3-adapter', '~> 1.3.24', platforms: :jruby
5+
gem 'sqlite3', '~> 1.3.10', platforms: :ruby
46
end
57

68
appraise 'rails-5-0' do
79
gem 'rails', '~> 5.0.0'
10+
gem 'jdbc-sqlite3', platforms: :jruby
11+
gem 'activerecord-jdbcsqlite3-adapter', platforms: :jruby
12+
gem 'sqlite3', '~> 1.3.10', platforms: :ruby
813
end
914

1015
appraise 'rails-5-1' do
1116
gem 'rails', '~> 5.1.0'
17+
gem 'jdbc-sqlite3', platforms: :jruby
18+
gem 'activerecord-jdbcsqlite3-adapter', platforms: :jruby
19+
gem 'sqlite3', '~> 1.3.10', platforms: :ruby
20+
end
21+
22+
appraise 'ruby' do
1223
end

Gemfile

-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,3 @@ source 'https://rubygems.org'
22
git_source(:github) { |repo_name| "https://github.com/#{repo_name}.git" }
33

44
gemspec
5-
6-
gem 'jdbc-sqlite3', platforms: :jruby
7-
gem 'activerecord-jdbcsqlite3-adapter', platforms: :jruby
8-
gem 'sqlite3', '~> 1.3.10', platforms: :ruby

README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Check out [the Rails at Scale talk](https://www.youtube.com/watch?v=Nd9hnffxCP8)
1818

1919
## Getting Started
2020

21-
WebValve is designed to work with Rails 4+.
21+
WebValve is designed to work with Rails 4+, but it also should work with
22+
non-Rails apps and gems.
2223

2324
### Installation
2425

@@ -75,6 +76,8 @@ This will drop a new file in your config directory.
7576
# WebValve.whitelist_url 'https://example.com'
7677
```
7778

79+
If you're not using Rails, you can create this file for yourself.
80+
7881
### Registering a service
7982

8083
Next, you will want create a `FakeService` and register
@@ -113,6 +116,9 @@ And it will automatically register it in `config/webvalve.rb`
113116
WebValve.register FakeBank
114117
```
115118

119+
Again, if you're not using Rails, you'll have to create this file
120+
yourself and update the config file manually.
121+
116122
You'll also want to define an environment variable for the base url of
117123
your service.
118124

@@ -141,7 +147,7 @@ have to configure WebValve at the beginning of each test. For RSpec, there
141147
is a configuration provided.
142148

143149
```ruby
144-
# spec/rails_helper.rb
150+
# spec/spec_helper.rb
145151
require 'webvalve/rspec'
146152
```
147153

@@ -202,6 +208,13 @@ environments; however, it can be enabled in other environments by
202208
setting `WEBVALVE_ENABLED=true`. This can be useful for spinning up
203209
cheap, one-off environments for user-testing or demos.
204210

211+
> Can I use WebValve without Rails?
212+
213+
Yep! If you're not using Rails, you'll have to load the config file
214+
yourself. You will want to explicitly `require` each of your fake
215+
services in your `config/webvalve.rb`, `require` your config file, and
216+
call `WebValve.setup` during your app's boot-up process.
217+
205218
## How to Contribute
206219

207220
We would love for you to contribute! Anything that benefits the majority

Rakefile

+10-17
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,18 @@ rescue LoadError
44
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
55
end
66

7-
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
8-
load 'rails/tasks/engine.rake'
9-
107
Bundler::GemHelper.install_tasks
118

12-
if Rails.env.development? || Rails.env.test?
13-
if defined? Dummy
14-
task(:default).clear
15-
if ENV['APPRAISAL_INITIALIZED'] || ENV['TRAVIS']
16-
require 'rspec/core'
17-
require 'rspec/core/rake_task'
18-
RSpec::Core::RakeTask.new(:spec)
19-
task default: :spec
20-
else
21-
require 'appraisal'
22-
Appraisal::Task.new
23-
task default: :appraisal
24-
end
25-
end
9+
task(:default).clear
10+
if ENV['APPRAISAL_INITIALIZED'] || ENV['TRAVIS']
11+
require 'rspec/core'
12+
require 'rspec/core/rake_task'
13+
RSpec::Core::RakeTask.new(:spec)
14+
task default: :spec
15+
else
16+
require 'appraisal'
17+
Appraisal::Task.new
18+
task default: :appraisal
2619
end
2720

2821
require 'yard'

examples/sinatra/Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'http://rubygems.org'
2+
3+
gem 'sinatra'
4+
gem 'webvalve', path: '../../'

examples/sinatra/app.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'sinatra'
2+
require 'net/http'
3+
require './config/webvalve'
4+
WebValve.setup
5+
6+
get '/' do
7+
Net::HTTP.get('faketwitter.test', '/')
8+
end

examples/sinatra/config.ru

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require './app'
2+
3+
run Sinatra::Application

examples/sinatra/config/webvalve.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'webvalve'
2+
require_relative '../webvalve/fake_twitter'
3+
4+
WebValve.register FakeTwitter, url: FakeTwitter::URL
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class FakeTwitter < WebValve::FakeService
2+
URL = 'http://faketwitter.test'.freeze
3+
4+
get '/' do
5+
json hello: 'world'
6+
end
7+
end

gemfiles/rails_4_2.gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
source "https://rubygems.org"
44

5+
gem "rails", "~> 4.2.0"
56
gem "jdbc-sqlite3", platforms: :jruby
67
gem "activerecord-jdbcsqlite3-adapter", "~> 1.3.24", platforms: :jruby
78
gem "sqlite3", "~> 1.3.10", platforms: :ruby
8-
gem "rails", "~> 4.2.0"
99

1010
gemspec path: "../"

gemfiles/rails_5_0.gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
source "https://rubygems.org"
44

5+
gem "rails", "~> 5.0.0"
56
gem "jdbc-sqlite3", platforms: :jruby
67
gem "activerecord-jdbcsqlite3-adapter", platforms: :jruby
78
gem "sqlite3", "~> 1.3.10", platforms: :ruby
8-
gem "rails", "~> 5.0.0"
99

1010
gemspec path: "../"

gemfiles/rails_5_1.gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
source "https://rubygems.org"
44

5+
gem "rails", "~> 5.1.0"
56
gem "jdbc-sqlite3", platforms: :jruby
67
gem "activerecord-jdbcsqlite3-adapter", platforms: :jruby
78
gem "sqlite3", "~> 1.3.10", platforms: :ruby
8-
gem "rails", "~> 5.1.0"
99

1010
gemspec path: "../"

gemfiles/ruby.gemfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This file was generated by Appraisal
2+
3+
source "https://rubygems.org"
4+
5+
gemspec path: "../"

lib/tasks/webvalve_tasks.rake

Whitespace-only changes.

lib/webvalve.rb

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
require 'set'
2+
require 'active_support'
3+
require 'active_support/core_ext'
24

35
module WebValve
46
ALWAYS_ENABLED_ENVS = %w(development test).freeze
@@ -16,9 +18,9 @@ class << self
1618
delegate :setup, :register, :whitelist_url, :reset, to: :manager
1719

1820
def enabled?
19-
if Rails.env.in?(ALWAYS_ENABLED_ENVS)
21+
if env.in?(ALWAYS_ENABLED_ENVS)
2022
if ENV.key? 'WEBVALVE_ENABLED'
21-
Rails.logger.warn(<<~MESSAGE)
23+
logger.warn(<<~MESSAGE)
2224
WARNING: Ignoring WEBVALVE_ENABLED environment variable setting (#{ENV['WEBVALVE_ENABLED']})
2325
WebValve is always enabled in development and test environments.
2426
MESSAGE
@@ -33,16 +35,36 @@ def config_paths
3335
@config_paths ||= Set.new
3436
end
3537

36-
private
38+
if defined?(::Rails)
39+
delegate :env, :env=, :logger, :logger=, to: ::Rails
40+
else
41+
def env
42+
@env ||= (ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development').inquiry
43+
end
44+
45+
def env=(env)
46+
@env = env&.inquiry
47+
end
48+
49+
def logger
50+
@logger ||= ActiveSupport::Logger.new(STDOUT).tap do |l|
51+
l.formatter = ::Logger::Formatter.new
52+
end
53+
end
54+
55+
def logger=(logger)
56+
@logger = logger
57+
end
58+
end
3759

3860
def manager
3961
WebValve::Manager.instance
4062
end
4163
end
4264
end
4365

66+
require 'webvalve/railtie' if defined?(::Rails)
4467
require 'webvalve/instrumentation'
45-
require 'webvalve/engine'
4668
require 'webvalve/fake_service'
4769
require 'webvalve/fake_service_wrapper'
4870
require 'webvalve/fake_service_config'

lib/webvalve/fake_service_config.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def initialize(service:, url: nil)
88
end
99

1010
def should_intercept?
11-
Rails.env.test? || # always intercept in test
11+
WebValve.env.test? || # always intercept in test
1212
(WebValve.enabled? && !service_enabled_in_env?)
1313
end
1414

lib/webvalve/instrumentation/log_subscriber.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'active_support/log_subscriber'
2+
13
module WebValve
24
module Instrumentation
35
class LogSubscriber < ActiveSupport::LogSubscriber

lib/webvalve/instrumentation/middleware.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'active_support/notifications'
2+
13
module WebValve
24
module Instrumentation
35
class Middleware

lib/webvalve/manager.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def whitelisted_urls
5050

5151
def webmock_disable_options
5252
{ allow_localhost: true }.tap do |opts|
53-
opts[:allow] = whitelisted_url_regexps unless Rails.env.test?
53+
opts[:allow] = whitelisted_url_regexps unless WebValve.env.test?
5454
end
5555
end
5656

lib/webvalve/engine.rb lib/webvalve/railtie.rb

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
module WebValve
2-
class Engine < ::Rails::Engine
3-
isolate_namespace WebValve
4-
2+
class Railtie < ::Rails::Railtie
53
if WebValve.enabled?
6-
initializer "webvalve.set_autoload_paths", before: :set_autoload_paths do |app|
4+
initializer 'webvalve.set_autoload_paths', before: :set_autoload_paths do |app|
75
WebValve.config_paths << app.root
86

97
WebValve.config_paths.each do |root|
108
app.config.eager_load_paths << root.join('webvalve').to_s
119
end
1210
end
1311

14-
initializer "webvalve.setup" do
12+
initializer 'webvalve.setup' do
1513
WebValve.config_paths.each do |root|
1614
path = root.join('config', 'webvalve.rb').to_s
1715
load path if File.exist?(path)

spec/dummy/config/routes.rb

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
Rails.application.routes.draw do
2-
mount WebValve::Engine => "/webvalve"
32
end

spec/rails_helper.rb

-14
This file was deleted.

spec/spec_helper.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
ENV['RAILS_ENV'] ||= 'test'
2+
require File.expand_path('dummy/config/environment', __dir__) if ENV['BUNDLE_GEMFILE'] =~ /rails/
3+
require 'rspec'
4+
require 'pry'
5+
require 'webvalve'
6+
7+
Dir[File.join(__dir__, 'support/**/*.rb')].each { |f| require f }
8+
19
RSpec.configure do |config|
210
config.expect_with :rspec do |expectations|
311
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
@@ -18,5 +26,7 @@
1826
config.order = :random
1927
Kernel.srand config.seed
2028

21-
# config.profile_examples = 10
29+
config.profile_examples = 10
30+
31+
config.include Helpers
2232
end

spec/support/helpers.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ def with_env(opts = {})
1515
end
1616

1717
def with_rails_env(env)
18-
initial_env = Rails.env
19-
Rails.env = env
18+
initial_env = WebValve.env
19+
WebValve.env = env
2020
yield
2121
ensure
22-
Rails.env = initial_env
22+
WebValve.env = initial_env
2323
end
2424
end

spec/webvalve/fake_service_config_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'rails_helper'
1+
require 'spec_helper'
22

33
RSpec.describe WebValve::FakeServiceConfig do
44
let(:fake_service) do

spec/webvalve/fake_service_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'rails_helper'
1+
require 'spec_helper'
22

33
RSpec.describe WebValve::FakeService do
44
subject do

spec/webvalve/manager_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'rails_helper'
1+
require 'spec_helper'
22

33
RSpec.describe WebValve::Manager do
44

spec/webvalve_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'rails_helper'
1+
require 'spec_helper'
22

33
RSpec.describe WebValve do
44
it 'delegates .setup to manager' do

0 commit comments

Comments
 (0)