File Coverage

blib/lib/Parcel/Track/KR/CJKorea.pm
Criterion Covered Total %
statement 57 77 74.0
branch 6 14 42.8
condition 1 3 33.3
subroutine 12 12 100.0
pod 2 3 66.6
total 78 109 71.5


line stmt bran cond sub pod time code
1             package Parcel::Track::KR::CJKorea;
2             # ABSTRACT: Parcel::Track driver for the CJ Korea Express (CJ 대한통운)
3              
4 1     1   84605 use utf8;
  1         12  
  1         4  
5              
6 1     1   26 use Moo;
  1         2  
  1         4  
7              
8             our $VERSION = '0.005';
9              
10             with 'Parcel::Track::Role::Base';
11              
12 1     1   581 use Encode;
  1         6984  
  1         60  
13 1     1   222 use File::Which;
  1         761  
  1         41  
14 1     1   294 use HTML::Selector::XPath;
  1         1883  
  1         38  
15 1     1   282 use HTML::TreeBuilder::XPath;
  1         43824  
  1         8  
16 1     1   484 use HTTP::Tiny;
  1         34931  
  1         37  
17              
18             #
19             # to support HTTPS
20             #
21 1     1   538 use IO::Socket::SSL;
  1         35282  
  1         11  
22 1     1   393 use Mozilla::CA;
  1         186  
  1         585  
23              
24             our $URI =
25             'https://www.doortodoor.co.kr/parcel/doortodoor.do?fsp_action=PARC_ACT_002&fsp_cmd=retrieveInvNoACT&invc_no=%s';
26              
27             our $AGENT = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
28              
29             sub BUILDARGS {
30 2     2 0 2346 my ( $class, @args ) = @_;
31              
32 2         4 my %params;
33 2 50       8 if ( ref $args[0] eq 'HASH' ) {
34 0         0 %params = %{ $args[0] };
  0         0  
35             }
36             else {
37 2         7 %params = @args;
38             }
39 2         5 $params{id} =~ s/\D//g;
40              
41 2         38 return \%params;
42             }
43              
44 2     2 1 47 sub uri { sprintf( $URI, $_[0]->id ) }
45              
46             sub track {
47 2     2 1 276 my $self = shift;
48              
49 2         10 my %result = (
50             from => q{},
51             to => q{},
52             result => q{},
53             htmls => [],
54             descs => [],
55             );
56              
57 2         15 my $http = HTTP::Tiny->new( agent => $AGENT );
58 2         149 my $res = $http->get( $self->uri );
59              
60 2 50       8560856 unless ( $res->{success} ) {
61 0         0 $result{result} = 'failed to get parcel tracking info from the site';
62 0         0 return \%result;
63             }
64              
65 2         16 my $content = Encode::decode_utf8( $res->{content} );
66              
67 2 50       260 unless ($content) {
68 0         0 $result{result} = 'failed to tracking parcel info';
69 0         0 return \%result;
70             }
71              
72             #
73             # http://stackoverflow.com/questions/19703341/disabling-html-entities-expanding-in-htmltreebuilder-perl-module
74             #
75 2         37 my $tree = HTML::TreeBuilder::XPath->new;
76 2         629 $tree->ignore_unknown(0);
77 2         34 $tree->no_expand_entities(1);
78 2         25 $tree->attr_encoded(1);
79 2         26 $tree->parse($content);
80 2         246835 $tree->eof;
81              
82 2         9336 my $prefix = '/html/body/div/div[2]/div/div[2]/ul/li[1]/div';
83              
84 2         24 my $html1 = ( $tree->findnodes("$prefix/div[1]/div/table") )[0];
85 2         9168 my $html2 = ( $tree->findnodes("$prefix/div[2]/div/table") )[0];
86 2 50 33     7914 unless ( $html1 && $html2 ) {
87 0         0 $result{result} = 'cannot find such parcel info';
88 0         0 return \%result;
89             }
90              
91 2         12 my $found = ( $tree->findnodes("$prefix/div[2]/div/table/tr[2]/td") )[0];
92 2 50       9283 my $found_text = $found ? $found->as_text : q{};
93 2         54 my $not_found = '조회된 데이터가 없습니다';
94 2 50       24 if ( $found_text =~ m/$not_found/ ) {
95 2         15 $result{result} = 'cannot find such parcel info';
96 2         575 return \%result;
97             }
98              
99 0           $result{from} = $tree->findvalue("$prefix/div[1]/div/table/tr[2]/td[2]");
100 0           $result{to} = $tree->findvalue("$prefix/div[1]/div/table/tr[2]/td[3]");
101 0           $result{htmls} = [ $html1->as_HTML, $html2->as_HTML ];
102              
103 0           my @elements = $tree->findnodes("$prefix/div[2]/div/table/tr");
104 0           my $row_index = 0;
105 0           for my $e (@elements) {
106 0 0         next if $row_index++ == 0;
107              
108 0           my @tds = $e->look_down( '_tag', 'td' );
109 0           push @{ $result{descs} }, join( q{ }, map $_->as_text, @tds[ 1, 0, 3, 2 ] );
  0            
110              
111 0           $result{result} = join( q{ }, map $_->as_text, @tds[ 1, 0 ] );
112             }
113              
114 0           return \%result;
115             }
116              
117             1;
118              
119             #
120             # This file is part of Parcel-Track-KR-CJKorea
121             #
122             # This software is copyright (c) 2017 by Keedi Kim.
123             #
124             # This is free software; you can redistribute it and/or modify it under
125             # the same terms as the Perl 5 programming language system itself.
126             #
127              
128             __END__