| 1 | package Net::Braintree::Xml; | ||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | |||||||||||||||||||
| 4 | 1 | ▶ | use XML::Simple; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 1 | ▶ | use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 1 | ▶ | use Exporter; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||
| 8 | 1 | our @EXPORT = qw(hash_to_xml xml_to_hash array collect_from_array); | |||||||||||||||||
| 9 | 1 | our @EXPORT_OK = qw(); | |||||||||||||||||
| 10 | 1 | ▶ | use Net::Braintree::Util; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 1 | ▶ | use DateTime::Format::Atom; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | 1 | ▶ | use XML::LibXML; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | 0 | ▶ | sub hash_to_xml { | ||||||||||||||||
|
Subroutine
hash_to_xml: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 15 | 0 | my $hash = shift; | |||||||||||||||||
| 16 | 0 | my $doc = XML::LibXML::Document->createDocument('1.0', 'UTF-8'); | |||||||||||||||||
| 17 | 0 | my $root_key = (keys %$hash)[0]; | |||||||||||||||||
| 18 | 0 | my $element = XML::LibXML::Element->new($root_key); | |||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | 0 | my $value = $hash->{$root_key}; | |||||||||||||||||
| 21 | 0 | my $node = add_node($element, $value); | |||||||||||||||||
| 22 | 0 | $doc->setDocumentElement($element); | |||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | 0 | return $doc->toString(1); | |||||||||||||||||
| 25 | } | ||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | 0 | ▶ | sub add_node { | ||||||||||||||||
|
Subroutine
add_node: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||
| 28 | 0 | my ($parent, $value) = @_; | |||||||||||||||||
| 29 | 0 | ▶ | if(is_hashref($value)){ | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 30 | 0 | build_from_hash($parent, $value); | |||||||||||||||||
| 31 | } elsif(is_arrayref($value)){ | ||||||||||||||||||
| 32 | 0 | build_from_array($parent, $value); | |||||||||||||||||
| 33 | } else { | ||||||||||||||||||
| 34 | 0 | ▶ | $parent->appendText($value) if $value; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 35 | } | ||||||||||||||||||
| 36 | } | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | 0 | ▶ | sub build_from_hash { | ||||||||||||||||
|
Subroutine
build_from_hash: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 39 | 0 | my($parent, $value) = @_; | |||||||||||||||||
| 40 | 0 | ▶ | while(my($key, $child_value) = each(%$value)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 41 | 0 | build_node($key, $child_value, $parent); | |||||||||||||||||
| 42 | } | ||||||||||||||||||
| 43 | } | ||||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | 0 | ▶ | sub build_from_array { | ||||||||||||||||
|
Subroutine
build_from_array: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 46 | 0 | my($parent, $value) = @_; | |||||||||||||||||
| 47 | 0 | $parent->setAttribute('type', 'array'); | |||||||||||||||||
| 48 | 0 | foreach my $child_value (@$value) { | |||||||||||||||||
| 49 | 0 | build_node('item', $child_value, $parent); | |||||||||||||||||
| 50 | } | ||||||||||||||||||
| 51 | } | ||||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | 0 | ▶ | sub build_node { | ||||||||||||||||
|
Subroutine
build_node: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 54 | 0 | my($node_name, $child_value, $parent) = @_; | |||||||||||||||||
| 55 | 0 | my $child = XML::LibXML::Element->new($node_name); | |||||||||||||||||
| 56 | 0 | add_node($child, $child_value); | |||||||||||||||||
| 57 | 0 | $parent->appendChild($child); | |||||||||||||||||
| 58 | } | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | 0 | ▶ | sub xml_to_hash { | ||||||||||||||||
|
Subroutine
xml_to_hash: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 61 | 0 | my $return = XMLin(shift, KeyAttr => [], KeepRoot => 1); | |||||||||||||||||
| 62 | 0 | my $scrubbed = scrubbed($return); | |||||||||||||||||
| 63 | 0 | return $scrubbed; | |||||||||||||||||
| 64 | } | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | 0 | ▶ | sub scrubbed { | ||||||||||||||||
|
Subroutine
scrubbed: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||
| 67 | 0 | my $tree = shift; | |||||||||||||||||
| 68 | 0 | ▶ | if (is_hashref($tree)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 69 | 0 | return collect_from_hash($tree); | |||||||||||||||||
| 70 | } | ||||||||||||||||||
| 71 | 0 | ▶ | if (is_arrayref($tree)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 72 | 0 | return collect_from_array($tree); | |||||||||||||||||
| 73 | } | ||||||||||||||||||
| 74 | 0 | return $tree; | |||||||||||||||||
| 75 | } | ||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | 0 | ▶ | sub collect_from_array { | ||||||||||||||||
|
Subroutine
collect_from_array: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 78 | 0 | my ($tree) = @_; | |||||||||||||||||
| 79 | 0 | my @new_array = (); | |||||||||||||||||
| 80 | 0 | foreach my $value (@$tree) { | |||||||||||||||||
| 81 | 0 | push(@new_array, scrubbed($value)); | |||||||||||||||||
| 82 | } | ||||||||||||||||||
| 83 | 0 | return \@new_array; | |||||||||||||||||
| 84 | } | ||||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | 0 | ▶ | sub collect_from_hash { | ||||||||||||||||
|
Subroutine
collect_from_hash: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||
| 87 | 0 | my ($tree) = @_; | |||||||||||||||||
| 88 | 0 | my $new_hash = {}; | |||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | 0 | my %types = ( | |||||||||||||||||
| 91 | 'array' => \&array, | ||||||||||||||||||
| 92 | 'boolean' => \&boolean, | ||||||||||||||||||
| 93 | 'integer' => \&integer, | ||||||||||||||||||
| 94 | 'datetime' => \&datetime, | ||||||||||||||||||
| 95 | 'date' => \&date | ||||||||||||||||||
| 96 | ); | ||||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | 0 | foreach my $type (keys %types) { | |||||||||||||||||
| 99 | 0 | ▶ | return $types{$type}->($tree) if is_of_type($type, $tree); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 100 | } | ||||||||||||||||||
| 101 | 0 | ▶ | return $tree->{'content'} if exists($tree->{'content'}); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 102 | 0 | ▶ | return undef if is_nil($tree); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 103 | 0 | ▶ | while(my ($key, $subtree) = each(%$tree)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 104 | 0 | $new_hash->{sub_dashes($key)} = scrubbed($subtree); | |||||||||||||||||
| 105 | } | ||||||||||||||||||
| 106 | 0 | return $new_hash; | |||||||||||||||||
| 107 | } | ||||||||||||||||||
| 108 | |||||||||||||||||||
| 109 | 0 | ▶ | sub is_of_type { | ||||||||||||||||
|
Subroutine
is_of_type: not called CC=2 SCAR=10.9
Pod
undocumented
| |||||||||||||||||||
| 110 | 0 | my ($type, $tree) = @_; | |||||||||||||||||
| 111 | 1 | ▶ | no warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 112 | 0 | ▶ | return 0 unless $tree->{'type'}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 113 | 0 | return $tree->{'type'} eq $type; | |||||||||||||||||
| 114 | } | ||||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | 0 | ▶ | sub is_nil { | ||||||||||||||||
|
Subroutine
is_nil: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 117 | 0 | my $tree = shift; | |||||||||||||||||
| 118 | 0 | ▶ | return 0 unless $tree->{'nil'}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 119 | 0 | return $tree->{'nil'} eq "true"; | |||||||||||||||||
| 120 | } | ||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | 0 | ▶ | sub boolean { | ||||||||||||||||
|
Subroutine
boolean: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 123 | 0 | ▶ | return shift->{'content'} eq 'true' ? 1 : 0; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 124 | } | ||||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | 0 | ▶ | sub integer { | ||||||||||||||||
|
Subroutine
integer: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 127 | 0 | return shift->{'content'}; | |||||||||||||||||
| 128 | } | ||||||||||||||||||
| 129 | |||||||||||||||||||
| 130 | 1 | my $f = DateTime::Format::Atom->new(); | |||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | 0 | ▶ | sub datetime { | ||||||||||||||||
|
Subroutine
datetime: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 133 | 0 | my $dt = $f->parse_datetime(shift->{'content'}); | |||||||||||||||||
| 134 | } | ||||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | 0 | ▶ | sub date { | ||||||||||||||||
|
Subroutine
date: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 137 | 0 | my $date = shift->{'content'}; | |||||||||||||||||
| 138 | 0 | $date .= 'T00:00:00Z'; | |||||||||||||||||
| 139 | 0 | my $dt = $f->parse_datetime($date); | |||||||||||||||||
| 140 | } | ||||||||||||||||||
| 141 | |||||||||||||||||||
| 142 | 0 | ▶ | sub array { | ||||||||||||||||
|
Subroutine
array: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||
| 143 | 0 | my $tree = shift; | |||||||||||||||||
| 144 | |||||||||||||||||||
| 145 | 0 | delete $tree->{type}; | |||||||||||||||||
| 146 | 0 | my $subtree = (values %$tree)[0]; | |||||||||||||||||
| 147 | 0 | ▶ | if (ref $subtree eq 'HASH') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 148 | 0 | return [scrubbed($subtree)]; | |||||||||||||||||
| 149 | } elsif (ref $subtree eq 'ARRAY') { | ||||||||||||||||||
| 150 | 0 | return scrubbed(force_array($subtree)); | |||||||||||||||||
| 151 | } elsif (defined($subtree)) { | ||||||||||||||||||
| 152 | 0 | return [$subtree]; | |||||||||||||||||
| 153 | } else { | ||||||||||||||||||
| 154 | 0 | return []; | |||||||||||||||||
| 155 | } | ||||||||||||||||||
| 156 | |||||||||||||||||||
| 157 | } | ||||||||||||||||||
| 158 | |||||||||||||||||||
| 159 | |||||||||||||||||||
| 160 | 0 | ▶ | sub sub_dashes { | ||||||||||||||||
|
Subroutine
sub_dashes: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 161 | 0 | my $string = shift; | |||||||||||||||||
| 162 | 0 | $string =~ s/-/_/g; | |||||||||||||||||
| 163 | 0 | return $string; | |||||||||||||||||
| 164 | } | ||||||||||||||||||
| 165 | |||||||||||||||||||
| 166 | 0 | ▶ | sub force_array { | ||||||||||||||||
|
Subroutine
force_array: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 167 | 0 | my $subtree = shift; | |||||||||||||||||
| 168 | 0 | ▶ | return $subtree if(is_arrayref($subtree)); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 169 | 0 | return [$subtree]; | |||||||||||||||||
| 170 | } | ||||||||||||||||||
| 171 | |||||||||||||||||||
| 172 | 1 | 1; | |||||||||||||||||