Skip to content

Commit

Permalink
Added unit tests for the API & GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
YarosMallorca committed May 24, 2024
1 parent 31e886d commit 3999d75
Show file tree
Hide file tree
Showing 13 changed files with 845 additions and 33 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/dart_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Dart Test and Analysis

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: dart-lang/setup-dart@v1

- name: Format code
run: dart format --set-exit-if-changed .

- name: Get dependencies
run: dart pub get

- name: Run tests
run: dart test

- name: Analyze code
run: dart analyze
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 0.5.7

- Added unit tests for the API

## 0.5.6

- Added Connection Close message type to realtime websocket API
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# TIB API
<img src="https://github.com/YarosMallorca/tib_api/assets/54041533/55ff2f26-4ec9-40dd-8445-f0936324f9af" height="80px" />

<img src="https://github.com/YarosMallorca/tib_api/assets/54041533/55ff2f26-4ec9-40dd-8445-f0936324f9af" alt="TIB Logo" height="80px" />

## Features

Expand All @@ -23,7 +24,7 @@ Install the package by adding it to your `pubspec.yaml` file:

```yaml
dependencies:
tib_api: ^0.5.6
tib_api: ^0.5.7
```
## Usage
Expand Down Expand Up @@ -107,8 +108,8 @@ await RouteLine.getPdfTimetable('A42');
Full example can be found in the [example.dart](example/tib_api_example.dart)

## Facing Issues?

TIB's website is pretty unstable, and changes can occur.
I'm trying my best to make this API as bug-free as possible, which can't be said about their website.

<img src="https://github.com/YarosMallorca/tib_api/assets/54041533/f6def031-bf38-4f7d-b2e3-b719e5e12dbe" height="300px" />

43 changes: 22 additions & 21 deletions lib/src/api/departures.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:typed_data';
import 'package:http/http.dart';

class Departures {
static Client httpClient = Client();
List<Departure>? departures;

Departures({this.departures});
Expand All @@ -21,27 +22,27 @@ class Departures {
{required int stationCode, required int numberOfDepartures}) async {
Uri url = Uri.parse(
'http://tib.org/o/manager/stop-code/$stationCode/departures/ctmr4?res=$numberOfDepartures');
// try {
Uint8List responseBytes = await get(url).then((value) => value.bodyBytes);
List<Departure> departures = [];
for (var response in json.decode(utf8.decode(responseBytes))) {
Departure responseDeparture = Departure.fromJson(response);
departures.add(responseDeparture);
}
try {
Uint8List responseBytes =
await httpClient.get(url).then((value) => value.bodyBytes);
List<Departure> departures = [];
for (var response in json.decode(utf8.decode(responseBytes))) {
Departure responseDeparture = Departure.fromJson(response);
departures.add(responseDeparture);
}

if (departures.isEmpty) {
if (departures.isEmpty) {
throw Exception(
"No departures found. 😕 Please check that the station code is correct.");
} else {
return departures;
}
} on FormatException {
throw FormatException("The station code is invalid. 😶");
} catch (e) {
throw Exception(
"No departures found. 😕 Please check that the station code is correct.");
} else {
return departures;
"There was an error fetching the departures. 😕 Please try again later.");
}
// } on FormatException {
// throw FormatException("The station code is invalid. 😶");
// } catch (e) {
// print(e);
// throw Exception(
// "There was an error fetching the departures. 😕 Please try again later.");
// }
}
}

Expand Down Expand Up @@ -82,7 +83,7 @@ class RealTrip {
/// Converts a [RealTrip] object to a JSON map.
static String toJson(RealTrip realTrip) {
return jsonEncode({
'aet': realTrip.estimatedArrival.toString(),
'aet': realTrip.estimatedArrival?.toIso8601String(),
'lastCoords': {'lat': realTrip.lat, 'lng': realTrip.long},
'id': realTrip.id
});
Expand Down Expand Up @@ -143,8 +144,8 @@ class Departure {
/// returns a map representing the JSON data.
static Map toJson(Departure departure) {
return {
'dt': departure.departureTime.toString(),
'aet': departure.estimatedArrival.toString(),
'dt': departure.departureTime.toIso8601String(),
'aet': departure.estimatedArrival.toIso8601String(),
'snam': departure.name,
'trip_id': departure.tripId,
'realTrip': departure.realTrip != null
Expand Down
20 changes: 14 additions & 6 deletions lib/src/api/route_line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ class RouteLine {
LineType type;
List<Subline>? sublines;

static Client httpClient = Client();

static Future<List<RouteLine>> getAllLines() async {
Uri url = Uri.parse("https://ws.tib.org/sictmws-rest/lines/ctmr4");
try {
Uint8List responseBytes = await get(url).then((value) => value.bodyBytes);
Uint8List responseBytes =
await httpClient.get(url).then((value) => value.bodyBytes);

List<RouteLine> lines = [];
for (Map line in json.decode(utf8.decode(responseBytes))["linesInfo"]) {
Expand All @@ -42,7 +45,8 @@ class RouteLine {
Uri url =
Uri.parse("https://ws.tib.org/sictmws-rest/lines/ctmr4/$lineCode");
try {
Uint8List responseBytes = await get(url).then((value) => value.bodyBytes);
Uint8List responseBytes =
await httpClient.get(url).then((value) => value.bodyBytes);
return RouteLine.fromJson(json.decode(utf8.decode(responseBytes)));
} on FormatException {
throw FormatException("Something went wrong. 😶");
Expand All @@ -51,10 +55,11 @@ class RouteLine {

static Future<Uri?> getPdfTimetable(String lineCode) async {
try {
final parser = await Chaleno().load(
"https://www.tib.org/es/lineas-y-horarios/autobus/-/linia/$lineCode");
final response = await httpClient.get(Uri.parse(
"https://www.tib.org/es/lineas-y-horarios/autobus/-/linia/$lineCode"));
final parser = Parser(response.body);
Result div =
parser!.getElementsByClassName('ctm-line-schedule-link').first;
parser.getElementsByClassName('ctm-line-schedule-link').first;
String? href = div.querySelector('a')!.href;
return href != null ? Uri.parse(href) : null;
} catch (e) {
Expand Down Expand Up @@ -225,6 +230,8 @@ class Subline {
/// The coordinates are represented by a [LatLng] object.
/// The [subline] parameter is the subline of the route path.
class RoutePath {
static Client httpClient = Client();

Subline subline;
List<List<LatLng>> paths;

Expand All @@ -237,7 +244,8 @@ class RoutePath {
Uri url = Uri.parse(
"https://ws.tib.org/sictmws-rest/lines/ctmr4/${subline.parentLine.code}/kmz/${subline.code}");
try {
Uint8List responseBytes = await get(url).then((value) => value.bodyBytes);
Uint8List responseBytes =
await httpClient.get(url).then((value) => value.bodyBytes);
return RoutePath.fromKmz(utf8.decode(responseBytes), subline);
} on FormatException {
throw FormatException("Something went wrong. 😶");
Expand Down
8 changes: 6 additions & 2 deletions lib/src/api/stations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'package:tib_api/src/api/route_line.dart';
/// A station has a code, an ID, a latitude, a longitude, a name, and a reference.
/// The reference is optional.
class Station {
static Client httpClient = Client();

int code;
int id;
double lat;
Expand Down Expand Up @@ -63,7 +65,8 @@ class Station {
Uri url =
Uri.parse("https://ws.tib.org/sictmws-rest/stops/ctmr4/$stationCode");
try {
Uint8List responseBytes = await get(url).then((value) => value.bodyBytes);
Uint8List responseBytes =
await httpClient.get(url).then((value) => value.bodyBytes);

List<RouteLine> lines = [];
for (Map line in json.decode(utf8.decode(responseBytes))["lines"]) {
Expand All @@ -88,7 +91,8 @@ class Station {
Uri.parse("https://ws.tib.org/sictmws-rest/stops/ctmr4?res=$count");

try {
Uint8List responseBytes = await get(url).then((value) => value.bodyBytes);
Uint8List responseBytes =
await httpClient.get(url).then((value) => value.bodyBytes);

List<Station> stations = [];
for (Map station
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: tib_api
description: A reverse engineered API for the TIB (Transportes de las Islas Baleares) public transport service.
version: 0.5.6
version: 0.5.7
repository: https://github.com/YarosMallorca/tib_api
issue_tracker: https://github.com/YarosMallorca/tib_api/issues

Expand Down
Loading

0 comments on commit 3999d75

Please sign in to comment.