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