| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::GoDaddy::REST::Shell::DocsCommand; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 1574 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 38 |  | 
| 4 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 50 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  |  |  | 14 | use Sub::Exporter -setup => { | 
| 7 |  |  |  |  |  |  | exports => [qw(run_docs smry_docs help_docs comp_docs alias_docs)], | 
| 8 |  |  |  |  |  |  | groups  => { default => [qw(run_docs smry_docs help_docs comp_docs alias_docs)] } | 
| 9 | 1 |  |  | 1 |  | 4 | }; | 
|  | 1 |  |  |  |  | 2 |  | 
| 10 | 1 |  |  | 1 |  | 971 | use Text::FormatTable; | 
|  | 1 |  |  |  |  | 2782 |  | 
|  | 1 |  |  |  |  | 61 |  | 
| 11 | 1 |  |  | 1 |  | 13 | use WWW::GoDaddy::REST::Shell::Util qw(get_resource_by_schema_or_uri); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 34 |  | 
| 12 | 1 |  |  | 1 |  | 816 | use WWW::GoDaddy::REST::Util qw(json_encode); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub run_docs { | 
| 15 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 0 |  |  |  |  |  | my $usage = "Usage:\n docs [schema] [id]\n docs /uri/id\n"; | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 0 |  |  |  |  |  | my $resource; | 
| 20 | 0 | 0 | 0 |  |  |  | if ( @_ == 1 && $self->client->schema( $_[0] ) ) { | 
| 21 | 0 |  |  |  |  |  | $resource = $self->client->schema( $_[0] ); | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  | else { | 
| 24 | 0 |  |  |  |  |  | $resource = eval { return get_resource_by_schema_or_uri( $self, @_ ); }; | 
|  | 0 |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | } | 
| 26 | 0 | 0 |  |  |  |  | if ($@) { | 
| 27 | 0 | 0 |  |  |  |  | if ( UNIVERSAL::isa( $@, 'WWW::GoDaddy::REST::Resource' ) ) { | 
| 28 | 0 |  |  |  |  |  | $self->page( $@->to_string(1) . "\n" ); | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  | else { | 
| 31 | 0 |  |  |  |  |  | warn($@); | 
| 32 | 0 |  |  |  |  |  | warn($usage); | 
| 33 |  |  |  |  |  |  | } | 
| 34 | 0 |  |  |  |  |  | return 0; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 0 |  |  |  |  |  | eval { | 
| 38 | 0 | 0 |  |  |  |  | if ( $resource->type() eq 'schema' ) { | 
| 39 | 0 |  |  |  |  |  | return format_schema_docs( $self, $resource ); | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  | else { | 
| 42 | 0 |  |  |  |  |  | return format_resource_docs( $self, $resource ); | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  | }; | 
| 45 | 0 | 0 |  |  |  |  | if ($@) { | 
| 46 | 0 |  |  |  |  |  | warn($@); | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub smry_docs { | 
| 52 | 0 |  |  | 0 | 0 |  | return "summarize a resource or schema"; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub help_docs { | 
| 56 |  |  |  |  |  |  | return <<HELP | 
| 57 |  |  |  |  |  |  | View a "man page" of a resource. | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | Usage: | 
| 60 |  |  |  |  |  |  | docs [schema] [id] | 
| 61 |  |  |  |  |  |  | docs [/uri/to/id] | 
| 62 |  |  |  |  |  |  | man [schema] | 
| 63 |  |  |  |  |  |  | HELP | 
| 64 | 0 |  |  | 0 | 0 |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | sub comp_docs { | 
| 67 | 0 |  |  | 0 | 0 |  | my $self   = shift; | 
| 68 | 0 |  |  |  |  |  | my $client = $self->client; | 
| 69 | 0 |  |  |  |  |  | return grep { $client->schema($_)->is_queryable } $self->schema_completion(@_); | 
|  | 0 |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | sub alias_docs { | 
| 73 | 0 |  |  | 0 | 0 |  | return ('man'); | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | sub format_resource_docs { | 
| 77 | 0 |  |  | 0 | 0 |  | my $self     = shift; | 
| 78 | 0 |  |  |  |  |  | my $resource = shift; | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 0 |  |  |  |  |  | my $link_self = $resource->link('self'); | 
| 81 | 0 |  |  |  |  |  | my $type      = $resource->type(); | 
| 82 | 0 |  |  |  |  |  | my $type_fq   = $resource->type_fq(); | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 0 |  |  |  |  |  | my $output = ''; | 
| 85 | 0 |  |  |  |  |  | $output .= "RESOURCE\n"; | 
| 86 | 0 |  |  |  |  |  | $output .= "    GET $link_self\n\n"; | 
| 87 | 0 |  |  |  |  |  | $output .= "SCHEMA\n"; | 
| 88 | 0 |  |  |  |  |  | $output .= "    $type - GET $type_fq\n"; | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 0 | 0 |  |  |  |  | my %links = %{ $resource->links() || {} }; | 
|  | 0 |  |  |  |  |  |  | 
| 91 | 0 | 0 |  |  |  |  | if (%links) { | 
| 92 | 0 |  |  |  |  |  | $output .= "LINKS\n"; | 
| 93 | 0 |  |  |  |  |  | my ($table) = $self->format_pairs( | 
| 94 | 0 |  |  |  |  |  | [ map {"    $_"} keys %links ], | 
| 95 | 0 |  |  |  |  |  | [ map {"GET $_"} values %links ], | 
| 96 |  |  |  |  |  |  | " - ", 1 | 
| 97 |  |  |  |  |  |  | ); | 
| 98 | 0 |  |  |  |  |  | $output .= "$table"; | 
| 99 | 0 |  |  |  |  |  | $output .= "\n"; | 
| 100 |  |  |  |  |  |  | } | 
| 101 | 0 | 0 |  |  |  |  | my %actions = %{ $resource->actions() || {} }; | 
|  | 0 |  |  |  |  |  |  | 
| 102 | 0 | 0 |  |  |  |  | if (%actions) { | 
| 103 | 0 |  |  |  |  |  | $output .= "EXTRA ACTIONS\n"; | 
| 104 | 0 |  |  |  |  |  | my ($table) = $self->format_pairs( | 
| 105 | 0 |  |  |  |  |  | [ map {"    $_"} keys %actions ], | 
| 106 | 0 |  |  |  |  |  | [ map {"POST $_"} values %actions ], | 
| 107 |  |  |  |  |  |  | " - ", 1 | 
| 108 |  |  |  |  |  |  | ); | 
| 109 | 0 |  |  |  |  |  | $output .= "$table"; | 
| 110 | 0 |  |  |  |  |  | $output .= "\n"; | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 0 |  |  |  |  |  | my %fields = %{ $resource->fields() }; | 
|  | 0 |  |  |  |  |  |  | 
| 114 | 0 |  |  |  |  |  | delete $fields{links}; | 
| 115 | 0 |  |  |  |  |  | delete $fields{actions}; | 
| 116 | 0 | 0 |  |  |  |  | if (%fields) { | 
| 117 | 0 |  |  |  |  |  | $output .= "FIELDS\n"; | 
| 118 | 0 |  |  |  |  |  | my ($table) = $self->format_pairs( | 
| 119 |  |  |  |  |  |  | [ map {"    $_"} keys %fields ], | 
| 120 |  |  |  |  |  |  | [   map { | 
| 121 | 0 | 0 |  |  |  |  | if   ( ref($_) ) { json_encode($_) } | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | else             {$_} | 
| 123 |  |  |  |  |  |  | } values %fields | 
| 124 |  |  |  |  |  |  | ], | 
| 125 |  |  |  |  |  |  | " - ", | 
| 126 |  |  |  |  |  |  | 1 | 
| 127 |  |  |  |  |  |  | ); | 
| 128 | 0 |  |  |  |  |  | $output .= "$table"; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 0 |  |  |  |  |  | $self->page($output); | 
| 132 |  |  |  |  |  |  |  | 
| 133 | 0 |  |  |  |  |  | return 1; | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | sub format_schema_docs { | 
| 137 | 0 |  |  | 0 | 0 |  | my $self     = shift; | 
| 138 | 0 |  |  |  |  |  | my $resource = shift; | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 0 |  |  |  |  |  | my $link_self = $resource->link('self'); | 
| 141 | 0 |  |  |  |  |  | my $type      = $resource->type(); | 
| 142 | 0 |  |  |  |  |  | my $type_fq   = $resource->type_fq(); | 
| 143 |  |  |  |  |  |  |  | 
| 144 | 0 |  |  |  |  |  | my $output = ''; | 
| 145 | 0 |  |  |  |  |  | $output .= "SCHEMA\n"; | 
| 146 | 0 |  |  |  |  |  | $output .= "    GET $link_self\n\n"; | 
| 147 |  |  |  |  |  |  |  | 
| 148 | 0 | 0 |  |  |  |  | my %links = %{ $resource->links() || {} }; | 
|  | 0 |  |  |  |  |  |  | 
| 149 | 0 | 0 |  |  |  |  | if (%links) { | 
| 150 | 0 |  |  |  |  |  | $output .= "LINKS\n"; | 
| 151 | 0 |  |  |  |  |  | my ($table) = $self->format_pairs( | 
| 152 | 0 |  |  |  |  |  | [ map {"    $_"} keys %links ], | 
| 153 | 0 |  |  |  |  |  | [ map {"GET $_"} values %links ], | 
| 154 |  |  |  |  |  |  | " - ", 1 | 
| 155 |  |  |  |  |  |  | ); | 
| 156 | 0 |  |  |  |  |  | $output .= "$table"; | 
| 157 | 0 |  |  |  |  |  | $output .= "\n"; | 
| 158 |  |  |  |  |  |  | } | 
| 159 |  |  |  |  |  |  |  | 
| 160 | 0 |  |  |  |  |  | my $collection_link = $resource->link('collection'); | 
| 161 | 0 |  |  |  |  |  | $output .= "COLLECTION\n"; | 
| 162 | 0 |  |  |  |  |  | $output | 
| 163 |  |  |  |  |  |  | .= "    The collection is the URL that allows you to both search for things, as well as create new things of a particular type.\n\n"; | 
| 164 |  |  |  |  |  |  |  | 
| 165 | 0 | 0 |  |  |  |  | if ($collection_link) { | 
| 166 | 0 |  |  |  |  |  | my @methods = @{ $resource->f('collectionMethods') }; | 
|  | 0 |  |  |  |  |  |  | 
| 167 | 0 |  |  |  |  |  | my $methods_string = join ',', @methods; | 
| 168 | 0 | 0 |  |  |  |  | $methods_string .= ' ' if @methods; | 
| 169 | 0 | 0 |  |  |  |  | if (@methods) { | 
| 170 | 0 |  |  |  |  |  | foreach (@methods) { | 
| 171 | 0 |  |  |  |  |  | $output .= "    $_ $collection_link\n"; | 
| 172 |  |  |  |  |  |  | } | 
| 173 |  |  |  |  |  |  | } | 
| 174 |  |  |  |  |  |  | else { | 
| 175 | 0 |  |  |  |  |  | $output .= "    $collection_link\n\n"; | 
| 176 |  |  |  |  |  |  | } | 
| 177 | 0 |  |  |  |  |  | $output .= "\n    See the 'FIELDS' section for a list of queryable fields\n\n"; | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | } | 
| 180 |  |  |  |  |  |  | else { | 
| 181 | 0 |  |  |  |  |  | $output | 
| 182 |  |  |  |  |  |  | .= "    Searching is not available for this schema.  There is no\n'collection' link.\n\n"; | 
| 183 |  |  |  |  |  |  | } | 
| 184 |  |  |  |  |  |  |  | 
| 185 | 0 |  |  |  |  |  | my @fields  = sort $resource->resource_field_names(); | 
| 186 | 0 |  |  |  |  |  | my %filters = %{ $resource->f('collectionFilters') }; | 
|  | 0 |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  |  | 
| 188 | 0 |  |  |  |  |  | my $table = Text::FormatTable->new(' l | l | l | l '); | 
| 189 | 0 |  |  |  |  |  | $table->rule('-'); | 
| 190 | 0 |  |  |  |  |  | $table->head( 'Name', 'Searchable', 'Flags', 'Type' ); | 
| 191 | 0 |  |  |  |  |  | $table->rule('-'); | 
| 192 |  |  |  |  |  |  |  | 
| 193 | 0 |  |  |  |  |  | my $base_uri = $resource->client->url; | 
| 194 |  |  |  |  |  |  |  | 
| 195 | 0 |  |  |  |  |  | foreach my $field_name (@fields) { | 
| 196 | 0 |  |  |  |  |  | my $field      = $resource->resource_field($field_name); | 
| 197 | 0 |  |  |  |  |  | my $filter     = $filters{$field_name}; | 
| 198 | 0 |  |  |  |  |  | my $filter_ops = '-'; | 
| 199 | 0 | 0 |  |  |  |  | if ($filter) { | 
| 200 | 0 |  |  |  |  |  | $filter_ops = join ' ', @{ $filter->{modifiers} }; | 
|  | 0 |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | } | 
| 202 |  |  |  |  |  |  |  | 
| 203 | 0 | 0 |  |  |  |  | my $flags = sprintf( "%s%s%s%s", | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | $field->{unique}   ? 'K' : '-', | 
| 205 |  |  |  |  |  |  | $field->{required} ? 'R' : '-', | 
| 206 |  |  |  |  |  |  | $field->{create}   ? 'C' : '-', | 
| 207 |  |  |  |  |  |  | $field->{update}   ? 'U' : '-' ); | 
| 208 |  |  |  |  |  |  |  | 
| 209 | 0 |  |  |  |  |  | my @types = map { $_ =~ s|$base_uri||; $_; } | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 210 | 0 |  |  |  |  |  | grep {defined} | 
| 211 |  |  |  |  |  |  | $resource->resource_field_type( $field_name, { qualify_schema_types => 1 } ); | 
| 212 |  |  |  |  |  |  |  | 
| 213 | 0 |  |  |  |  |  | $table->row( $field_name, $filter_ops, $flags, ( join ' of ', @types ) ); | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | } | 
| 216 | 0 |  |  |  |  |  | $table->rule('-'); | 
| 217 |  |  |  |  |  |  |  | 
| 218 | 0 |  |  |  |  |  | my $table_text = $table->render( $self->termsize->{cols} - 4 ); | 
| 219 | 0 |  |  |  |  |  | $table_text =~ s/\n/\n    /gs; | 
| 220 | 0 |  |  |  |  |  | $table_text =~ s/^/    /g; | 
| 221 |  |  |  |  |  |  |  | 
| 222 | 0 |  |  |  |  |  | $output .= $table_text; | 
| 223 |  |  |  |  |  |  |  | 
| 224 | 0 |  |  |  |  |  | $output | 
| 225 |  |  |  |  |  |  | .= "Flags: K - Unique key, R - Required, C - Allowed on create, U - Allowed on update\n"; | 
| 226 |  |  |  |  |  |  |  | 
| 227 | 0 |  |  |  |  |  | $self->page($output); | 
| 228 |  |  |  |  |  |  |  | 
| 229 | 0 |  |  |  |  |  | return 1; | 
| 230 |  |  |  |  |  |  | } | 
| 231 |  |  |  |  |  |  |  | 
| 232 |  |  |  |  |  |  | 1; | 
| 233 |  |  |  |  |  |  |  | 
| 234 |  |  |  |  |  |  | =head1 AUTHOR | 
| 235 |  |  |  |  |  |  |  | 
| 236 |  |  |  |  |  |  | David Bartle, C<< <davidb@mediatemple.net> >> | 
| 237 |  |  |  |  |  |  |  | 
| 238 |  |  |  |  |  |  | =head1 COPYRIGHT & LICENSE | 
| 239 |  |  |  |  |  |  |  | 
| 240 |  |  |  |  |  |  | Copyright (c) 2014 Go Daddy Operating Company, LLC | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  | Permission is hereby granted, free of charge, to any person obtaining a | 
| 243 |  |  |  |  |  |  | copy of this software and associated documentation files (the "Software"), | 
| 244 |  |  |  |  |  |  | to deal in the Software without restriction, including without limitation | 
| 245 |  |  |  |  |  |  | the rights to use, copy, modify, merge, publish, distribute, sublicense, | 
| 246 |  |  |  |  |  |  | and/or sell copies of the Software, and to permit persons to whom the | 
| 247 |  |  |  |  |  |  | Software is furnished to do so, subject to the following conditions: | 
| 248 |  |  |  |  |  |  |  | 
| 249 |  |  |  |  |  |  | The above copyright notice and this permission notice shall be included in | 
| 250 |  |  |  |  |  |  | all copies or substantial portions of the Software. | 
| 251 |  |  |  |  |  |  |  | 
| 252 |  |  |  |  |  |  | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
| 253 |  |  |  |  |  |  | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
| 254 |  |  |  |  |  |  | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | 
| 255 |  |  |  |  |  |  | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
| 256 |  |  |  |  |  |  | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | 
| 257 |  |  |  |  |  |  | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 
| 258 |  |  |  |  |  |  | DEALINGS IN THE SOFTWARE. | 
| 259 |  |  |  |  |  |  |  | 
| 260 |  |  |  |  |  |  | =cut |