File Coverage

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