#!/usr/bin/perl -w use Mojolicious::Lite; # # / # : URI my $downloads = []; foreach my $id (0..10) { $downloads->[$id] = { 'id' => $id, 'uri' => "http://site.com/download_$id", 'name' => "Video $id", 'size' => (int(rand(10000)) + 1) * 1024 }; } # - # (create) post '/downloads/' => sub { my $self = shift; # Rails JSON # , my $params = Mojo::JSON->decode($self->req->body)->{'download'}; # id # my $id = $#{ $downloads } + 1; my $uri = $params->{'uri'} || 'http://localhost/video.mp4'; my $name = $params->{'name'} || "Video $id"; my $size = $params->{'size'} || (int(rand(10000)) + 1) * 1024; $downloads->[$id] = { 'id' => $id, 'uri' => $uri, 'name' => $name, 'size' => $size }; # $self->render_json($downloads->[$id]); }; # (index) get '/downloads' => sub { my $self = shift; $self->render_json($downloads); }; # (find/show) get '/downloads/:id' => sub { my $self = shift; my $id = $self->param('id'); if (!exists($downloads->[$id])) { # - 404 $self->render_not_found; } else { # - $self->render_json($downloads->[$id]); } }; # (update) put '/downloads/:id' => sub { my $self = shift; my $params = Mojo::JSON->decode($self->req->body)->{'download'}; my $id = $self->param('id'); my $uri = $params->{'uri'} || 'http://localhost/video.mp4'; my $name = $params->{'name'} || "Video $id"; my $size = $params->{'size'} || (int(rand(10000)) + 1) * 1024; if (!exists($downloads->[$id])) { $self->render_not_found; } else { $downloads->[$id] = { 'id' => $id, 'uri' => $uri, 'name' => $name, 'size' => $size }; $self->render_json($downloads->[$id]); } }; # (delete) del '/downloads/:id' => sub { my $self = shift; my $id = $self->param('id'); if (!exists($downloads->[$id])) { $self->render_not_found; } else { delete $downloads->[$id]; # HTTP 200 OK - $self->rendered; } }; # - post '/downloads/:id/start' => sub { my $self = shift; my $id = $self->param('id'); if (!exists($downloads->[$id])) { $self->render_not_found; } else { $self->rendered; } }; # app->start;
./restful-server.pl daemon --listen=http://*:3001
class Download < ActiveResource::Base # Perl- self.site = 'http://localhost:3001' end
> Download.find(:all) => [#<Download:0x00000004b77060 @attributes={"name"=>"Video 0", "id"=>"0", "size"=>7654400, "uri"=>"http://site.com/download_0"}, @prefix_options={}, @persisted=true>, #<Download:0x0000000446f740 @attributes={"name"=>"Video 1", "id"=>"1", "size"=>8672256, "uri"=>"http://site.com/download_1"}, @prefix_options={}, @persisted=true>, #<Download:0x0000000446d300 @attributes={"name"=>"Video 2", "id"=>"2", "size"=>5931008, "uri"=>"http://site.com/download_2"}, @prefix_options={}, @persisted=true>, #<Download:0x0000000446c888 @attributes={"name"=>"Video 3", "id"=>"3", "size"=>2273280, "uri"=>"http://site.com/download_3"}, @prefix_options={}, @persisted=true>, #<Download:0x000000045c7c50 @attributes={"name"=>"Video 4", "id"=>"4", "size"=>8466432, "uri"=>"http://site.com/download_4"}, @prefix_options={}, @persisted=true>, #<Download:0x000000045c6ee0 @attributes={"name"=>"Video 5", "id"=>"5", "size"=>7057408, "uri"=>"http://site.com/download_5"}, @prefix_options={}, @persisted=true>, #<Download:0x000000045c5d60 @attributes={"name"=>"Video 6", "id"=>"6", "size"=>2351104, "uri"=>"http://site.com/download_6"}, @prefix_options={}, @persisted=true>, #<Download:0x00000004116058 @attributes={"name"=>"Video 7", "id"=>"7", "size"=>5640192, "uri"=>"http://site.com/download_7"}, @prefix_options={}, @persisted=true>, #<Download:0x00000004114320 @attributes={"name"=>"Video 8", "id"=>"8", "size"=>9701376, "uri"=>"http://site.com/download_8"}, @prefix_options={}, @persisted=true>, #<Download:0x0000000411b080 @attributes={"name"=>"Video 9", "id"=>"9", "size"=>9717760, "uri"=>"http://site.com/download_9"}, @prefix_options={}, @persisted=true>, #<Download:0x00000004a46330 @attributes={"name"=>"Video 10", "id"=>"10", "size"=>6734848, "uri"=>"http://site.com/download_10"}, @prefix_options={}, @persisted=true>]
> Download.find(5) => #<Download:0x00000004aa5420 @attributes={"name"=>"Video 5", "id"=>"5", "size"=>7057408, "uri"=>"http://site.com/download_5"}, @prefix_options={}, @persisted=true>
> Download.find(100) ActiveResource::ResourceNotFound: Failed. Response code = 404. Response message = Not Found.
> download = Download.new => #<Download:0x00000004802380 @attributes={}, @prefix_options={}, @persisted=false> > download.name = "New Video" => "New Video" > download.uri = "http://site.com/video.mp4" => "http://site.com/video.mp4" > download.size = 23452363 => 23452363 > download.save => true > Download.last => #<Download:0x000000049408f0 @attributes={"name"=>"New Video", "id"=>11, "size"=>23452363, "uri"=>"http://site.com/video.mp4"}, @prefix_options={}, @persisted=true>
> download = Download.find(5) => #<Download:0x0000000473ee30 @attributes={"name"=>"Video 5", "id"=>"5", "size"=>7057408, "uri"=>"http://site.com/download_5"}, @prefix_options={}, @persisted=true> > download.name = "New Video 5" => "New Video 5" > download.save => true > Download.find(5) => #<Download:0x000000043dade8 @attributes={"name"=>"New Video 5", "id"=>"5", "size"=>7057408, "uri"=>"http://site.com/download_5"}, @prefix_options={}, @persisted=true>
> Download.find(5).destroy => #<Net::HTTPOK 200 OK readbody=true> > Download.find(5) ActiveResource::ResourceNotFound: Failed. Response code = 404. Response message = Not Found.
> Download.find(1).post(:start) => #<Net::HTTPOK 200 OK readbody=true>
Source: https://habr.com/ru/post/148492/
All Articles