Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
東京と大阪の天気情報を取る
#!/usr/bin/env perl
use Mojolicious::Lite;
use HTTP::Tiny;
use JSON::PP;
my $json;
my $location_list = [ [ '東京' => '130010' ], [ '大阪' => '270000' ] ];
get '/' => sub {
my $c = shift;
$c->stash( location_list => $location_list );
$c->render( template => 'index' );
};
get '/location/:location' => sub {
my $c = shift;
$c->stash( location_list => $location_list, livedoor_weather => $json );
$c->render( template => 'index' );
};
post '/' => sub {
my $c = shift;
my $location_num = $c->param('location');
$json = weather($location_num);
$c->redirect_to( '/location/' . $location_num );
};
sub weather {
my $location = shift;
my $url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city='
. $location;
my $response = HTTP::Tiny->new->get($url);
return decode_json( $response->{content} );
}
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Welcome';
<h1>Welcome to the Mojolicious real-time web framework!</h1>
<%= form_for '/' => (method => 'POST') => begin %>
<%= select_field location => $location_list %>
<%= submit_button %>
<% end %>
<div>
<% unless ($livedoor_weather eq '') { %>
場所:<%= $livedoor_weather->{location}->{city} %><br>
天気:<%= $livedoor_weather->{forecasts}->[0]->{telop} %><br>
最高気温:<%= $livedoor_weather->{forecasts}->[0]->{temperature}->{max}->{celsius} %><br>
<% } %>
</div>
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.