| 1 | package Net::Nostr::ExternalId; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strictures 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use Net::Nostr::_ConstructorArgs (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | ▶ | use Class::Tiny qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | type | ||||||||||
| 11 | value | ||||||||||
| 12 | blockchain | ||||||||||
| 13 | chain_id | ||||||||||
| 14 | hint | ||||||||||
| 15 | 2 | ); | |||||||||
| 16 | |||||||||||
| 17 | 3 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 18 | 3 | my $class = shift; | |||||||||
| 19 | 3 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | |||||||||
| 20 | 3 | my $self = bless { %args }, $class; | |||||||||
| 21 | 3 | my %known; @known{Class::Tiny->get_all_attributes_for($class)} = (); | |||||||||
| 22 | 3 | my @unknown = grep { !exists $known{$_} } keys %$self; | |||||||||
| 23 | 3 | ▶ | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 1 | return $self; | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | # Tag builders -- each returns ($i_tag, $k_tag) | ||||||||||
| 28 | |||||||||||
| 29 | 5 | ▶ | sub url_tags { | ||||||||
|
Subroutine
url_tags: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 30 | 5 | my ($class, $url, %opts) = @_; | |||||||||
| 31 | # Strip fragment | ||||||||||
| 32 | 5 | $url =~ s/#.*//; | |||||||||
| 33 | 5 | my @i = ('i', $url); | |||||||||
| 34 | 5 | ▶ | push @i, $opts{hint} if defined $opts{hint}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 5 | return (\@i, ['k', 'web']); | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 4 | ▶ | sub isbn_tags { | ||||||||
|
Subroutine
isbn_tags: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 39 | 4 | my ($class, $isbn, %opts) = @_; | |||||||||
| 40 | 4 | $isbn =~ s/-//g; | |||||||||
| 41 | 4 | my @i = ('i', "isbn:$isbn"); | |||||||||
| 42 | 4 | ▶ | push @i, $opts{hint} if defined $opts{hint}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 4 | return (\@i, ['k', 'isbn']); | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 4 | ▶ | sub geo_tags { | ||||||||
|
Subroutine
geo_tags: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 47 | 4 | my ($class, $geohash, %opts) = @_; | |||||||||
| 48 | 4 | $geohash = lc $geohash; | |||||||||
| 49 | 4 | my @i = ('i', "geo:$geohash"); | |||||||||
| 50 | 4 | ▶ | push @i, $opts{hint} if defined $opts{hint}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 4 | return (\@i, ['k', 'geo']); | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 5 | ▶ | sub country_tags { | ||||||||
|
Subroutine
country_tags: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 55 | 5 | my ($class, $code, %opts) = @_; | |||||||||
| 56 | 5 | $code = uc $code; | |||||||||
| 57 | 5 | my @i = ('i', "iso3166:$code"); | |||||||||
| 58 | 5 | ▶ | push @i, $opts{hint} if defined $opts{hint}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 5 | return (\@i, ['k', 'iso3166']); | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 3 | ▶ | sub isan_tags { | ||||||||
|
Subroutine
isan_tags: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 63 | 3 | my ($class, $isan, %opts) = @_; | |||||||||
| 64 | 3 | my @i = ('i', "isan:$isan"); | |||||||||
| 65 | 3 | ▶ | push @i, $opts{hint} if defined $opts{hint}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 3 | return (\@i, ['k', 'isan']); | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 2 | ▶ | sub doi_tags { | ||||||||
|
Subroutine
doi_tags: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 70 | 2 | my ($class, $doi, %opts) = @_; | |||||||||
| 71 | 2 | $doi = lc $doi; | |||||||||
| 72 | 2 | my @i = ('i', "doi:$doi"); | |||||||||
| 73 | 2 | ▶ | push @i, $opts{hint} if defined $opts{hint}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 2 | return (\@i, ['k', 'doi']); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 2 | ▶ | sub hashtag_tags { | ||||||||
|
Subroutine
hashtag_tags: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 78 | 2 | my ($class, $topic, %opts) = @_; | |||||||||
| 79 | 2 | $topic = lc $topic; | |||||||||
| 80 | 2 | my @i = ('i', "#$topic"); | |||||||||
| 81 | 2 | ▶ | push @i, $opts{hint} if defined $opts{hint}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 2 | return (\@i, ['k', '#']); | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 1 | ▶ | sub podcast_feed_tags { | ||||||||
|
Subroutine
podcast_feed_tags: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 86 | 1 | my ($class, $guid, %opts) = @_; | |||||||||
| 87 | 1 | my @i = ('i', "podcast:guid:$guid"); | |||||||||
| 88 | 1 | ▶ | push @i, $opts{hint} if defined $opts{hint}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 1 | return (\@i, ['k', 'podcast:guid']); | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 2 | ▶ | sub podcast_episode_tags { | ||||||||
|
Subroutine
podcast_episode_tags: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 93 | 2 | my ($class, $guid, %opts) = @_; | |||||||||
| 94 | 2 | my @i = ('i', "podcast:item:guid:$guid"); | |||||||||
| 95 | 2 | ▶ | push @i, $opts{hint} if defined $opts{hint}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 2 | return (\@i, ['k', 'podcast:item:guid']); | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 1 | ▶ | sub podcast_publisher_tags { | ||||||||
|
Subroutine
podcast_publisher_tags: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 100 | 1 | my ($class, $guid, %opts) = @_; | |||||||||
| 101 | 1 | my @i = ('i', "podcast:publisher:guid:$guid"); | |||||||||
| 102 | 1 | ▶ | push @i, $opts{hint} if defined $opts{hint}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 1 | return (\@i, ['k', 'podcast:publisher:guid']); | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 4 | ▶ | sub blockchain_tx_tags { | ||||||||
|
Subroutine
blockchain_tx_tags: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 107 | 4 | my ($class, $blockchain, $txid, %opts) = @_; | |||||||||
| 108 | 4 | $txid = lc $txid; | |||||||||
| 109 | 4 | my $id_str; | |||||||||
| 110 | 4 | ▶ | if (defined $opts{chain_id}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 2 | $id_str = "$blockchain:$opts{chain_id}:tx:$txid"; | |||||||||
| 112 | } else { | ||||||||||
| 113 | 2 | $id_str = "$blockchain:tx:$txid"; | |||||||||
| 114 | } | ||||||||||
| 115 | 4 | my @i = ('i', $id_str); | |||||||||
| 116 | 4 | ▶ | push @i, $opts{hint} if defined $opts{hint}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 4 | return (\@i, ['k', "$blockchain:tx"]); | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 4 | ▶ | sub blockchain_address_tags { | ||||||||
|
Subroutine
blockchain_address_tags: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 121 | 4 | my ($class, $blockchain, $address, %opts) = @_; | |||||||||
| 122 | # Ethereum addresses are hex, lowercase. Bitcoin base58 is case-sensitive. | ||||||||||
| 123 | 4 | ▶ | if ($blockchain eq 'ethereum') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 2 | $address = lc $address; | |||||||||
| 125 | } | ||||||||||
| 126 | 4 | my $id_str; | |||||||||
| 127 | 4 | ▶ | if (defined $opts{chain_id}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 2 | $id_str = "$blockchain:$opts{chain_id}:address:$address"; | |||||||||
| 129 | } else { | ||||||||||
| 130 | 2 | $id_str = "$blockchain:address:$address"; | |||||||||
| 131 | } | ||||||||||
| 132 | 4 | my @i = ('i', $id_str); | |||||||||
| 133 | 4 | ▶ | push @i, $opts{hint} if defined $opts{hint}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 4 | return (\@i, ['k', "$blockchain:address"]); | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | # Parse an i tag value into its components | ||||||||||
| 138 | |||||||||||
| 139 | 33 | ▶ | sub parse { | ||||||||
|
Subroutine
parse: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||
| 140 | 33 | my ($class, $value) = @_; | |||||||||
| 141 | |||||||||||
| 142 | # URL | ||||||||||
| 143 | 33 | ▶ | if ($value =~ m{^https?://}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 2 | return { type => 'web', value => $value }; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | # Hashtag | ||||||||||
| 148 | 31 | ▶ | if ($value =~ /^#(.+)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | 2 | return { type => '#', value => $1 }; | |||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | # Podcast types (must check before generic colon split) | ||||||||||
| 153 | 29 | ▶ | if ($value =~ /^podcast:publisher:guid:(.+)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 154 | 2 | return { type => 'podcast:publisher:guid', value => $1 }; | |||||||||
| 155 | } | ||||||||||
| 156 | 27 | ▶ | if ($value =~ /^podcast:item:guid:(.+)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 2 | return { type => 'podcast:item:guid', value => $1 }; | |||||||||
| 158 | } | ||||||||||
| 159 | 25 | ▶ | if ($value =~ /^podcast:guid:(.+)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | 2 | return { type => 'podcast:guid', value => $1 }; | |||||||||
| 161 | } | ||||||||||
| 162 | |||||||||||
| 163 | # Simple prefixed types: isbn, geo, iso3166, isan, doi | ||||||||||
| 164 | 23 | ▶ | if ($value =~ /^(isbn|geo|iso3166|isan|doi):(.+)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 165 | 13 | return { type => $1, value => $2 }; | |||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | # Blockchain with chain_id: <chain>:<chainId>:<tx|address>:<value> | ||||||||||
| 169 | 10 | ▶ | if ($value =~ /^(\w+):(\d+):(tx|address):(.+)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | return { | ||||||||||
| 171 | 4 | type => "$1:$3", | |||||||||
| 172 | value => $4, | ||||||||||
| 173 | blockchain => $1, | ||||||||||
| 174 | chain_id => int($2), | ||||||||||
| 175 | }; | ||||||||||
| 176 | } | ||||||||||
| 177 | |||||||||||
| 178 | # Blockchain without chain_id: <chain>:<tx|address>:<value> | ||||||||||
| 179 | 6 | ▶ | if ($value =~ /^(\w+):(tx|address):(.+)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 180 | return { | ||||||||||
| 181 | 4 | type => "$1:$2", | |||||||||
| 182 | value => $3, | ||||||||||
| 183 | blockchain => $1, | ||||||||||
| 184 | chain_id => undef, | ||||||||||
| 185 | }; | ||||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | 2 | return undef; | |||||||||
| 189 | } | ||||||||||
| 190 | |||||||||||
| 191 | # Derive the k tag value from an i tag value | ||||||||||
| 192 | |||||||||||
| 193 | 16 | ▶ | sub kind_for { | ||||||||
|
Subroutine
kind_for: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 194 | 16 | my ($class, $value) = @_; | |||||||||
| 195 | 16 | my $parsed = $class->parse($value); | |||||||||
| 196 | 16 | ▶ | return undef unless $parsed; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 197 | 15 | return $parsed->{type}; | |||||||||
| 198 | } | ||||||||||
| 199 | |||||||||||
| 200 | 2 | 1; | |||||||||
| 201 | |||||||||||
| 202 | __END__ | ||||||||||