lib/Sorauta/Utility.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 #============================================
2 # 便利関数群
3 # 基本的に全関数、exportして使う
4 # BASIC認証については、必要に応じてパッケージ変数(BASIC_AUTH_LIST)を上書きする
5 # -------------------------------------------
6 # アクセサ
7 # xxx_xxx String xxxxxx
8 # デフォルトは$XXX_XXX
9 #============================================
10 package Sorauta::Utility;
11 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use 5.012003;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 0 ▶use CGI::Carp qw/fatalsToBrowser/;
Subroutine BEGIN: called CC=1 SCAR=2.6
18 use Data::Dumper;
19 use LWP::UserAgent;
20 use Time::Local; # for timegm
21  
22 our @ISA = qw(Exporter);
23  
24 # Items to export into callers namespace by default. Note: do not export
25 # names by default without a very good reason. Use EXPORT_OK instead.
26 # Do not simply export all your public functions/methods/constants.
27  
28 # This allows declaration use Sorauta::Utility ':all';
29 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
30 # will save memory.
31 our %EXPORT_TAGS = ( 'all' => [ qw(
32  
33 ) ] );
34  
35 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
36  
37 # 利用可能なサブルーチン一覧
38 our @EXPORT = qw/get_from_http save_file create_get_url get_timestamp get_date get_epoch_from_formated_http cat is_hidden_file is_unnecessary_copying_file/;
39  
40 our $VERSION = '0.02';
41  
42 # Preloaded methods go here.
43  
44 # ベーシック認証情報
45 # override: %Sorauta::Utility::BASIC_AUTH_LIST = ();
46 our %BASIC_AUTH_LIST = (
47 #'host.example.com' => {
48 # id => 'user_id',
49 # pass => 'user_pass',
50 #},
51 );
52  
53 # HTTPリクエストのタイムアウト時間
54 our $TIMEOUT = 20;
55  
56 # HTTPリクエスト失敗時のリトライ回数
57 our $RETRY_COUNT = 1;
58  
59 # デバッグ出力するか
60 our $DEBUG = 0;
61  
62 #==========================================
63 # httpリクエスト送る
64 # req:
65 # url : リクエスト先のURL
66 # timeout : リクエストのタイムアウト時間
67 # retry_count: リクエスト失敗時にリトライする回数
68 # res:
69 # response: LWP::UserAgentのレスポンスオブジェクト
70 #==========================================
71 sub get_from_http {
72 my($url, $timeout, $retry_count) = @_;
73 unless ($timeout) {
74 $timeout = $TIMEOUT;
75 }
76 unless ($retry_count) {
77 $retry_count = $RETRY_COUNT;
78 }
79  
80 if ($DEBUG) {
81 print '[Sorauta::Utility][get_from_http]', $url, $/;
82 }
83  
84 # create agent
85 my $ua = LWP::UserAgent->new(
86 );
87 $ua->timeout($timeout);
88  
89 # create request
90 my $req = HTTP::Request->new(
91 GET => $url
92 );
93 while (my($key, $basic_auth) = each(%BASIC_AUTH_LIST)) {
94 if ($url =~ /$key/) {
95 $req->authorization_basic($basic_auth->{id}, $basic_auth->{pass});
96 last;
97 }
98 }
99  
100 my $try_count = 0;
101 my $response;
102 while ($try_count++ < $retry_count) {
103 $response = $ua->request($req);
104 if ($response->is_success) {
105 last;
106 }
107 else {
108 if ($DEBUG) {
109 print '[Sorauta::Utility][get_from_http]', $url, ' got ', $response->status_line, ' ... retry', $/;
110 }
111 }
112 }
113  
114 return $response;
115 }
116  
117 #==========================================
118 # ファイルを保存
119 # req:
120 # path: 保存ファイルのパス
121 # content: ファイルの内容
122 # is_binary: バイナリの場合は1、それ以外は0
123 # res:
124 # result: 成功時は1、失敗時は2
125 #==========================================
126 sub save_file {
127 my($path, $content, $is_binary) = @_;
128  
129 if ($DEBUG) {
130 print "[Sorauta::Utility][save_file]", $path, $/;
131 }
132  
133 open(my $F, '>', $path) or die 'Can\'t open('.$path.'): '.$!;
134 if ($is_binary) {
135 binmode($F);
136 }
137 print $F $content;
138 close $F;
139 }
140  
141 #==========================================
142 # パラメータをGET引数に変換
143 # ※文字列エスケープ等は引数に渡す前に既に行っている前提
144 # req:
145 # url: 元になるURL
146 # ex) http://localhost/index.pl
147 # params: 引数をハッシュで表現
148 # ex) { id => 1, name => "test" }
149 # res:
150 # url_str: 文字列化されたURL
151 # ex) http://localhost/index.pl?id=1&name=test
152 #==========================================
153 sub create_get_url {
154 my($url, $params) = @_;
155  
156 # GET引数に変換
157 my $get_param = q{};
158 while (my($key, $val) = each(%$params)) {
159 $get_param .= $key . '=' . $val . '&';
160 }
161  
162 return $url . '?' . $get_param;
163 }
164  
165 #==========================================
166 # 指定した時刻をタイムスタンプで取得
167 # req:
168 # unix_time: 取得したい時刻のエポックタイム、指定しなければ現在時刻
169 # res:
170 # timestamp: タイムスタンプを文字列に変換したもの
171 #==========================================
172 sub get_timestamp {
173 my $time = shift || time;
174 my($sec, $min, $hour, $day, $month, $year) = (localtime($time))[0..5];
175  
176 return sprintf("%04d/%02d/%02d %02d:%02d:%02d",
177 $year + 1900, $month + 1, $day, $hour, $min, $sec);
178 }
179  
180 #------------------------------------------
181 # 時刻情報取得
182 # req:
183 # unix_time: 指定したい時刻のエポックタイム、指定しなければ現在時刻
184 # res:
185 # date_obj: 時刻情報をハッシュリファレンスにしたもの
186 #------------------------------------------
187 sub get_date {
188 my $time = shift || time;
189 my($sec, $min, $hour, $day, $month, $year) = (localtime($time))[0..5];
190  
191 return {
192 year => $year + 1900,
193 month => $month + 1,
194 day => $day,
195 hour => $hour,
196 min => $min,
197 sec => $sec,
198 };
199 }
200  
201 #==========================================
202 # http経由で取得した時間からエポックタイムを抽出
203 # req:
204 # date_str: httpリクエストのヘッダにあるlast-modified等
205 # ex)"Fri, 13 Jan 2012 23:49:21 GMT"
206 # res:
207 # unix_time: エポックタイム
208 #==========================================
209 sub get_epoch_from_formated_http {
210 my(@MoY, %MoY);
211 @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
212 @MoY{@MoY} = (1..12);
213  
214 if ($_[0] =~ /^
215 [SMTWF][a-z][a-z],
216 \ (\d\d)
217 \ ([JFMAJSOND][a-z][a-z])
218 \ (\d\d\d\d)
219 \ (\d\d):(\d\d):(\d\d)
220 \ GMT$/x) {
221 return timegm($6, $5, $4, $1, $MoY{$2} - 1, $3 - 1900);
222 }
223 }
224  
225 #==========================================
226 # パスを配列で表現したものから文字列のパスを作る
227 # req:
228 # path_list: ('/Users', 'user', 'Desktop', 'Hoge.txt')
229 # res:
230 # path_str: '/Users/user/Desktop/Hoge.txt'
231 #==========================================
232 sub cat {
233 #return join('/', @_);
234 return File::Spec->catfile(@_);
235 }
236  
237 #==========================================
238 # 隠しファイルか判定
239 # is_skip_file
240 # req:
241 # file_path: ファイルまでのパス
242 # ex) /Users/user/Desktop/.svn
243 # res:
244 # result: 隠しファイルの場合は1、それ以外は0
245 #==========================================
246 sub is_hidden_file {
247 my $file_path = shift;
248  
249 return 1 if(
250 $file_path eq '.' ||
251 $file_path eq '..' ||
252 $file_path =~ /.svn$/ ||
253 $file_path =~ /desktop\.ini$/ ||
254 $file_path =~ /.DS_Store$/ ||
255 $file_path =~ /Thumbs\.db$/
256 );
257  
258 return 0;
259 }
260  
261 #------------------------------------------
262 # コピー不要なファイルか
263 # copy_filter
264 # req:
265 # file_name: ファイル名
266 # ex).svn, desktop.ini
267 # res:
268 # result: コピーすべきファイルの場合は0、それ以外は1
269 #------------------------------------------
270 sub is_unnecessary_copying_file {
271 my $file_name = shift;
272  
273 # .や..等,階層を指定している場合は無視
274 if ($file_name =~ /^\./) {
275 return 1;
276 }
277 # サムネイル用ファイルなどは無視
278 if ($file_name eq 'Thumbs.db' || !($file_name =~ /[^\.]/)) {
279 return 1;
280 }
281 # .svnはコピーしたくない
282 if ($file_name eq '.svn' || $file_name eq 'desktop.ini' || $file_name eq '.DS_Store') {
283 return 1;
284 }
285  
286 # ログファイルのディレクトリは無視
287 if ($file_name eq 'log') {
288 return 1;
289 }
290  
291 return 0;
292 }
293  
294 1;
295 __END__
↑ Summary