Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ axum = { version = "0.7.3", features = ["macros", "query", "tokio"] }
axum-macros = "0.4.0"
axum-template = { version = "2.1.0", features = ["handlebars"] }
base64 = "0.21"
chrono = { version = "0.4", features = ["serde"] }
chrono = { version = "0.4", features = ["clock", "serde"] }
handlebars = { version = "5.0.0", features = ["dir_source"] }
http = "1"
indoc = "2"
Expand All @@ -41,6 +41,7 @@ uuid = { version = "1", features = ["serde", "v4"] }
url = { version = "2", features = ["serde"] }

[dev-dependencies]
assertables = "9.5.5"
select = "0.6"
speculoos = "0.11"
wiremock = "0.5"
40 changes: 25 additions & 15 deletions src/commands/stops.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use chrono::Local;

use serde::Deserialize;
use serde_json::Value;
use serde_json::{Number, Value};
use sqlx::{types::Uuid, PgPool};

use crate::{commands::StopsCommand, config::Config, odws::fetch_from_odws};
Expand All @@ -14,7 +16,12 @@ pub async fn handle_stops_request(
maybe_incoming_message_id: Option<Uuid>,
db: &PgPool,
) -> Result<String, Box<dyn std::error::Error>> {
let locations_query = format!("/v3/locations:{}.json?usage=short", command.location);
let effective_on_string = Local::now().format("%Y-%m-%d").to_string();

let locations_query = format!(
"/v4/locations:{}.json?usage=short&effective-on={}",
command.location, effective_on_string
);
log::trace!("locations URL: {}", locations_query);

let (_locations_response_status, locations_response_text) = fetch_from_odws(
Expand All @@ -33,8 +40,8 @@ pub async fn handle_stops_request(
};

let stops_query = format!(
"/v3/stops.json?lat={}&lon={}&distance={}&usage=short",
latitude, longitude, STOPS_DISTANCE
"/v4/stops.json?lat={}&lon={}&distance={}&usage=short&effective-on={}",
latitude, longitude, STOPS_DISTANCE, effective_on_string
);

log::trace!("stops URL: {}", stops_query);
Expand Down Expand Up @@ -68,7 +75,10 @@ pub async fn handle_stops_request(
let mut response = format!("Stops near {}\n", location_name);

for stop in stops_response.stops.iter().take(MAXIMUM_STOPS_TO_RETURN) {
let routes_query = format!("/v3/routes.json?stop={}", stop.number);
let routes_query = format!(
"/v4/routes.json?stop={}&effective-on={}",
stop.number, effective_on_string
);

log::trace!("routes URL: {}", routes_query);

Expand Down Expand Up @@ -131,7 +141,7 @@ pub async fn handle_stops_request(

fn extract_location_details(
locations_response_text: &str,
) -> Result<(String, String, String), Box<dyn std::error::Error>> {
) -> Result<(String, Number, Number), Box<dyn std::error::Error>> {
let location_name;
let latitude;
let longitude;
Expand Down Expand Up @@ -194,10 +204,10 @@ mod tests {
let (location_name, latitude, longitude) = result.unwrap();
assert_eq!(
location_name,
"Via Rail Station (Union Station) (123 Main Street)"
"Via Rail Station (Union Station) (123 MainSt)"
);
assert_eq!(latitude, "49.88895");
assert_eq!(longitude, "-97.13424");
assert_eq!(latitude.to_string(), "49.88895");
assert_eq!(longitude.to_string(), "-97.13424");
}

#[test]
Expand All @@ -210,8 +220,8 @@ mod tests {

let (location_name, latitude, longitude) = result.unwrap();
assert_eq!(location_name, "245 SmithSt");
assert_eq!(latitude, "49.89218");
assert_eq!(longitude, "-97.14084");
assert_eq!(latitude.to_string(), "49.89218");
assert_eq!(longitude.to_string(), "-97.14084");
}

#[test]
Expand All @@ -224,8 +234,8 @@ mod tests {

let (location_name, latitude, longitude) = result.unwrap();
assert_eq!(location_name, "PortageAve@MainSt");
assert_eq!(latitude, "49.89553");
assert_eq!(longitude, "-97.13848");
assert_eq!(latitude.to_string(), "49.89553");
assert_eq!(longitude.to_string(), "-97.13848");
}
}

Expand All @@ -249,8 +259,8 @@ struct Centre {

#[derive(Deserialize)]
struct Geographic {
latitude: String,
longitude: String,
latitude: Number,
longitude: Number,
}

#[derive(Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/commands/times.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub async fn handle_times_request(
number: &Option<Number>,
) -> Result<String, Box<dyn std::error::Error>> {
let query = format!(
"/v3/stops/{}/schedule.json?usage=short",
"/v4/stops/{}/schedule.json?usage=short",
command.stop_number,
);

Expand Down
12 changes: 12 additions & 0 deletions templates/changelog.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
changelog
</h2>

<h3>
2025-06-08
</h3>
<h4>
chore
</h4>
<ul>
<li>
updated to consume Winnipeg Transit’s <a href="https://api.winnipegtransit.com/home/api/v4">v4 API</a>
</li>
</ul>

<h3>
2024-02-16
</h3>
Expand Down
28 changes: 28 additions & 0 deletions tests/fixtures/fetch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

BASE_URL="https://api.winnipegtransit.com/v4"
DATE_PARAM=$(date +%Y-%m-%d)

fetch_and_format_api_response() {
local endpoint=$1
local output_file=$2
local extra_query_parameters=${3:-""}

curl "${BASE_URL}/${endpoint}?api-key=${API_KEY}&usage=short&effective-on=${DATE_PARAM}${extra_query_parameters}" | jq "." > "${output_file}"
}

fetch_and_format_api_response "locations:245%20smith.json" "stops/locations-address.json"
fetch_and_format_api_response "locations:portage%20%40%20main.json" "stops/locations-intersection.json"
fetch_and_format_api_response "locations:assiniboia%20downs.json" "stops/locations-no-stops.json"
fetch_and_format_api_response "locations:jortleby.json" "stops/locations-none.json"
fetch_and_format_api_response "locations:union%20station.json" "stops/locations.json"

fetch_and_format_api_response "stops.json" "stops/stops.json" "&x=634017&y=5527953&distance=500"
fetch_and_format_api_response "stops.json" "stops/stops-none.json" "&x=734017&y=5527953&distance=500"

STOP_NUMBERS=(10625 10641 11052 11010 10642 10901 10902 10624 10830 10590 10907 10639 10939 10589 11051 10651 11024 10620 10675 10803 10804 10591 10158 10588 10672 10652 10157)

for STOP_NUMBER in "${STOP_NUMBERS[@]}"
do
fetch_and_format_api_response "routes.json" "stops/routes/stop_${STOP_NUMBER}.json" "&stop=${STOP_NUMBER}"
done
8 changes: 0 additions & 8 deletions tests/fixtures/stops/fetch.sh

This file was deleted.

10 changes: 5 additions & 5 deletions tests/fixtures/stops/locations-address.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"y": 5528300
},
"geographic": {
"latitude": "49.89218",
"longitude": "-97.14084"
"latitude": 49.89218,
"longitude": -97.14084
}
},
"type": "address"
Expand All @@ -36,12 +36,12 @@
"y": 5532745
},
"geographic": {
"latitude": "49.9319",
"longitude": "-97.12439"
"latitude": 49.9319,
"longitude": -97.12439
}
},
"type": "address"
}
],
"query-time": "2024-01-12T22:43:47"
"query-time": "2025-06-03T19:55:07"
}
19 changes: 9 additions & 10 deletions tests/fixtures/stops/locations-intersection.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
"centre": {
"utm": {
"zone": "14U",
"x": "633694.71",
"y": "5528676.7"
"x": 633694.71,
"y": 5528676.7
},
"geographic": {
"latitude": "49.89553",
"longitude": "-97.13848"
"latitude": 49.89553,
"longitude": -97.13848
}
},
"type": "intersection"
Expand All @@ -41,17 +41,16 @@
"centre": {
"utm": {
"zone": "14U",
"x": "633694.71",
"y": "5528676.7"
"x": 633694.71,
"y": 5528676.7
},
"geographic": {
"latitude": "49.89553",
"longitude": "-97.13848"
"latitude": 49.89553,
"longitude": -97.13848
}
},
"type": "intersection"
}
],

"query-time": "2024-01-12T22:49:03"
"query-time": "2025-06-03T19:55:09"
}
10 changes: 5 additions & 5 deletions tests/fixtures/stops/locations-no-stops.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"key": 11556,
"name": "Assiniboine Downs",
"categories": [
"Entertainment: Tourist Attractions",
"Sports and Recreation: Race Tracks"
"Sports and Recreation: Race Tracks",
"Entertainment: Tourist Attractions"
],
"address": {
"key": 78758,
Expand All @@ -22,13 +22,13 @@
"y": 5527870
},
"geographic": {
"latitude": "49.89117",
"longitude": "-97.32834"
"latitude": 49.89117,
"longitude": -97.32834
}
}
},
"type": "monument"
}
],
"query-time": "2024-01-13T10:27:40"
"query-time": "2025-06-03T19:55:11"
}
2 changes: 1 addition & 1 deletion tests/fixtures/stops/locations-none.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"locations": [],
"query-time": "2024-01-13T10:14:31"
"query-time": "2025-06-03T19:55:12"
}
12 changes: 7 additions & 5 deletions tests/fixtures/stops/locations.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
{
"key": 2140,
"name": "Via Rail Station (Union Station)",
"categories": ["Transportation"],
"categories": [
"Transportation"
],
"address": {
"key": 133533,
"street-number": 123,
"street": {
"key": 2265,
"name": "Main Street",
"name": "MainSt",
"type": "Street"
},
"centre": {
Expand All @@ -19,13 +21,13 @@
"y": 5527953
},
"geographic": {
"latitude": "49.88895",
"longitude": "-97.13424"
"latitude": 49.88895,
"longitude": -97.13424
}
}
},
"type": "monument"
}
],
"query-time": "2024-01-11T19:58:07"
"query-time": "2025-06-03T19:55:13"
}
Loading
Loading