| 1 | # ABSTRACT: ponapi demo running command line utility | ||
| 2 | package PONAPI::CLI::Command::demo; | ||
| 3 | |||
| 4 | use PONAPI::CLI -command; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | sub abstract { "Run a DEMO PONAPI server" } | ||
| 10 | sub description { "This tool will run a demo server with a mock DB" } | ||
| 11 | |||
| 12 | sub opt_spec { | ||
| 13 | return ( | ||
| 14 | [ "s|server", "Run a local PONAPI demo server" ], | ||
| 15 | [ "q|query:s", "Send a random/provided query to local server" ], | ||
| 16 | [ "p|port=i", "Specify a port for the server (default=5000)" ], | ||
| 17 | ); | ||
| 18 | } | ||
| 19 | |||
| 20 | sub validate_args { | ||
| 21 | my ( $self, $opt, $args ) = @_; | ||
| 22 | |||
| 23 | $self->usage_error("(only) one of server (-s) or query (-q [STR]) is required.\n") | ||
| 24 | unless exists $opt->{s} xor exists $opt->{q}; | ||
| 25 | |||
| 26 | $self->{port} = $opt->{port} || $opt->{p} || 5000; | ||
| 27 | |||
| 28 | $self->{query_string} = ""; | ||
| 29 | |||
| 30 | if ( exists $opt->{q} and $opt->{q} ) { | ||
| 31 | $opt->{q} =~ s|^/||; | ||
| 32 | $self->{query_string} = | ||
| 33 | ( $opt->{q} !~ /^http/ ? 'http://localhost:' . $self->{port} . '/' : '' ) | ||
| 34 | . $opt->{q} | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | sub execute { | ||
| 39 | my ( $self, $opt, $args ) = @_; | ||
| 40 | |||
| 41 | $self->run_server() if exists $opt->{s}; | ||
| 42 | $self->run_query() if exists $opt->{q}; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub run_server { | ||
| 46 | my $self = shift; | ||
| 47 | |||
| 48 | require Plack::Runner; | ||
| 49 | require Plack::Middleware::MethodOverride; | ||
| 50 | require PONAPI::Server; | ||
| 51 | |||
| 52 | require File::Temp; | ||
| 53 | File::Temp->import('tempdir'); | ||
| 54 | |||
| 55 | require Path::Class; | ||
| 56 | Path::Class->import('file'); | ||
| 57 | |||
| 58 | my $dir = tempdir( CLEANUP => 1 ); | ||
| 59 | my $conf = file( $dir . '/server.yml' ); | ||
| 60 | $conf->spew(<<"DEFAULT_CONF"); | ||
| 61 | server: | ||
| 62 | spec_version: "1.0" | ||
| 63 | sort_allowed: "false" | ||
| 64 | send_version_header: "true" | ||
| 65 | send_document_self_link: "true" | ||
| 66 | links_type: "relative" | ||
| 67 | respond_to_updates_with_200: "false" | ||
| 68 | |||
| 69 | repository: | ||
| 70 | class: "Test::PONAPI::Repository::MockDB" | ||
| 71 | args: [] | ||
| 72 | DEFAULT_CONF | ||
| 73 | |||
| 74 | my $app = Plack::Middleware::MethodOverride->wrap( | ||
| 75 | PONAPI::Server->new( | ||
| 76 | 'repository.class' => 'Test::PONAPI::Repository::MockDB', | ||
| 77 | 'ponapi.config_dir' => $dir | ||
| 78 | )->to_app() | ||
| 79 | ); | ||
| 80 | |||
| 81 | my $runner = Plack::Runner->new; | ||
| 82 | $runner->parse_options( '-port', $self->{port} ); | ||
| 83 | $runner->run($app); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub run_query { | ||
| 87 | my $self = shift; | ||
| 88 | |||
| 89 | require JSON::XS; | ||
| 90 | require HTTP::Tiny; | ||
| 91 | |||
| 92 | my $url = $self->{query_string} || $self->random_url(); | ||
| 93 | |||
| 94 | my $res = HTTP::Tiny->new->get( $url, { | ||
| 95 | headers => { | ||
| 96 | 'Content-Type' => 'application/vnd.api+json', | ||
| 97 | }, | ||
| 98 | }); | ||
| 99 | |||
| 100 | die "Failed to connect to a local server (try 'ponapi demo -s' to start one)\n" | ||
| 101 | unless $res and ref($res) eq 'HASH' and $res->{status} < 500; | ||
| 102 | |||
| 103 | print "\nGET $url\n\n"; | ||
| 104 | print $res->{protocol} . " " . $res->{status} . " " . $res->{reason} . "\n"; | ||
| 105 | |||
| 106 | print "Content-Type: " . $res->{headers}{'content-type'} . "\n\n"; | ||
| 107 | |||
| 108 | my $json = JSON::XS->new; | ||
| 109 | print $json->pretty(1)->encode( $json->decode($res->{content}) ); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub random_url { | ||
| 113 | my $self = shift; | ||
| 114 | |||
| 115 | my %rels = ( | ||
| 116 | articles => { | ||
| 117 | id => [ 1, 2, 3 ], | ||
| 118 | include => [qw< comments authors >], | ||
| 119 | fields => [qw< body title created updated status >], | ||
| 120 | }, | ||
| 121 | comments => { | ||
| 122 | id => [ 5, 12 ], | ||
| 123 | include => [qw< articles >], | ||
| 124 | }, | ||
| 125 | people => { | ||
| 126 | id => [ 42, 88, 91 ], | ||
| 127 | include => [qw< articles >], | ||
| 128 | fields => [qw< name age gender >], | ||
| 129 | }, | ||
| 130 | ); | ||
| 131 | |||
| 132 | my $type = ( keys %rels )[ int(rand( scalar keys %rels )) ]; | ||
| 133 | |||
| 134 | my $id = ""; | ||
| 135 | if ( int(rand(2)) % 2 == 0 ) { | ||
| 136 | my $_id = $rels{$type}{id}->[ int(rand(scalar @{ $rels{$type}{id} } )) ]; | ||
| 137 | $id = "/$_id"; | ||
| 138 | } | ||
| 139 | |||
| 140 | my $include = ""; | ||
| 141 | my @include; | ||
| 142 | if ( int(rand(2)) % 2 == 0 ) { | ||
| 143 | my @_inc = @{ $rels{$type}{include} }; | ||
| 144 | @include = scalar @_inc > 1 | ||
| 145 | ? map { int(rand(2)) % 2 ? $_ : () } @_inc | ||
| 146 | : @_inc; | ||
| 147 | |||
| 148 | $include = "include=" . ( join ',' => @include ) | ||
| 149 | if @include; | ||
| 150 | } | ||
| 151 | |||
| 152 | my $fields = ""; | ||
| 153 | if ( int(rand(2)) % 2 == 0 ) { | ||
| 154 | my @fields = map { int(rand(2)) % 2 ? $_ : () } @{ $rels{$type}{fields} }; | ||
| 155 | $fields = "fields[$type]=" . ( join ',' => @fields, @include ) | ||
| 156 | if @fields; | ||
| 157 | } | ||
| 158 | |||
| 159 | my $query = ( $include || $fields ? "?" : "" ); | ||
| 160 | my $sep = ( $include && $fields ? "&" : "" ); | ||
| 161 | |||
| 162 | my $url = 'http://localhost:' . $self->{port} | ||
| 163 | . "/$type" . $id . $query . $include . $sep . $fields; | ||
| 164 | |||
| 165 | return $url; | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; | ||
| 169 | |||
| 170 | __END__ | ||
| 171 | |||
| 172 | =pod | ||
| 173 | |||
| 174 | =encoding UTF-8 | ||
| 175 | |||
| 176 | =head1 NAME | ||
| 177 | |||
| 178 | PONAPI::CLI::Command::demo - ponapi demo running command line utility | ||
| 179 | |||
| 180 | =head1 VERSION | ||
| 181 | |||
| 182 | version 0.002012 | ||
| 183 | |||
| 184 | =head1 AUTHORS | ||
| 185 | |||
| 186 | =over 4 | ||
| 187 | |||
| 188 | =item * | ||
| 189 | |||
| 190 | Mickey Nasriachi <mickey@cpan.org> | ||
| 191 | |||
| 192 | =item * | ||
| 193 | |||
| 194 | Stevan Little <stevan@cpan.org> | ||
| 195 | |||
| 196 | =item * | ||
| 197 | |||
| 198 | Brian Fraser <hugmeir@cpan.org> | ||
| 199 | |||
| 200 | =back | ||
| 201 | |||
| 202 | =head1 COPYRIGHT AND LICENSE | ||
| 203 | |||
| 204 | This software is copyright (c) 2016 by Mickey Nasriachi, Stevan Little, Brian Fraser. | ||
| 205 | |||
| 206 | This is free software; you can redistribute it and/or modify it under | ||
| 207 | the same terms as the Perl 5 programming language system itself. | ||
| 208 | |||
| 209 | =cut |