File Coverage

blib/lib/WWW/GudangAPI.pm
Criterion Covered Total %
statement 18 33 54.5
branch 0 16 0.0
condition 0 2 0.0
subroutine 6 7 85.7
pod 0 1 0.0
total 24 59 40.6


line stmt bran cond sub pod time code
1             package WWW::GudangAPI;
2              
3 1     1   27755 use 5.010;
  1         4  
  1         38  
4 1     1   6 use strict;
  1         2  
  1         44  
5 1     1   8 use warnings;
  1         2  
  1         45  
6 1     1   811 use Exporter::Lite;
  1         829  
  1         6  
7 1     1   2768 use Log::Any '$log';
  1         2220  
  1         6  
8 1     1   1044 use Perinci::Access;
  1         5018  
  1         641  
9              
10             our @EXPORT_OK = qw(get_ga_ssuri);
11             our %SPEC;
12             our $VERSION = '0.04'; # VERSION
13              
14             $SPEC{call_ga} = {
15             v => 1.1,
16             summary =>
17             'Call GudangAPI.com API functions',
18             description => <<'_',
19              
20             Note that GudangAPI.com is a Riap server, so you can use any Riap client to
21             access it.
22              
23             _
24             args => {
25             module => {
26             req => 1,
27             pos => 0,
28             schema => ['str*' => {
29             match => qr!^\w+(?:::\w+)*$!,
30             }],
31             summary => 'Name of module to call',
32             },
33             func => {
34             req => 1,
35             pos => 1,
36             schema => ['str*' => {
37             match => qr/^\w+$/,
38             }],
39             summary => 'Name of function to call',
40             },
41             user => {
42             schema => ['str' => {
43             default => 'ga',
44             }],
45             summary => 'GudangAPI username',
46             },
47             args => {
48             schema => ['hash*' => {
49             default => {},
50             }],
51             summary => 'Function arguments',
52             },
53             https => {
54             schema => ['bool' => {
55             default => 0,
56             }],
57             summary => 'Whether to use HTTPS instead of HTTP',
58             description => <<'_',
59              
60             You might want to use HTTPS if you send sensitive data such as password or
61             financial data. Note that HTTPS access has higher latency.
62              
63             _
64             },
65             },
66             };
67             sub call_ga {
68 0     0 0   my %args = @_;
69              
70 0           state $pa = Perinci::Access->new;
71              
72             # XXX schema
73              
74 0           my $user = $args{user};
75 0 0         if (defined $user) {
76 0 0         $user =~ /\A\w+\z/
77             or return [400, "Invalid user `$user`: use alphanums only"];
78             }
79 0   0       $user //= "ga";
80              
81 0 0         my $module = $args{module}
82             or return [400, "Please specify module"];
83 0 0         $module =~ m!\A\w+(?:::\w+)*\z!
84             or return [400, "Invalid module `$module`: use 'foo::bar' syntax"];
85              
86 0           my $func = $args{func};
87 0 0         if (defined $func) {
88 0 0         $func =~ /\A\w+\z/
89             or return [400, "Invalid sub: use alphanums only"];
90             }
91 0           my $https = $args{https};
92              
93 0 0         my $url = join("",
    0          
94             ($https ? "https" : "http"), "://",
95             "gudangapi.com/",
96             $user,
97             "/$module",
98             (defined($func) ? "::$func" : "")
99             );
100 0           $log->tracef("url=%s", $url);
101 0           $pa->request(call => $url, {args=>});
102             }
103              
104             1;
105              
106              
107             =pod
108              
109             =head1 NAME
110              
111             WWW::GudangAPI
112              
113             =head1 VERSION
114              
115             version 0.04
116              
117             =head1 SYNOPSIS
118              
119             use WWW::GudangAPI qw(call_ga);
120             my $uri = call_ga(
121             module => 'tax/id/npwp',
122             func => 'parse_npwp',
123             #https => 1, # use https, default is 0
124             args => {npwp=>'00.000.001.8-000'}
125             );
126             my $res = $uri->call(npwp=>'00.000.001.8-000');
127             say "valid!" if $res->[0] == 200; # prints 'valid!'
128              
129             =head1 DESCRIPTION
130              
131             This module is the Perl client library for GudangAPI,
132             L. It is currently a very thin (and probably pretty
133             useless) wrapper for L, since GudangAPI is L-compliant.
134             As a matter of fact, you can just do:
135              
136             my $pa = Perinci::Access->new;
137             my $res = $pa->request(call => "http://gudangapi.com/ga/MODULE::FUNC",
138             {args=>{ARG=>...}});
139              
140             and skip this module altogether. But in the future some convenience features
141             will be added to this module.
142              
143             This module uses L.
144              
145             This module has L metadata.
146              
147             =head1 SEE ALSO
148              
149             L
150              
151             L
152              
153             http://www.gudangapi.com/
154              
155             =head1 AUTHOR
156              
157             Steven Haryanto
158              
159             =head1 COPYRIGHT AND LICENSE
160              
161             This software is copyright (c) 2012 by Steven Haryanto.
162              
163             This is free software; you can redistribute it and/or modify it under
164             the same terms as the Perl 5 programming language system itself.
165              
166             =cut
167              
168              
169             __END__