Skip to content

Commit

Permalink
Merge pull request #9 from arielmagbanua/0.2.0
Browse files Browse the repository at this point in the history
Added Retrieval of Reading Plans
  • Loading branch information
arielmagbanua authored Sep 3, 2022
2 parents df4de83 + 74887ec commit e0dcaad
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@
## 0.1.0

- Added parsing of book chapters and verses.


## 0.2.0

- Added retrieving of reading plans.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ You can secure an API key by creating a new application on the [NLT API website]
## Features
* Get passages from NLT Bible API.
* Perform search with passages and short context paragraphs for a given search query terms.
* Retrieve a parsed list of references for the given reference string. (still in development)
* Retrieve a list of reading plans supported by the API. (still in development)
* Retrieve a parsed list of references for the given reference string.
* Retrieve a list of reading plans supported by the API.
* Retrieve the text for a given date in the given reading plan. (still in development)

## Getting started
Expand Down
9 changes: 8 additions & 1 deletion lib/src/api.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:http/http.dart' as http;
import 'package:nlt_bible/src/domain/entities/reading_plan.dart';

import 'data/data_sources/nlt_remote_data_source_implementation.dart';
import 'domain/entities/parsed_passage_segment.dart';
Expand Down Expand Up @@ -61,7 +62,13 @@ class Api {
///
/// The [ref] is the reference string to parse.
/// The [language] is the language of the result and defaults to english (en).
Future<List<List<ParsedPassageSegment?>>> parse(String ref, {String language = 'en'}) {
Future<List<List<ParsedPassageSegment?>>> parse(String ref,
{String language = 'en'}) {
return nltBibleRepository.parse(ref, language: language);
}

/// Retrieve all available reading plans.
Future<List<ReadingPlan?>> plans() {
return nltBibleRepository.plans();
}
}
3 changes: 3 additions & 0 deletions lib/src/data/data_sources/nlt_remote_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ abstract class NltRemoteDataSource extends RestfulDataSource {
/// The [ref] is the reference string to parse.
/// The [language] is the language of the result and defaults to english (en).
Future<List<dynamic>> parse(String ref, {String language = 'en'});

/// Retrieves the list of reading plans supported by the API.
Future<List<Map<String, dynamic>>> plans();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class NltRemoteDataSourceImplementation extends NltRemoteDataSource {

return sendRequest(
method: method,
url: '$url?$queryString',
url: queryString.isNotEmpty ? '$url?$queryString' : url,
);
}

Expand Down Expand Up @@ -91,4 +91,20 @@ class NltRemoteDataSourceImplementation extends NltRemoteDataSource {

return [];
}

/// Retrieves the list of reading plans supported by the API.
@override
Future<List<Map<String, dynamic>>> plans() async {
final response = await _response(
endpoint: 'plans',
method: 'GET',
);

if (response.statusCode == 200) {
return List<Map<String, dynamic>>.from(
json.decode(response.body) as List);
}

return [];
}
}
19 changes: 17 additions & 2 deletions lib/src/data/repositories/nlt_bible_repository.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:nlt_bible/src/domain/entities/reading_plan.dart';

import '../../domain/entities/parsed_passage_segment.dart';
import '../../domain/entities/passages.dart';
import '../../domain/entities/search.dart';
Expand Down Expand Up @@ -53,8 +55,10 @@ class NltBibleRepository implements contracts.NltBibleRepository {
/// The [ref] is the reference string to parse.
/// The [language] is the language of the result and defaults to english (en).
@override
Future<List<List<ParsedPassageSegment?>>> parse(String ref, {String language = 'en'}) async {
final parsedSource = await nltRemoteDataSource.parse(ref, language: language);
Future<List<List<ParsedPassageSegment?>>> parse(String ref,
{String language = 'en'}) async {
final parsedSource =
await nltRemoteDataSource.parse(ref, language: language);

List<List<dynamic>> parsedList = List<List<dynamic>>.from(parsedSource);

Expand All @@ -65,4 +69,15 @@ class NltBibleRepository implements contracts.NltBibleRepository {
}).toList();
}).toList();
}

/// Retrieve all available reading plans.
@override
Future<List<ReadingPlan?>> plans() async {
final plansData = await nltRemoteDataSource.plans();

final plans = plansData.map((plan) => ReadingPlan.fromJson(plan)).toList();

// remove duplicates and return the plans
return plans.toSet().toList();
}
}
51 changes: 51 additions & 0 deletions lib/src/domain/entities/reading_plan.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

part 'reading_plan.g.dart';

/// ReadingPlan
///
/// The entity class for reading plan.
@JsonSerializable()
class ReadingPlan extends Equatable {
/// The id of the reading plan.
final String id;

/// The title of the reading plan.
final String? title;

/// The text content of the reading plan.
@JsonKey(ignore: true)
final String? text;

/// Constructor
///
/// The [id] is id of the reading plan.
/// The [title] is the tile of the reading plan.
/// The [text] is the text content of the reading plan.
ReadingPlan({
required this.id,
required this.title,
this.text = null,
});

/// Generates [ReadingPlan] object from [json] map object.
factory ReadingPlan.fromJson(Map<String, dynamic> json) =>
_$ReadingPlanFromJson(json);

/// Converts this object to a json map object.
Map<String, dynamic> toJson() {
final json = _$ReadingPlanToJson(this);
json['text'] = text;

return json;
}

/// Retrieves all properties of this class.
@override
List<Object?> get props => [
id,
title,
text,
];
}
18 changes: 18 additions & 0 deletions lib/src/domain/entities/reading_plan.g.dart

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

8 changes: 7 additions & 1 deletion lib/src/domain/repositories/nlt_bible_repository.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:nlt_bible/src/domain/entities/reading_plan.dart';

import '../entities/parsed_passage_segment.dart';
import '../entities/passages.dart';
import '../entities/search.dart';
Expand Down Expand Up @@ -32,5 +34,9 @@ abstract class NltBibleRepository {
///
/// The [ref] is the reference string to parse.
/// The [language] is the language of the result and defaults to english (en).
Future<List<List<ParsedPassageSegment?>>> parse(String ref, {String language = 'en'});
Future<List<List<ParsedPassageSegment?>>> parse(String ref,
{String language = 'en'});

/// Retrieve all available reading plans.
Future<List<ReadingPlan?>> plans();
}
10 changes: 10 additions & 0 deletions prep_publis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

# format dart files
dart format .

# run the test
dart test

# execute dry run publish
dart pub publish --dry-run
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: nlt_bible
description: A future-based dart package for the NLT API from Tyndale House Publishers which can be used to fetch NLT bible passages.
version: 0.1.0
version: 0.2.0
homepage: https://github.com/arielmagbanua/nlt_bible

environment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,31 @@ void main() {
expect(second2['vs'], 21);
expect(second2['type_desc'], 'END');
});

test('Test retrieval of plans', () async {
final correctUri = Uri.parse(
'https://api.nlt.to/api/plans',
);

when(() => mockResponse.statusCode).thenReturn(200);
when(() => mockResponse.body).thenReturn(
plansSampleResponse,
);
when(() => mockedHttpClient.get(correctUri, headers: {})).thenAnswer(
(_) => Future<http.Response>.value(mockResponse),
);

final remoteDataSource = NltRemoteDataSourceImplementation(
httpClient: mockedHttpClient,
apiKey: apiKey,
);

final result = await remoteDataSource.plans();

final plan = result[0];

expect(plan['id'], 'OYCB');
expect(plan['title'], 'One Year® Chronological Bible');
expect(result.length, 2);
});
}
27 changes: 22 additions & 5 deletions test/src/data/repositories/nlt_bible_repository_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void main() {

when(() => mockNltRemoteDataSource.parse(ref, language: language))
.thenAnswer(
(_) => Future.value(parsedSource),
(_) => Future.value(parsedSource),
);

final repository = NltBibleRepository(mockNltRemoteDataSource);
Expand Down Expand Up @@ -138,7 +138,7 @@ void main() {

when(() => mockNltRemoteDataSource.parse(ref, language: language))
.thenAnswer(
(_) => Future.value(parsedSource),
(_) => Future.value(parsedSource),
);

final repository = NltBibleRepository(mockNltRemoteDataSource);
Expand Down Expand Up @@ -175,7 +175,7 @@ void main() {

when(() => mockNltRemoteDataSource.parse(ref, language: language))
.thenAnswer(
(_) => Future.value(parsedSource),
(_) => Future.value(parsedSource),
);

final repository = NltBibleRepository(mockNltRemoteDataSource);
Expand Down Expand Up @@ -212,7 +212,7 @@ void main() {

when(() => mockNltRemoteDataSource.parse(ref, language: language))
.thenAnswer(
(_) => Future.value(parsedSource),
(_) => Future.value(parsedSource),
);

final repository = NltBibleRepository(mockNltRemoteDataSource);
Expand Down Expand Up @@ -249,7 +249,7 @@ void main() {

when(() => mockNltRemoteDataSource.parse(ref, language: language))
.thenAnswer(
(_) => Future.value(parsedSource),
(_) => Future.value(parsedSource),
);

final repository = NltBibleRepository(mockNltRemoteDataSource);
Expand Down Expand Up @@ -295,4 +295,21 @@ void main() {
expect(second2.vs, 21);
expect(second2.typeDesc, 'END');
});

test('Test getting of plans', () async {
final plansSource = List<Map<String, dynamic>>.from(
json.decode(plansSampleResponse) as List,
);

when(() => mockNltRemoteDataSource.plans())
.thenAnswer((_) => Future.value(plansSource));

final repository = NltBibleRepository(mockNltRemoteDataSource);
final plans = await repository.plans();
final plan = plans[0]!;

expect(plans.length, 1);
expect(plan.id, 'OYCB');
expect(plan.title, 'One Year® Chronological Bible');
});
}
13 changes: 13 additions & 0 deletions test/test_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3088,3 +3088,16 @@ const parsedMultiRegularNotationRangeResponse = r'''
]
]
''';

const plansSampleResponse = r'''
[
{
"id": "OYCB",
"title": "One Year® Chronological Bible"
},
{
"id": "OYCB",
"title": "One Year® Chronological Bible"
}
]
''';

0 comments on commit e0dcaad

Please sign in to comment.