File Coverage

blib/lib/CryptoCurrency/Catalog.pm
Criterion Covered Total %
statement 42 42 100.0
branch 7 8 87.5
condition n/a
subroutine 11 11 100.0
pod 8 8 100.0
total 68 69 98.5


line stmt bran cond sub pod time code
1             package CryptoCurrency::Catalog;
2              
3             our $DATE = '2018-04-03'; # DATE
4             our $VERSION = '20180403'; # VERSION
5              
6 1     1   56144 use 5.010001;
  1         11  
7 1     1   5 use strict;
  1         1  
  1         27  
8 1     1   4 use warnings;
  1         2  
  1         527  
9              
10             my %by_code;
11             my %by_name_lc;
12             my %by_safename;
13             my @all_data;
14              
15             sub new {
16 1     1 1 72 my $class = shift;
17              
18 1 50       4 unless (keys %by_code) {
19 1         5 while (defined(my $line = )) {
20 1599         1940 chomp $line;
21 1599         3243 my @ff = split /\t/, $line;
22 1599         2550 my ($code, $name, $safename) = @ff;
23 1599         3399 $by_code{$code} = \@ff;
24 1599         3007 $by_name_lc{lc $name} = \@ff;
25 1599         2343 $by_safename{$safename} = \@ff;
26 1599         3719 push @all_data, \@ff;
27             }
28             }
29              
30 1         6 bless {}, $class;
31             }
32              
33             sub by_code {
34 3     3 1 977 my ($self, $code) = @_;
35 3         6 $code = uc($code);
36             die "Can't find cryptocurrency with code '$code'"
37 3 100       17 unless $by_code{$code};
38             return {
39             code=>$code,
40             name=>$by_code{$code}[1],
41 2         15 safename=>$by_code{$code}[2],
42             };
43             }
44              
45 1     1 1 1854 sub by_ticker { by_code(@_) }
46              
47             sub by_name {
48 3     3 1 1972 my ($self, $name) = @_;
49             die "Can't find cryptocurrency with name '$name'"
50 3 100       18 unless my $rec = $by_name_lc{lc $name};
51             return {
52 2         11 name=>$rec->[1],
53             code=>$rec->[0],
54             safename=>$rec->[2],
55             };
56             }
57              
58             sub by_safename {
59 3     3 1 1708 my ($self, $safename) = @_;
60 3         6 $safename = lc($safename);
61             die "Can't find cryptocurrency with safename '$safename'"
62 3 100       20 unless $by_safename{$safename};
63             return {
64             safename=>$safename,
65             code=>$by_safename{$safename}[0],
66 2         12 name=>$by_safename{$safename}[1],
67             };
68             }
69              
70 1     1 1 1597 sub by_slug { by_safename(@_) }
71              
72             sub all_codes {
73 1     1 1 1802 my $self = shift;
74 1         1 my @res;
75 1         3 for (@all_data) {
76 1599         2259 push @res, $_->[0];
77             }
78 1         353 @res;
79             }
80              
81             sub all_data {
82 1     1 1 1828 my $self = shift;
83 1         3 my @res;
84 1         2 for (@all_data) {
85 1599         3654 push @res, {code=>$_->[0], name=>$_->[1], safename=>$_->[2]};
86             }
87 1         143 @res;
88             }
89              
90             1;
91             # ABSTRACT: Catalog of cryptocurrencies
92              
93             =pod
94              
95             =encoding UTF-8
96              
97             =head1 NAME
98              
99             CryptoCurrency::Catalog - Catalog of cryptocurrencies
100              
101             =head1 VERSION
102              
103             This document describes version 20180403 of CryptoCurrency::Catalog (from Perl distribution CryptoCurrency-Catalog), released on 2018-04-03.
104              
105             =head1 SYNOPSIS
106              
107             use CryptoCurrency::Catalog;
108              
109             my $cat = CryptoCurrency::Catalog->new;
110              
111             my $record = $cat->by_code("ETH"); # => { code=>"ETH", name=>"Ethereum", safename=>"ethereum" }
112             my $record = $cat->by_ticker("eth"); # alias for by_code(), lowercase also works
113             my $record = $cat->by_name("Ethereum"); # note: case-sensitive
114             my $record = $cat->by_safename("ethereum");
115             my $record = $cat->by_slug("Ethereum"); # alias for by_safename(), mixed case also works
116              
117             my @codes = $cat->all_codes(); # => ("BTC", "ETH", ...)
118              
119             my @data = $cat->all_data; # => ({code=>"BTC", name=>"Bitcoin", safename=>"bitcoin"}, {...}, ...)
120              
121             =head1 DESCRIPTION
122              
123             This class attempts to provide a list/catalog of cryptocurrencies. The main
124             source for this catalog is the Cryptocurrency Market Capitalizations website
125             (L, or CMC for short).
126              
127             CMC does not provide unique codes nor unique names, only unique "safenames"
128             (slugs). Whenever there is a clash, this catalog modifies the clashing code
129             and/or unique name to make code and name to be unique again (usually the
130             coin/token with the smaller market cap "loses" the name).
131              
132             There is no guarantee that the code/name/safename of old/unlisted coins or
133             tokens will not be reused.
134              
135             =head1 METHODS
136              
137             =head2 new
138              
139             =head2 by_code
140              
141             =head2 by_ticker
142              
143             Alias for L.
144              
145             =head2 by_name
146              
147             =head2 by_safename
148              
149             =head2 by_slug
150              
151             Alias for L.
152              
153             =head2 all_codes
154              
155             =head2 all_data
156              
157             =head1 HOMEPAGE
158              
159             Please visit the project's homepage at L.
160              
161             =head1 SOURCE
162              
163             Source repository is at L.
164              
165             =head1 BUGS
166              
167             Please report any bugs or feature requests on the bugtracker website L
168              
169             When submitting a bug or request, please include a test-file or a
170             patch to an existing test-file that illustrates the bug or desired
171             feature.
172              
173             =head1 SEE ALSO
174              
175             L
176              
177             =head1 AUTHOR
178              
179             perlancar
180              
181             =head1 COPYRIGHT AND LICENSE
182              
183             This software is copyright (c) 2018 by perlancar@cpan.org.
184              
185             This is free software; you can redistribute it and/or modify it under
186             the same terms as the Perl 5 programming language system itself.
187              
188             =cut
189              
190             __DATA__