blib/lib/Parcel/Track/KR/Hanjin.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 117 CC 7 SCAR 27.1
CC
7
Cov
0%
track
42.0
BUILDARGS
6.0
uri
2.0
CRAP 15.1
1 package Parcel::Track::KR::Hanjin;
2 # ABSTRACT: Parcel::Track driver for the Hanjin (한진택배)
3  
4 use utf8;
5  
6 use Moo;
7  
8 our $VERSION = '0.001';
9  
10 with 'Parcel::Track::Role::Base';
11  
12 use Encode;
13 use HTML::Selector::XPath;
14 use HTML::TreeBuilder::XPath;
15 use HTTP::Tiny;
16  
17 #
18 # to support HTTPS
19 #
20 use IO::Socket::SSL;
21 use Mozilla::CA;
22 use Net::SSLeay;
23  
24 our $URI =
25 'https://www.hanjin.co.kr/delivery_html/inquiry/result_waybill.jsp?wbl_num=%s';
26  
27 our $AGENT = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
28  
29 sub BUILDARGS {
30 my ( $class, @args ) = @_;
31  
32 my %params;
33 if ( ref $args[0] eq 'HASH' ) {
34 %params = %{ $args[0] };
35 }
36 else {
37 %params = @args;
38 }
39 $params{id} =~ s/\D//g;
40  
41 return \%params;
42 }
43  
44 sub uri { sprintf( $URI, $_[0]->id ) }
45  
46 sub track {
47 my $self = shift;
48  
49 my %result = (
50 from => q{},
51 to => q{},
52 result => q{},
53 htmls => [],
54 descs => [],
55 );
56  
57 my $res = HTTP::Tiny->new( agent => $AGENT )->get( $self->uri );
58 unless ( $res->{success} ) {
59 $result{result} = 'failed to get parcel tracking info from the site';
60 return \%result;
61 }
62  
63 my $content = Encode::encode( 'utf-8', Encode::decode( 'cp949', $res->{content} ) );
64  
65 unless ($content) {
66 $result{result} = 'failed to tracking parcel info';
67 return \%result;
68 }
69  
70 #
71 # http://stackoverflow.com/questions/19703341/disabling-html-entities-expanding-in-htmltreebuilder-perl-module
72 #
73 my $tree = HTML::TreeBuilder::XPath->new;
74 $tree->ignore_unknown(0);
75 $tree->no_expand_entities(1);
76 $tree->attr_encoded(1);
77 $tree->parse($content);
78 $tree->eof;
79  
80 my ( $html1, $html2 ) = $tree->findnodes("//table");
81 unless ( $html1 && $html2 ) {
82 $result{result} = 'cannot find such parcel info';
83 return \%result;
84 }
85  
86 my $not_found = ( $tree->findnodes("//div[\@id='result_error']") )[0];
87 if ($not_found) {
88 $result{result} = 'cannot find such parcel info';
89 return \%result;
90 }
91  
92 $result{from} = $html1->findvalue("./tbody/tr/td[4]");
93 $result{to} = $html1->findvalue("./tbody/tr/td[5]");
94 $result{htmls} = [ $html1->as_HTML, $html2->as_HTML ];
95  
96 my @elements = $html2->findnodes("./tbody/tr");
97 for my $e (@elements) {
98 my @tds = $e->look_down( '_tag', 'td' );
99 my @td_texts = map $self->_filter_text( $_->as_text ), @tds;
100  
101 next unless @tds == 5;
102  
103 push @{ $result{descs} }, $self->_filter_text( join( q{ }, @td_texts ) );
104 $result{result} = $result{descs}->[-1];
105 }
106  
107 return \%result;
108 }
109  
110 sub _filter_text {
111 my ( $self, $text ) = @_;
112  
113 $text =~ s/(^\s+|\s+$)//gms;
114 $text =~ s/ +/ /gms;
115 $text =~ s/^-$//;
116  
117 return $text;
118 }
119  
120 1;
121  
122 #
123 # This file is part of Parcel-Track-KR-Hanjin
124 #
125 # This software is copyright (c) 2015 by Keedi Kim.
126 #
127 # This is free software; you can redistribute it and/or modify it under
128 # the same terms as the Perl 5 programming language system itself.
129 #
130  
131 __END__