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-11-29'; # DATE
4             our $VERSION = '20181129.0.0'; # VERSION
5              
6 1     1   70068 use 5.010001;
  1         12  
7 1     1   6 use strict;
  1         3  
  1         34  
8 1     1   6 use warnings;
  1         2  
  1         612  
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 101 my $class = shift;
17              
18 1 50       6 unless (keys %by_code) {
19 1         5 while (defined(my $line = )) {
20 2076         3170 chomp $line;
21 2076         5155 my @ff = split /\t/, $line;
22 2076         4288 my ($code, $name, $safename) = @ff;
23 2076         4356 $by_code{$code} = \@ff;
24 2076         5315 $by_name_lc{lc $name} = \@ff;
25 2076         3905 $by_safename{$safename} = \@ff;
26 2076         6025 push @all_data, \@ff;
27             }
28             }
29              
30 1         7 bless {}, $class;
31             }
32              
33             sub by_code {
34 3     3 1 1150 my ($self, $code) = @_;
35 3         7 $code = uc($code);
36             die "Can't find cryptocurrency with code '$code'"
37 3 100       21 unless $by_code{$code};
38             return {
39             code=>$code,
40             name=>$by_code{$code}[1],
41 2         19 safename=>$by_code{$code}[2],
42             };
43             }
44              
45 1     1 1 2464 sub by_ticker { by_code(@_) }
46              
47             sub by_name {
48 3     3 1 2437 my ($self, $name) = @_;
49             die "Can't find cryptocurrency with name '$name'"
50 3 100       23 unless my $rec = $by_name_lc{lc $name};
51             return {
52 2         18 name=>$rec->[1],
53             code=>$rec->[0],
54             safename=>$rec->[2],
55             };
56             }
57              
58             sub by_safename {
59 3     3 1 2236 my ($self, $safename) = @_;
60 3         8 $safename = lc($safename);
61             die "Can't find cryptocurrency with safename '$safename'"
62 3 100       27 unless $by_safename{$safename};
63             return {
64             safename=>$safename,
65             code=>$by_safename{$safename}[0],
66 2         17 name=>$by_safename{$safename}[1],
67             };
68             }
69              
70 1     1 1 2196 sub by_slug { by_safename(@_) }
71              
72             sub all_codes {
73 1     1 1 2418 my $self = shift;
74 1         3 my @res;
75 1         3 for (@all_data) {
76 2076         3741 push @res, $_->[0];
77             }
78 1         593 @res;
79             }
80              
81             sub all_data {
82 1     1 1 2341 my $self = shift;
83 1         3 my @res;
84 1         3 for (@all_data) {
85 2076         6003 push @res, {code=>$_->[0], name=>$_->[1], safename=>$_->[2]};
86             }
87 1         174 @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 20181129.0.0 of CryptoCurrency::Catalog (from Perl distribution CryptoCurrency-Catalog), released on 2018-11-29.
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__