LCOV - code coverage report
Current view: top level - lib - gitmoji_client.dart (source / functions) Coverage Total Hit
Test: gitmoji Lines: 93.5 % 31 29
Test Date: 2026-05-24 19:15:45 Functions: - 0 0

            Line data    Source code
       1              : import 'dart:convert';
       2              : import 'dart:io' as io;
       3              : 
       4              : import 'package:gitmoji/nullable_string_extension.dart';
       5              : import 'package:http/http.dart' as http;
       6              : 
       7              : import 'gitmoji.dart';
       8              : 
       9              : class GitmojiClient {
      10              :   final bool debug;
      11              :   final http.Client _httpClient;
      12              :   final io.Directory _cacheDirectory;
      13              : 
      14            1 :   GitmojiClient({
      15              :     this.debug = false,
      16              :     http.Client? httpClient,
      17              :     io.Directory? cacheDirectory,
      18            0 :   })  : _httpClient = httpClient ?? http.Client(),
      19            0 :         _cacheDirectory = cacheDirectory ?? io.Directory.systemTemp;
      20              : 
      21            1 :   Future<List<Gitmoji>> fetch({
      22              :     String url =
      23              :         'https://raw.githubusercontent.com/carloscuesta/gitmoji/refs/heads/master/packages/gitmojis/src/gitmojis.json',
      24              :   }) async {
      25            4 :     final cacheFile = io.File('${_cacheDirectory.path}/gitmoji.cache');
      26            2 :     final cacheExpire = DateTime.now().subtract(const Duration(days: 1));
      27              : 
      28              :     String jsonString = '';
      29              : 
      30            1 :     if (cacheFile.existsSync() &&
      31            2 :         cacheFile.lastModifiedSync().isAfter(cacheExpire)) {
      32            1 :       _log('[CACHE] Hit!');
      33            1 :       jsonString = cacheFile.readAsStringSync();
      34              :     } else {
      35            1 :       _log('[CACHE] Getting...');
      36            1 :       jsonString = await _fetchFromWeb(url);
      37              : 
      38            1 :       if (jsonString.isNotEmpty) {
      39            1 :         cacheFile.writeAsStringSync(jsonString);
      40            1 :         _log('[CACHE] Save!');
      41            1 :       } else if (cacheFile.existsSync()) {
      42            1 :         _log('[CACHE] Using old one.');
      43            1 :         jsonString = cacheFile.readAsStringSync();
      44              :       }
      45              :     }
      46              : 
      47            1 :     if (jsonString.isNullOrBlank) {
      48            1 :       throw Exception('Gitmojis definition not found.');
      49              :     }
      50              : 
      51            1 :     final body = jsonDecode(jsonString);
      52            1 :     final List<dynamic> list = body['gitmojis'];
      53              : 
      54            4 :     return list.map((item) => Gitmoji.fromJson(item)).toList();
      55              :   }
      56              : 
      57            1 :   Future<String> _fetchFromWeb(String url) async {
      58              :     try {
      59            3 :       final response = await _httpClient.get(Uri.parse(url));
      60              : 
      61            4 :       if (response.statusCode < 200 || response.statusCode > 299) {
      62            3 :         throw Exception('Invalid status code: ${response.statusCode}');
      63              :       }
      64              : 
      65            1 :       return response.body;
      66              :     } catch (e, s) {
      67            2 :       _log('Error fetching from web: $e\n$s');
      68              :       return '';
      69              :     }
      70              :   }
      71              : 
      72            1 :   void _log(final String message) {
      73            1 :     if (debug) print(message);
      74              :   }
      75              : }
        

Generated by: LCOV version 2.0-1