add files
|
1 |
package ojo; |
2 |
use Mojo::Base -strict; |
|
3 | ||
4 |
use Mojo::ByteStream 'b'; |
|
5 |
use Mojo::Collection 'c'; |
|
6 |
use Mojo::DOM; |
|
7 |
use Mojo::JSON 'j'; |
|
8 |
use Mojo::UserAgent; |
|
9 |
use Mojo::Util qw(dumper monkey_patch); |
|
10 | ||
11 |
# Silent one-liners |
|
12 |
$ENV{MOJO_LOG_LEVEL} ||= 'fatal'; |
|
13 | ||
14 |
# Singleton user agent for one-liners |
|
15 |
my $UA = Mojo::UserAgent->new; |
|
16 | ||
17 |
sub import { |
|
18 | ||
19 |
# Mojolicious::Lite |
|
20 |
my $caller = caller; |
|
21 |
eval "package $caller; use Mojolicious::Lite;"; |
|
22 |
my $server = $UA->server->app($caller->app); |
|
23 |
$server->app->hook(around_action => sub { local $_ = $_[1]; $_[0]->() }); |
|
24 | ||
25 |
$UA->max_redirects(10) unless defined $ENV{MOJO_MAX_REDIRECTS}; |
|
26 |
$UA->proxy->detect unless defined $ENV{MOJO_PROXY}; |
|
27 | ||
28 |
# The ojo DSL |
|
29 |
monkey_patch $caller, |
|
30 |
a => sub { $caller->can('any')->(@_) and return $UA->server->app }, |
|
31 |
b => \&b, |
|
32 |
c => \&c, |
|
33 |
d => sub { _request($UA->build_tx(DELETE => @_)) }, |
|
34 |
g => sub { _request($UA->build_tx(GET => @_)) }, |
|
35 |
h => sub { _request($UA->build_tx(HEAD => @_)) }, |
|
36 |
j => \&j, |
|
37 |
o => sub { _request($UA->build_tx(OPTIONS => @_)) }, |
|
38 |
p => sub { _request($UA->build_tx(POST => @_)) }, |
|
39 |
r => \&dumper, |
|
40 |
t => sub { _request($UA->build_tx(PATCH => @_)) }, |
|
41 |
u => sub { _request($UA->build_tx(PUT => @_)) }, |
|
42 |
x => sub { Mojo::DOM->new(@_) }; |
|
43 |
} |
|
44 | ||
45 |
sub _request { |
|
46 |
my $tx = $UA->start(@_); |
|
47 |
my ($err, $code) = $tx->error; |
|
48 |
warn qq/Problem loading URL "@{[$tx->req->url->to_abs]}". ($err)\n/ |
|
49 |
if $err && !$code; |
|
50 |
return $tx->res; |
|
51 |
} |
|
52 | ||
53 |
1; |
|
54 | ||
55 |
=encoding utf8 |
|
56 | ||
57 |
=head1 NAME |
|
58 | ||
59 |
ojo - Fun one-liners with Mojo! |
|
60 | ||
61 |
=head1 SYNOPSIS |
|
62 | ||
63 |
$ perl -Mojo -E 'say g("mojolicio.us")->dom->at("title")->text' |
|
64 | ||
65 |
=head1 DESCRIPTION |
|
66 | ||
67 |
A collection of automatically exported functions for fun Perl one-liners. Ten |
|
68 |
redirects will be followed by default, you can change this behavior with the |
|
69 |
MOJO_MAX_REDIRECTS environment variable. |
|
70 | ||
71 |
$ MOJO_MAX_REDIRECTS=0 perl -Mojo -E 'say g("example.com")->code' |
|
72 | ||
73 |
Proxy detection is enabled by default, but you can disable it with the |
|
74 |
MOJO_PROXY environment variable. |
|
75 | ||
76 |
$ MOJO_PROXY=0 perl -Mojo -E 'say g("example.com")->body' |
|
77 | ||
78 |
=head1 FUNCTIONS |
|
79 | ||
80 |
L<ojo> implements the following functions. |
|
81 | ||
82 |
=head2 a |
|
83 | ||
84 |
my $app = a('/hello' => sub { $_->render(json => {hello => 'world'}) }); |
|
85 | ||
86 |
Create a route with L<Mojolicious::Lite/"any"> and return the current |
|
87 |
L<Mojolicious::Lite> object. The current controller object is also available |
|
88 |
to actions as C<$_>. See also the L<Mojolicious::Lite> tutorial for more |
|
89 |
argument variations. |
|
90 | ||
91 |
$ perl -Mojo -E 'a("/hello" => {text => "Hello Mojo!"})->start' daemon |
|
92 | ||
93 |
=head2 b |
|
94 | ||
95 |
my $stream = b('lalala'); |
|
96 | ||
97 |
Turn string into a L<Mojo::ByteStream> object. |
|
98 | ||
99 |
$ perl -Mojo -E 'b(g("mojolicio.us")->body)->html_unescape->say' |
|
100 | ||
101 |
=head2 c |
|
102 | ||
103 |
my $collection = c(1, 2, 3); |
|
104 | ||
105 |
Turn list into a L<Mojo::Collection> object. |
|
106 | ||
107 |
=head2 d |
|
108 | ||
109 |
my $res = d('example.com'); |
|
110 |
my $res = d('http://example.com' => {DNT => 1} => 'Hi!'); |
|
111 | ||
112 |
Perform DELETE request with L<Mojo::UserAgent/"delete"> and return resulting |
|
113 |
L<Mojo::Message::Response> object. |
|
114 | ||
115 |
=head2 g |
|
116 | ||
117 |
my $res = g('example.com'); |
|
118 |
my $res = g('http://example.com' => {DNT => 1} => 'Hi!'); |
|
119 | ||
120 |
Perform GET request with L<Mojo::UserAgent/"get"> and return resulting |
|
121 |
L<Mojo::Message::Response> object. |
|
122 | ||
123 |
$ perl -Mojo -E 'say g("mojolicio.us")->dom("h1, h2, h3")->text' |
|
124 | ||
125 |
=head2 h |
|
126 | ||
127 |
my $res = h('example.com'); |
|
128 |
my $res = h('http://example.com' => {DNT => 1} => 'Hi!'); |
|
129 | ||
130 |
Perform HEAD request with L<Mojo::UserAgent/"head"> and return resulting |
|
131 |
L<Mojo::Message::Response> object. |
|
132 | ||
133 |
=head2 j |
|
134 | ||
135 |
my $bytes = j({foo => 'bar'}); |
|
136 |
my $array = j($bytes); |
|
137 |
my $hash = j($bytes); |
|
138 | ||
139 |
Encode Perl data structure or decode JSON with L<Mojo::JSON>. |
|
140 | ||
141 |
$ perl -Mojo -E 'b(j({hello => "world!"}))->spurt("hello.json")' |
|
142 | ||
143 |
=head2 o |
|
144 | ||
145 |
my $res = o('example.com'); |
|
146 |
my $res = o('http://example.com' => {DNT => 1} => 'Hi!'); |
|
147 | ||
148 |
Perform OPTIONS request with L<Mojo::UserAgent/"options"> and return resulting |
|
149 |
L<Mojo::Message::Response> object. |
|
150 | ||
151 |
=head2 p |
|
152 | ||
153 |
my $res = p('example.com'); |
|
154 |
my $res = p('http://example.com' => {DNT => 1} => 'Hi!'); |
|
155 | ||
156 |
Perform POST request with L<Mojo::UserAgent/"post"> and return resulting |
|
157 |
L<Mojo::Message::Response> object. |
|
158 | ||
159 |
=head2 r |
|
160 | ||
161 |
my $perl = r({data => 'structure'}); |
|
162 | ||
163 |
Dump a Perl data structure with L<Mojo::Util/"dumper">. |
|
164 | ||
165 |
perl -Mojo -E 'say r(g("example.com")->headers->to_hash)' |
|
166 | ||
167 |
=head2 t |
|
168 | ||
169 |
my $res = t('example.com'); |
|
170 |
my $res = t('http://example.com' => {DNT => 1} => 'Hi!'); |
|
171 | ||
172 |
Perform PATCH request with L<Mojo::UserAgent/"patch"> and return resulting |
|
173 |
L<Mojo::Message::Response> object. |
|
174 | ||
175 |
=head2 u |
|
176 | ||
177 |
my $res = u('example.com'); |
|
178 |
my $res = u('http://example.com' => {DNT => 1} => 'Hi!'); |
|
179 | ||
180 |
Perform PUT request with L<Mojo::UserAgent/"put"> and return resulting |
|
181 |
L<Mojo::Message::Response> object. |
|
182 | ||
183 |
=head2 x |
|
184 | ||
185 |
my $dom = x('<div>Hello!</div>'); |
|
186 | ||
187 |
Turn HTML/XML input into L<Mojo::DOM> object. |
|
188 | ||
189 |
$ perl -Mojo -E 'say x(b("test.html")->slurp)->at("title")->text' |
|
190 | ||
191 |
=head1 SEE ALSO |
|
192 | ||
193 |
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>. |
|
194 | ||
195 |
=cut |