# betterment/webvalve architecture

> Architecture diagram of the GitHub repository betterment/webvalve, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/betterment/webvalve
- Repository: https://github.com/betterment/webvalve

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Application
    subgraph "Client Application" 
        direction TB
        Config["config/webvalve.rb"]:::userland
    end
    click Config "https://github.com/betterment/webvalve/blob/main/examples/sinatra/config/webvalve.rb"

    %% WebValve Library
    subgraph "WebValve Library"
        direction TB
        Core["lib/webvalve.rb"]:::core
        Manager["Manager"]:::core
        URLConv["ServiceURLConverter"]:::core
        FakeSvcBase["FakeService"]:::core
        FakeSvcCfg["FakeServiceConfig"]:::core
        FakeSvcWrap["FakeServiceWrapper"]:::core
        Instr["Instrumentation"]:::core
        Middleware["Middleware"]:::core
        LogSub["LogSubscriber"]:::core
        Railtie["Railtie"]:::core
        Patches["MonkeyPatches"]:::core
    end
    click Core "https://github.com/betterment/webvalve/blob/main/lib/webvalve.rb"
    click Manager "https://github.com/betterment/webvalve/blob/main/lib/webvalve/manager.rb"
    click URLConv "https://github.com/betterment/webvalve/blob/main/lib/webvalve/service_url_converter.rb"
    click FakeSvcBase "https://github.com/betterment/webvalve/blob/main/lib/webvalve/fake_service.rb"
    click FakeSvcCfg "https://github.com/betterment/webvalve/blob/main/lib/webvalve/fake_service_config.rb"
    click FakeSvcWrap "https://github.com/betterment/webvalve/blob/main/lib/webvalve/fake_service_wrapper.rb"
    click Instr "https://github.com/betterment/webvalve/blob/main/lib/webvalve/instrumentation.rb"
    click Middleware "https://github.com/betterment/webvalve/blob/main/lib/webvalve/instrumentation/middleware.rb"
    click LogSub "https://github.com/betterment/webvalve/blob/main/lib/webvalve/instrumentation/log_subscriber.rb"
    click Railtie "https://github.com/betterment/webvalve/blob/main/lib/webvalve/railtie.rb"
    click Patches "https://github.com/betterment/webvalve/blob/main/lib/webvalve/monkey_patches.rb"

    %% External Dependencies
    subgraph "External Dependencies"
        direction TB
        WebMock["WebMock"]:::external
        Sinatra["Sinatra"]:::external
        Addressable["Addressable"]:::external
    end

    %% Request Flow
    ClientAppHTTP["HTTP Request"]:::userland
    ExternalHTTP["External HTTP Services"]:::external

    %% Test Integration
    subgraph "RSpec Integration"
        direction TB
        RSpecInt["RSpec Integration"]:::test
        DummyApp["spec/dummy Rails App"]:::test
    end
    click RSpecInt "https://github.com/betterment/webvalve/blob/main/lib/webvalve/rspec.rb"
    click DummyApp "https://github.com/betterment/webvalve/blob/main/spec/dummy/config/application.rb"

    %% Generators & Examples
    subgraph "Generators & Examples"
        direction TB
        InstallGen["InstallGenerator"]:::userland
        FakeGen["FakeServiceGenerator"]:::userland
        ExApp["Sinatra Example App"]:::userland
        ExFake["Example FakeService"]:::userland
    end
    click InstallGen "https://github.com/betterment/webvalve/blob/main/lib/generators/webvalve/install_generator.rb"
    click FakeGen "https://github.com/betterment/webvalve/blob/main/lib/generators/webvalve/fake_service_generator.rb"
    click ExApp "https://github.com/betterment/webvalve/blob/main/examples/sinatra/app.rb"
    click ExFake "https://github.com/betterment/webvalve/blob/main/examples/sinatra/webvalve/fake_twitter.rb"

    %% Boot Flow
    Config -->|loaded by| Railtie
    Railtie -->|boot hooks| Manager
    Manager -->|registers services| FakeSvcWrap
    Manager -->|uses| URLConv
    Manager -->|setup intercept| WebMock
    Instr -->|hooks into| WebMock
    Instr -->|registers| Middleware
    Instr -->|registers| LogSub
    Patches -->|override calls| WebMock

    %% Request Flow arrows
    ClientAppHTTP -->|intercept| WebMock
    WebMock -->|match URL| URLConv
    URLConv -->|if fake| FakeSvcBase
    FakeSvcBase -->|responds| ClientAppHTTP
    URLConv -->|if real| ExternalHTTP
    ExternalHTTP -->|response| ClientAppHTTP

    %% Test Integration Flow
    RSpecInt -->|setup/reset| Manager
    DummyApp -->|integration tests| RSpecInt

    %% Generators & Examples Flow
    InstallGen --> Config
    FakeGen --> ExFake
    ExApp --> ExFake

    %% Connections with core entry
    Config -->|requires| Core

    %% Class Definitions
    classDef core fill:#a8f0a1,stroke:#333,stroke-width:1px
    classDef external fill:#a1c8f0,stroke:#333,stroke-width:1px
    classDef userland fill:#f0c18a,stroke:#333,stroke-width:1px
    classDef test fill:#d3d3d3,stroke:#333,stroke-width:1px
```
