File Coverage

blib/lib/Data/Sah/Coerce/perl/To_str/From_str/ToCryptoExchangeCurrencyPair.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition 2 4 50.0
subroutine 5 5 100.0
pod 0 2 0.0
total 24 28 85.7


line stmt bran cond sub pod time code
1             package Data::Sah::Coerce::perl::To_str::From_str::ToCryptoExchangeCurrencyPair;
2              
3             # AUTHOR
4             our $DATE = '2019-11-28'; # DATE
5             our $DIST = 'Data-Sah-Coerce-perl-To_str-From_str-ToCryptoExchangeCurrencyPair'; # DIST
6             our $VERSION = '0.004'; # VERSION
7              
8 1     1   1790 use 5.010001;
  1         4  
9 1     1   5 use strict;
  1         3  
  1         18  
10 1     1   5 use warnings;
  1         1  
  1         217  
11              
12             sub meta {
13             +{
14 1     1 0 12 v => 4,
15             summary => 'Coerce string into cryptoexchange currency pair, e.g. LTC/USD',
16             might_fail => 1,
17             prio => 50,
18             };
19             }
20              
21             sub coerce {
22 1     1 0 15 my %args = @_;
23              
24 1         3 my $dt = $args{data_term};
25              
26 1         2 my $res = {};
27              
28 1         2 $res->{expr_match} = "!ref($dt)";
29 1   50     8 $res->{modules}{"CryptoCurrency::Catalog"} //= 0;
30 1   50     6 $res->{modules}{"Locale::Codes::Currency_Codes"} //= 0;
31 1         5 $res->{expr_coerce} = join(
32             "",
33             "do { my \$res; ",
34             " my (\$cur1, \$cur2) = $dt =~ m!\\A(\\S+)/(\\S+)\\z! or do { \$res = ['Invalid currency pair syntax, please use CUR1/CUR2 syntax']; goto RETURN_RES }; ",
35              
36             # check currency1
37             " my \$cat = CryptoCurrency::Catalog->new; ",
38             " my \$rec; eval { \$rec = \$cat->by_code(\$cur1) }; ",
39             " if (\$@) { \$res = ['Unknown cryptocurrency code: ' . \$cur1]; goto RETURN_RES } ",
40             " \$cur1 = \$rec->{code}; ",
41              
42             # check currency2
43             " \$cur2 = uc \$cur2; ",
44             " if (\$Locale::Codes::Data{currency}{code2id}{alpha}{\$cur2}) { } else { ",
45             " my \$rec; eval { \$rec = \$cat->by_code(\$cur2) }; ",
46             " if (\$@) { \$res = ['Unknown fiat/cryptocurrency code: ' . \$cur2]; goto RETURN_RES } ",
47             " } ",
48              
49             # check currency1 differs from currency2
50             " if (\$cur1 eq \$cur2) { \$res = ['Currency and base currency must differ']; goto RETURN_RES } ",
51              
52             " \$res = [undef, \"\$cur1/\$cur2\"]; ",
53              
54             " RETURN_RES: \$res; ",
55             "}",
56             );
57              
58 1         3 $res;
59             }
60              
61             1;
62             # ABSTRACT: Coerce string into cryptoexchange currency pair, e.g. LTC/USD
63              
64             __END__
65              
66             =pod
67              
68             =encoding UTF-8
69              
70             =head1 NAME
71              
72             Data::Sah::Coerce::perl::To_str::From_str::ToCryptoExchangeCurrencyPair - Coerce string into cryptoexchange currency pair, e.g. LTC/USD
73              
74             =head1 VERSION
75              
76             This document describes version 0.004 of Data::Sah::Coerce::perl::To_str::From_str::ToCryptoExchangeCurrencyPair (from Perl distribution Data-Sah-Coerce-perl-To_str-From_str-ToCryptoExchangeCurrencyPair), released on 2019-11-28.
77              
78             =head1 SYNOPSIS
79              
80             To use in a Sah schema:
81              
82             ["str",{"x.perl.coerce_rules"=>["From_str::ToCryptoExchangeCurrencyPair"]}]
83              
84             =head1 DESCRIPTION
85              
86             This coercion rules checks that:
87              
88             =over
89              
90             =item * string is in the form of "I<currency1>/I<currency2>"
91              
92             =item * I<currency1> is a known cryptocurrency code
93              
94             =item * I<currency2> is a known fiat currency or cryptocurrency code
95              
96             =item * I<currency1> is not the same as I<currency2>
97              
98             =back
99              
100             =for Pod::Coverage ^(meta|coerce)$
101              
102             =head1 HOMEPAGE
103              
104             Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah-Coerce-perl-To_str-From_str-ToCryptoExchangeCurrencyPair>.
105              
106             =head1 SOURCE
107              
108             Source repository is at L<https://github.com/perlancar/perl-Data-Sah-Coerce-perl-To_str-From_str-ToCryptoExchangeCurrencyPair>.
109              
110             =head1 BUGS
111              
112             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah-Coerce-perl-To_str-From_str-ToCryptoExchangeCurrencyPair>
113              
114             When submitting a bug or request, please include a test-file or a
115             patch to an existing test-file that illustrates the bug or desired
116             feature.
117              
118             =head1 AUTHOR
119              
120             perlancar <perlancar@cpan.org>
121              
122             =head1 COPYRIGHT AND LICENSE
123              
124             This software is copyright (c) 2019, 2018 by perlancar@cpan.org.
125              
126             This is free software; you can redistribute it and/or modify it under
127             the same terms as the Perl 5 programming language system itself.
128              
129             =cut