Skip to content

Commit

Permalink
refactor: Improvements and standardization of data conversion methods
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor-pelykh committed Sep 18, 2022
1 parent 64c1823 commit 36da5fe
Show file tree
Hide file tree
Showing 52 changed files with 564 additions and 548 deletions.
8 changes: 4 additions & 4 deletions lib/data/asset_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class AssetPlatform {
final int? chainIdentifier;

AssetPlatform.fromJson(Map<String, dynamic> json)
: this.id = Convert.toNotNullableString(json['id'], ''),
this.chainIdentifier = Convert.toInt(json['chain_identifier']),
this.name = Convert.toNotNullableString(json['name'], ''),
this.shortName = Convert.toNotNullableString(json['shortName'], '');
: this.id = Convert.toStr(json['id'], ''),
this.chainIdentifier = Convert.toIntN(json['chain_identifier']),
this.name = Convert.toStr(json['name'], ''),
this.shortName = Convert.toStr(json['shortName'], '');

@override
String toString() {
Expand Down
12 changes: 6 additions & 6 deletions lib/data/category.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class Category {
final DateTime updatedAt;

Category.fromJson(Map<String, dynamic> json)
: this.id = Convert.toNotNullableString(json['id'], ''),
this.name = Convert.toNotNullableString(json['name'], ''),
this.marketCap = Convert.toDouble(json['market_cap']) ?? 0,
: this.id = Convert.toStr(json['id'], ''),
this.name = Convert.toStr(json['name'], ''),
this.marketCap = Convert.toDouble(json['market_cap'], 0),
this.marketCapChange24h =
Convert.toDouble(json['market_cap_change_24h']) ?? 0,
this.volume24h = Convert.toDouble(json['volume_24h']) ?? 0,
Convert.toDouble(json['market_cap_change_24h'], 0),
this.volume24h = Convert.toDouble(json['volume_24h'], 0),
this.updatedAt =
Convert.toDateTime(json['updated_at']) ?? Helpers.defaultDateTime();
Convert.toDateTime(json['updated_at'], Helpers.defaultDateTime());

@override
String toString() {
Expand Down
4 changes: 2 additions & 2 deletions lib/data/category_short.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class CategoryShort {
final String name;

CategoryShort.fromJson(Map<String, dynamic> json)
: this.id = Convert.toNotNullableString(json['category_id'], ''),
this.name = Convert.toNotNullableString(json['name'], '');
: this.id = Convert.toStr(json['category_id'], ''),
this.name = Convert.toStr(json['name'], '');

@override
String toString() {
Expand Down
54 changes: 25 additions & 29 deletions lib/data/coin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,35 +112,32 @@ class Coin {
final List<Ticker>? tickers;

static List<StatusUpdate>? _parseStatusUpdates(dynamic json) {
final jsonList = Convert.toList(json);
final jsonList = Convert.toListN(json);
return jsonList != null
? jsonList.map((e) => StatusUpdate.fromJson(e)).toList()
: null;
}

static List<Ticker>? _parseTickers(dynamic json) {
final jsonList = Convert.toList(json);
final jsonList = Convert.toListN(json);
return jsonList != null
? jsonList.map((e) => Ticker.fromJson(e)).toList()
: null;
}

Coin.fromJson(Map<String, dynamic> json)
: this.id = Convert.toNotNullableString(json['id'], ''),
this.symbol = Convert.toNotNullableString(json['symbol'], ''),
this.name = Convert.toNotNullableString(json['name'], ''),
this.assetPlatformId =
Convert.toNullableString(json['asset_platform_id']),
this.platforms = Helpers.parseMapStringString(json['platforms']),
: this.id = Convert.toStr(json['id'], ''),
this.symbol = Convert.toStr(json['symbol'], ''),
this.name = Convert.toStr(json['name'], ''),
this.assetPlatformId = Convert.toStrN(json['asset_platform_id']),
this.platforms = Convert.toMapOfStringStringN(json['platforms']),
this.blockTimeInMinutes =
Convert.toDouble(json['block_time_in_minutes']) ?? 0,
this.hashingAlgorithm =
Convert.toNotNullableString(json['hashing_algorithm'], ''),
this.categories = Helpers.parseListString(json['categories']),
this.publicNotice =
Convert.toNotNullableString(json['public_notice'], ''),
Convert.toDouble(json['block_time_in_minutes'], 0),
this.hashingAlgorithm = Convert.toStr(json['hashing_algorithm'], ''),
this.categories = Convert.toListOfString(json['categories']),
this.publicNotice = Convert.toStr(json['public_notice'], ''),
this.additionalNotices =
Helpers.parseListString(json['additional_notices']),
Convert.toListOfString(json['additional_notices']),
this.localization = json.containsKey('localization')
? LocalizedString.fromJson(json['localization'])
: null,
Expand All @@ -153,22 +150,21 @@ class Coin {
this.image = json.containsKey('image')
? ImageInfo.fromJson(json['image'])
: null,
this.countryOrigin = Convert.toNullableString(json['country_origin']),
this.genesisDate = Convert.toDateTime(json['genesis_date']),
this.contractAddress =
Convert.toNullableString(json['contract_address']),
this.countryOrigin = Convert.toStrN(json['country_origin']),
this.genesisDate = Convert.toDateTimeN(json['genesis_date']),
this.contractAddress = Convert.toStrN(json['contract_address']),
this.sentimentVotesUpPercentage =
Convert.toDouble(json['sentiment_votes_up_percentage']),
Convert.toDoubleN(json['sentiment_votes_up_percentage']),
this.sentimentVotesDownPercentage =
Convert.toDouble(json['sentiment_votes_down_percentage']),
this.marketCapRank = Convert.toInt(json['market_cap_rank']),
this.coingeckoRank = Convert.toInt(json['coingecko_rank']),
this.coingeckoScore = Convert.toDouble(json['coingecko_score']),
this.developerScore = Convert.toDouble(json['developer_score']),
this.communityScore = Convert.toDouble(json['community_score']),
this.liquidityScore = Convert.toDouble(json['liquidity_score']),
Convert.toDoubleN(json['sentiment_votes_down_percentage']),
this.marketCapRank = Convert.toIntN(json['market_cap_rank']),
this.coingeckoRank = Convert.toIntN(json['coingecko_rank']),
this.coingeckoScore = Convert.toDoubleN(json['coingecko_score']),
this.developerScore = Convert.toDoubleN(json['developer_score']),
this.communityScore = Convert.toDoubleN(json['community_score']),
this.liquidityScore = Convert.toDoubleN(json['liquidity_score']),
this.publicInterestScore =
Convert.toDouble(json['public_interest_score']),
Convert.toDoubleN(json['public_interest_score']),
this.marketData = json.containsKey('market_data')
? CoinMarketData.fromJson(json['market_data'])
: null,
Expand All @@ -182,7 +178,7 @@ class Coin {
? CoinPublicInterestsStats.fromJson(json['public_interest_stats'])
: null,
this.statusUpdates = _parseStatusUpdates(json['status_updates']),
this.lastUpdated = Convert.toDateTime(json['last_updated']),
this.lastUpdated = Convert.toDateTimeN(json['last_updated']),
this.tickers = _parseTickers(json['tickers']);

@override
Expand Down
14 changes: 7 additions & 7 deletions lib/data/coin_community_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ class CoinCommunityData {
final int? telegramChannelUserCount;

CoinCommunityData.fromJson(Map<String, dynamic> json)
: this.facebookLikes = Convert.toInt(json['facebook_likes']),
this.twitterFollowers = Convert.toInt(json['twitter_followers']),
: this.facebookLikes = Convert.toIntN(json['facebook_likes']),
this.twitterFollowers = Convert.toIntN(json['twitter_followers']),
this.redditAveragePosts48h =
Convert.toDouble(json['reddit_average_posts_48h']),
Convert.toDoubleN(json['reddit_average_posts_48h']),
this.redditAverageComments48h =
Convert.toDouble(json['reddit_average_comments_48h']),
this.redditSubscribers = Convert.toInt(json['reddit_subscribers']),
Convert.toDoubleN(json['reddit_average_comments_48h']),
this.redditSubscribers = Convert.toIntN(json['reddit_subscribers']),
this.redditAccountsActive48h =
Convert.toDouble(json['reddit_accounts_active_48h']),
Convert.toDoubleN(json['reddit_accounts_active_48h']),
this.telegramChannelUserCount =
Convert.toInt(json['telegram_channel_user_count']);
Convert.toIntN(json['telegram_channel_user_count']);

@override
String toString() {
Expand Down
18 changes: 9 additions & 9 deletions lib/data/coin_developer_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ class CoinDeveloperData {
final List<int>? last4WeeksCommitActivitySeries;

CoinDeveloperData.fromJson(Map<String, dynamic> json)
: this.forks = Convert.toInt(json['forks']),
this.stars = Convert.toInt(json['stars']),
this.subscribers = Convert.toInt(json['subscribers']),
this.totalIssues = Convert.toInt(json['total_issues']),
this.closedIssues = Convert.toInt(json['closed_issues']),
this.pullRequestsMerged = Convert.toInt(json['pull_requests_merged']),
: this.forks = Convert.toIntN(json['forks']),
this.stars = Convert.toIntN(json['stars']),
this.subscribers = Convert.toIntN(json['subscribers']),
this.totalIssues = Convert.toIntN(json['total_issues']),
this.closedIssues = Convert.toIntN(json['closed_issues']),
this.pullRequestsMerged = Convert.toIntN(json['pull_requests_merged']),
this.pullRequestContributors =
Convert.toInt(json['pull_request_contributors']),
Convert.toIntN(json['pull_request_contributors']),
this.codeAdditionsDeletions4Weeks =
json.containsKey('code_additions_deletions_4_weeks')
? CoinDeveloperDataCodeChanges.fromJson(
json['code_additions_deletions_4_weeks'])
: null,
this.commitCount4Weeks = Convert.toInt(json['commit_count_4_weeks']),
this.commitCount4Weeks = Convert.toIntN(json['commit_count_4_weeks']),
this.last4WeeksCommitActivitySeries =
Helpers.parseListInt(json['last_4_weeks_commit_activity_series']);
Convert.toListOfInt(json['last_4_weeks_commit_activity_series']);

@override
String toString() {
Expand Down
4 changes: 2 additions & 2 deletions lib/data/coin_developer_data_code_changes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class CoinDeveloperDataCodeChanges {
final int? deletions;

CoinDeveloperDataCodeChanges.fromJson(Map<String, dynamic> json)
: this.additions = Convert.toInt(json['additions']),
this.deletions = Convert.toInt(json['deletions']);
: this.additions = Convert.toIntN(json['additions']),
this.deletions = Convert.toIntN(json['deletions']);

@override
String toString() {
Expand Down
6 changes: 3 additions & 3 deletions lib/data/coin_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class CoinHistory {
final CoinPublicInterestsStats? publicInterestStats;

CoinHistory.fromJson(Map<String, dynamic> json)
: this.id = Convert.toNotNullableString(json['id'], ''),
this.symbol = Convert.toNotNullableString(json['symbol'], ''),
this.name = Convert.toNotNullableString(json['name'], ''),
: this.id = Convert.toStr(json['id'], ''),
this.symbol = Convert.toStr(json['symbol'], ''),
this.name = Convert.toStr(json['name'], ''),
this.localization = json.containsKey('localization')
? LocalizedString.fromJson(json['localization'])
: null,
Expand Down
23 changes: 10 additions & 13 deletions lib/data/coin_links.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,19 @@ class CoinLinks {
final CoinReposUrl? reposUrl;

CoinLinks.fromJson(Map<String, dynamic> json)
: this.homepage = Helpers.parseListString(json['homepage']),
this.blockchainSite = Helpers.parseListString(json['blockchain_site']),
: this.homepage = Convert.toListOfString(json['homepage']),
this.blockchainSite = Convert.toListOfString(json['blockchain_site']),
this.officialForumUrl =
Helpers.parseListString(json['official_forum_url']),
this.chatUrl = Helpers.parseListString(json['chat_url']),
this.announcementUrl =
Helpers.parseListString(json['announcement_url']),
this.twitterScreenName =
Convert.toNullableString(json['twitter_screen_name']),
this.facebookUsername =
Convert.toNullableString(json['facebook_username']),
Convert.toListOfString(json['official_forum_url']),
this.chatUrl = Convert.toListOfString(json['chat_url']),
this.announcementUrl = Convert.toListOfString(json['announcement_url']),
this.twitterScreenName = Convert.toStrN(json['twitter_screen_name']),
this.facebookUsername = Convert.toStrN(json['facebook_username']),
this.bitcointalkThreadIdentifier =
Convert.toNullableString(json['bitcointalk_thread_identifier']),
Convert.toStrN(json['bitcointalk_thread_identifier']),
this.telegramChannelIdentifier =
Convert.toNullableString(json['telegram_channel_identifier']),
this.subredditUrl = Convert.toNullableString(json['subreddit_url']),
Convert.toStrN(json['telegram_channel_identifier']),
this.subredditUrl = Convert.toStrN(json['subreddit_url']),
this.reposUrl = json.containsKey('repos_url')
? CoinReposUrl.fromJson(json['repos_url'])
: null;
Expand Down
36 changes: 18 additions & 18 deletions lib/data/coin_market_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,36 @@ class CoinMarketData {

CoinMarketData.fromJson(Map<String, dynamic> json)
: this.dataByCurrency = Helpers.parseMarketData(json),
this.totalValueLocked = Convert.toDouble(json['total_value_locked']),
this.mcapToTvlRatio = Convert.toDouble(json['mcap_to_tvl_ratio']),
this.fdvToTvlRatio = Convert.toDouble(json['fdv_to_tvl_ratio']),
this.roi = Convert.toDouble(json['roi']),
this.priceChange24h = Convert.toDouble(json['price_change_24h']),
this.totalValueLocked = Convert.toDoubleN(json['total_value_locked']),
this.mcapToTvlRatio = Convert.toDoubleN(json['mcap_to_tvl_ratio']),
this.fdvToTvlRatio = Convert.toDoubleN(json['fdv_to_tvl_ratio']),
this.roi = Convert.toDoubleN(json['roi']),
this.priceChange24h = Convert.toDoubleN(json['price_change_24h']),
this.priceChangePercentage24h =
Convert.toDouble(json['price_change_percentage_24h']),
Convert.toDoubleN(json['price_change_percentage_24h']),
this.priceChangePercentage7d =
Convert.toDouble(json['price_change_percentage_7d']),
Convert.toDoubleN(json['price_change_percentage_7d']),
this.priceChangePercentage14d =
Convert.toDouble(json['price_change_percentage_14d']),
Convert.toDoubleN(json['price_change_percentage_14d']),
this.priceChangePercentage30d =
Convert.toDouble(json['price_change_percentage_30d']),
Convert.toDoubleN(json['price_change_percentage_30d']),
this.priceChangePercentage60d =
Convert.toDouble(json['price_change_percentage_60d']),
Convert.toDoubleN(json['price_change_percentage_60d']),
this.priceChangePercentage200d =
Convert.toDouble(json['price_change_percentage_200d']),
Convert.toDoubleN(json['price_change_percentage_200d']),
this.priceChangePercentage1y =
Convert.toDouble(json['price_change_percentage_1y']),
Convert.toDoubleN(json['price_change_percentage_1y']),
this.marketCapChange24h =
Convert.toDouble(json['market_cap_change_24h']),
Convert.toDoubleN(json['market_cap_change_24h']),
this.marketCapChangePercentage24h =
Convert.toDouble(json['market_cap_change_percentage_24h']),
this.totalSupply = Convert.toDouble(json['total_supply']),
this.maxSupply = Convert.toDouble(json['max_supply']),
this.circulatingSupply = Convert.toDouble(json['circulating_supply']),
Convert.toDoubleN(json['market_cap_change_percentage_24h']),
this.totalSupply = Convert.toDoubleN(json['total_supply']),
this.maxSupply = Convert.toDoubleN(json['max_supply']),
this.circulatingSupply = Convert.toDoubleN(json['circulating_supply']),
this.sparkline7d = json.containsKey('sparkline_7d')
? MarketSparkline.fromJson(json['sparkline_7d'])
: null,
this.lastUpdated = Convert.toDateTime(json['last_updated']);
this.lastUpdated = Convert.toDateTimeN(json['last_updated']);

@override
String toString() {
Expand Down
4 changes: 2 additions & 2 deletions lib/data/coin_public_interests_stats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class CoinPublicInterestsStats {
final int? bingMatches;

CoinPublicInterestsStats.fromJson(Map<String, dynamic> json)
: this.alexaRank = Convert.toInt(json['alexa_rank']),
this.bingMatches = Convert.toInt(json['bing_matches']);
: this.alexaRank = Convert.toIntN(json['alexa_rank']),
this.bingMatches = Convert.toIntN(json['bing_matches']);

@override
String toString() {
Expand Down
5 changes: 3 additions & 2 deletions lib/data/coin_repos_url.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:coingecko_api/helpers/convert.dart';
import 'package:coingecko_api/helpers/helpers.dart';

/// Repository urls data wrapper
Expand All @@ -9,8 +10,8 @@ class CoinReposUrl {
final List<String>? bitbucket;

CoinReposUrl.fromJson(Map<String, dynamic> json)
: this.github = Helpers.parseListString(json['homepage']),
this.bitbucket = Helpers.parseListString(json['bitbucket']);
: this.github = Convert.toListOfString(json['homepage']),
this.bitbucket = Convert.toListOfString(json['bitbucket']);

@override
String toString() {
Expand Down
8 changes: 4 additions & 4 deletions lib/data/coin_short.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class CoinShort {
final Map<String, String>? platforms;

CoinShort.fromJson(Map<String, dynamic> json)
: this.id = Convert.toNotNullableString(json['id'], ''),
this.symbol = Convert.toNotNullableString(json['symbol'], ''),
this.name = Convert.toNotNullableString(json['name'], ''),
this.platforms = Helpers.parseMapStringString(json['platforms']);
: this.id = Convert.toStr(json['id'], ''),
this.symbol = Convert.toStr(json['symbol'], ''),
this.name = Convert.toStr(json['name'], ''),
this.platforms = Convert.toMapOfStringStringN(json['platforms']);

@override
String toString() {
Expand Down
8 changes: 4 additions & 4 deletions lib/data/companies_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ class CompaniesData {
final List<Company> companies;

static List<Company> _parseCompanies(dynamic json) {
final jsonList = Convert.toList(json);
final jsonList = Convert.toListN(json);
return jsonList != null
? jsonList.map((e) => Company.fromJson(e)).toList()
: [];
}

CompaniesData.fromJson(Map<String, dynamic> json)
: this.totalHoldings = Convert.toDouble(json['total_holdings']) ?? 0,
this.totalValueUsd = Convert.toDouble(json['total_value_usd']) ?? 0,
: this.totalHoldings = Convert.toDouble(json['total_holdings'], 0),
this.totalValueUsd = Convert.toDouble(json['total_value_usd'], 0),
this.marketCapDominance =
Convert.toDouble(json['market_cap_dominance']) ?? 0,
Convert.toDouble(json['market_cap_dominance'], 0),
this.companies = _parseCompanies(json['companies']);

@override
Expand Down
14 changes: 7 additions & 7 deletions lib/data/company.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ class Company {
final double percentageOfTotalSupply;

Company.fromJson(Map<String, dynamic> json)
: this.name = Convert.toNotNullableString(json['name'], ''),
this.symbol = Convert.toNotNullableString(json['symbol'], ''),
this.country = Convert.toNotNullableString(json['country'], ''),
this.totalHoldings = Convert.toDouble(json['total_holdings']) ?? 0,
: this.name = Convert.toStr(json['name'], ''),
this.symbol = Convert.toStr(json['symbol'], ''),
this.country = Convert.toStr(json['country'], ''),
this.totalHoldings = Convert.toDouble(json['total_holdings'], 0),
this.totalEntryValueUsd =
Convert.toDouble(json['total_entry_value_usd']) ?? 0,
Convert.toDouble(json['total_entry_value_usd'], 0),
this.totalCurrentValueUsd =
Convert.toDouble(json['total_current_value_usd']) ?? 0,
Convert.toDouble(json['total_current_value_usd'], 0),
this.percentageOfTotalSupply =
Convert.toDouble(json['percentage_of_total_supply']) ?? 0;
Convert.toDouble(json['percentage_of_total_supply'], 0);

@override
String toString() {
Expand Down
Loading

0 comments on commit 36da5fe

Please sign in to comment.