# prashu474/weather_app architecture

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

- Interactive diagram: https://gitdiagram.com/prashu474/weather_app
- Repository: https://github.com/prashu474/weather_app

## Overview

##### Main Components
- Client/UI: Represented by the web views (HTML pages) within app/views. This is where the user interacts with the application (e.g., the weather search page).
- Controller Layer: The WeatherController (and ApplicationController) in app/controllers handles user input and orchestrates the response.
- Service Layer: The WeatherService in app/services encapsulates the logic for integrating with WeatherAPI and managing caching logic.
- Data Storage:
- WeatherCache Model (app/models/weather_cache.rb): Handles caching details including API responses and expiry times.
- PostgreSQL Database: As configured in config/database.yml, it’s used to persist weather cache data.
- (Optional) Redis: Mentioned in the README as an in-memory caching alternative.
- External Service: WeatherAPI – the third-party API that provides the weather forecast data.

##### Relationships and Data Flow
- The client makes a request (e.g., entering a zip code on the search page).
- The WeatherController receives the request and passes the zip code to the WeatherService.
- WeatherService first checks if up-to-date data exists (via WeatherCache in the database or optionally Redis if implemented).
- If the cache is valid, it returns the stored data.
- If not, it makes an API call to WeatherAPI, processes the response, saves it in WeatherCache, and returns the new data.
- The Controller then renders the appropriate view indicating whether the data was cached or freshly fetched.

##### Architectural Patterns
- MVC (Model-View-Controller): Clearly separating concerns – user interface (views), application logic (controller/service), and data storage (models).
- Service-Oriented: WeatherService encapsulates the logic for external API interactions and caching, demonstrating single responsibility and encapsulation.
- Caching Strategy: Caches weather data to optimize performance, with a 30-minute expiry window as managed by the WeatherCache model (and optionally Redis).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client/UI"
        WS("Weather Search Page"):::frontend
        WShow("Weather Show Page"):::frontend
    end

    subgraph "Controller Layer"
        WC("WeatherController: Manages user requests"):::controller
    end

    subgraph "Service Layer"
        WService("WeatherService: API integration & caching"):::service
    end

    subgraph "Data Storage"
        WCach("WeatherCache (ActiveRecord Model)"):::model
        DB("[Database: PostgreSQL]"):::database
    end

    subgraph "External Services"
        WAPI("WeatherAPI"):::external
        R("Cache (Redis)-Optional"):::external
    end

    WS -->|"submitZipCode"| WC
    WC -->|"invokeService"| WService
    WService -->|"checkAndStoreCache"| WCach
    WCach -->|"persistData"| DB
    WService -->|"fetchData"| WAPI
    WService -->|"optionalCache"| R
    WService -->|"returnData"| WC
    WC -->|"renderView"| WShow
    WAPI -.-|"error"| WC

    click WS "https://github.com/prashu474/weather_app/blob/main/app/views/weather/search.html.erb"
    click WShow "https://github.com/prashu474/weather_app/blob/main/app/views/weather/show.html.erb"
    click WC "https://github.com/prashu474/weather_app/blob/main/app/controllers/weather_controller.rb"
    click WService "https://github.com/prashu474/weather_app/blob/main/app/services/weather_service.rb"
    click WCach "https://github.com/prashu474/weather_app/blob/main/app/models/weather_cache.rb"
    click DB "https://github.com/prashu474/weather_app/blob/main/config/database.yml"

    classDef frontend fill:#FFEEAD,stroke:#333,stroke-width:2px;
    classDef controller fill:#AEDFF7,stroke:#333,stroke-width:2px;
    classDef service fill:#C1F0C1,stroke:#333,stroke-width:2px;
    classDef model fill:#F8D7DA,stroke:#333,stroke-width:2px;
    classDef database fill:#D1E8E2,stroke:#333,stroke-width:2px;
    classDef external fill:#E2D0F5,stroke:#333,stroke-width:2px;
```
