| 1 | package HTTP::Command::Wrapper; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | |||
| 6 | our $VERSION = "0.07"; | ||
| 7 | |||
| 8 | use File::Which qw/which/; | ||
| 9 | |||
| 10 | use HTTP::Command::Wrapper::Curl; | ||
| 11 | use HTTP::Command::Wrapper::Wget; | ||
| 12 | |||
| 13 | sub create { | ||
| 14 | my $class = shift; | ||
| 15 | my $opt = {}; | ||
| 16 | my $type = undef; | ||
| 17 | |||
| 18 | $opt = $_[0] if ref $_[0] eq 'HASH'; | ||
| 19 | $opt = $_[1] if ref $_[1] eq 'HASH'; | ||
| 20 | |||
| 21 | $type = $_[0] unless ref $_[0]; | ||
| 22 | $type = $class->_detect_type unless defined $type; | ||
| 23 | |||
| 24 | return HTTP::Command::Wrapper::Curl->new($opt) if $type eq 'curl'; | ||
| 25 | return HTTP::Command::Wrapper::Wget->new($opt) if $type eq 'wget'; | ||
| 26 | |||
| 27 | die 'Command not detected (curl or wget)'; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub _detect_type { | ||
| 31 | my $class = shift; | ||
| 32 | |||
| 33 | return 'curl' if $class->_which('curl'); | ||
| 34 | return 'wget' if $class->_which('wget'); | ||
| 35 | return undef; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _which { which($_[1]) } | ||
| 39 | |||
| 40 | 1; | ||
| 41 | __END__ | ||
| 42 | |||
| 43 | =encoding utf-8 | ||
| 44 | |||
| 45 | =head1 NAME | ||
| 46 | |||
| 47 | HTTP::Command::Wrapper - The command based HTTP client (wget/curl wrapper). Too minimum dependencies! | ||
| 48 | |||
| 49 | =head1 SYNOPSIS | ||
| 50 | |||
| 51 | use HTTP::Command::Wrapper; | ||
| 52 | |||
| 53 | my $client = HTTP::Command::Wrapper->create; # auto detecting (curl or wget) | ||
| 54 | my $content = $client->fetch('https://github.com/'); | ||
| 55 | |||
| 56 | print "$content\n"; | ||
| 57 | |||
| 58 | =head1 DESCRIPTION | ||
| 59 | |||
| 60 | HTTP::Command::Wrapper is a very simple HTTP client module. | ||
| 61 | It can wrap C<wget> or C<curl> command, and can use same interface. | ||
| 62 | |||
| 63 | =head1 REQUIREMENTS | ||
| 64 | |||
| 65 | C<wget> or C<curl> | ||
| 66 | |||
| 67 | =head1 METHODS | ||
| 68 | |||
| 69 | =head2 CLASS METHODS | ||
| 70 | |||
| 71 | =head3 C<create($options = {})> | ||
| 72 | |||
| 73 | Create new wrapper instance using automatic commands detecting. | ||
| 74 | |||
| 75 | =head3 C<create($type, $options = {})> | ||
| 76 | |||
| 77 | Create new wrapper instance. C<'wget'> or C<'curl'> can be specified as C<$type> value. | ||
| 78 | |||
| 79 | =head4 C<$options> | ||
| 80 | |||
| 81 | =over | ||
| 82 | |||
| 83 | =item C<verbose =E<gt> 1> | ||
| 84 | |||
| 85 | Turn on verbose output, with all the available data. | ||
| 86 | |||
| 87 | =item C<quiet =E<gt> 1> | ||
| 88 | |||
| 89 | Turn off output. | ||
| 90 | |||
| 91 | =back | ||
| 92 | |||
| 93 | =head2 METHODS | ||
| 94 | |||
| 95 | =head3 C<fetch($url, $headers = [])> | ||
| 96 | |||
| 97 | Fetch http/https contents from C<$url>. Return a content body as string. | ||
| 98 | |||
| 99 | =head3 C<fetch_able($url, $headers = [])> | ||
| 100 | |||
| 101 | Return true if C<$url> contents can fetch (status code is C<200>). | ||
| 102 | |||
| 103 | =head3 C<download($url, $path, $headers = [])> | ||
| 104 | |||
| 105 | Fetch http/https contents from C<$url>. Save in file. Return process exit code as boolean. | ||
| 106 | |||
| 107 | =head1 LICENSE | ||
| 108 | |||
| 109 | The MIT License (MIT) | ||
| 110 | |||
| 111 | Copyright (c) 2015-2016 Pine Mizune | ||
| 112 | |||
| 113 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 114 | of this software and associated documentation files (the "Software"), to deal | ||
| 115 | in the Software without restriction, including without limitation the rights | ||
| 116 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 117 | copies of the Software, and to permit persons to whom the Software is | ||
| 118 | furnished to do so, subject to the following conditions: | ||
| 119 | |||
| 120 | The above copyright notice and this permission notice shall be included in | ||
| 121 | all copies or substantial portions of the Software. | ||
| 122 | |||
| 123 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 124 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 125 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 126 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 127 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 128 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 129 | THE SOFTWARE. | ||
| 130 | |||
| 131 | =head1 AUTHOR | ||
| 132 | |||
| 133 | Pine Mizune E<lt>pinemz@gmail.comE<gt> | ||
| 134 | |||
| 135 | =cut | ||
| 136 |