How betterment/webvalve works
Project Type and Purpose
• WebValve is a Ruby library/gem (open-source tool) for defining and toggling fake HTTP services in non-production environments.
• It plugs into Rails (via Railtie) or any Ruby app, uses WebMock to intercept real HTTP calls, and hosts Sinatra-based fakes.
Main Components
a. Client Application
• Rails app (or non-Rails Ruby app) – this is where WebValve is installed.
• config/webvalve.rb – user-defined file registering services & allowlist.
b. Core WebValve Library (lib/webvalve)
• Manager – orchestrates registration, toggling, setup/reset.
• FakeService – Sinatra::Base subclass where users define fake routes.
• ServiceURLConverter – maps requested URLs to fake or real endpoints.
• Instrumentation
– Middleware & LogSubscriber: hooks into WebMock/Net::HTTP.
• MonkeyPatches & Railtie – integrate with Rails and override network calls.
c. External Dependencies
• WebMock – intercepts HTTP requests.
• Sinatra – drives fake service endpoints.
• Addressable (for dynamic URL templates).
d. Test Integration
• RSpec support (lib/webvalve/rspec.rb)
• spec/dummy Rails app – integration tests.
e. Generators & CLI
• InstallGenerator & FakeServiceGenerator – populate config and scaffold services.
• bin/yard, bin/rspec, bin/rails etc. – development tooling.
Relationships & Data Flow
• At boot: Railtie loads config/webvalve.rb → Manager.registers services → Manager invokes WebMock to disable real HTTP (by default) → Sinatra apps for each FakeService are mounted (internally).
• During request: Client app issues HTTP call → WebMock intercepts → ServiceURLConverter checks if URL matches a registered fake service (and whether it’s enabled) →
• If fake: route is forwarded to the corresponding Sinatra app (FakeService) → returns JSON response.
• If real: WebMock allows passthrough (if service enabled) or blocks (if disabled).
• In tests: lib/webvalve/rspec ensures WebValve.setup + WebMock hooks run before each example, and WebValve.reset! after.
Architectural Patterns & Principles
• Inversion of Control via Railtie and monkey patches to intercept network calls.
• Pluggable middleware (Rack/Sinatra) for fakes.
• Single Responsibility: Manager handles registration/toggle, ServiceURLConverter handles URL matching, FakeService handles route logic.
• Extensibility: Generators scaffold new services; config allows wildcards, templates, regex.
Essential Elements
• Manager
• ServiceURLConverter
• FakeService (Sinatra)
• Instrumentation (Middleware, LogSubscriber)
• Railtie / Monkey Patches
• Annotations for Generators (install, fake_service) and Examples folder.