| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | WWW::TypePad::CodeGen - Functions for auto-generating this library from the API's reflection endpoint | ||
| 5 | |||
| 6 | =cut | ||
| 7 | |||
| 8 | package WWW::TypePad::CodeGen; | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | use String::CamelCase qw(camelize decamelize); | ||
| 14 | |||
| 15 | sub handle_object { | ||
| 16 | my($key, $mapping) = @_; | ||
| 17 | |||
| 18 | my $name = camelize($key); | ||
| 19 | my $file = "lib/WWW/TypePad/$name.pm"; | ||
| 20 | rewrite_file($mapping, $name, $file); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub rewrite_file { | ||
| 24 | my($mapping, $package, $file) = @_; | ||
| 25 | |||
| 26 | my $content = slurp($file) || stub_for($package, $mapping); | ||
| 27 | $content =~ s/### BEGIN auto-generated.*### END auto-generated\n/generate_code($package, $mapping)/egs | ||
| 28 | or return warn "$file: auto-generated code marked was not found.\n"; | ||
| 29 | |||
| 30 | warn "Writing $file\n"; | ||
| 31 | open my $fh, ">", $file or die "$file: $!"; | ||
| 32 | print $fh $content; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub generate_code { | ||
| 36 | my($package, $mapping) = @_; | ||
| 37 | |||
| 38 | my %podesc = ('<' => 'E<lt>', '>' => 'E<gt>'); | ||
| 39 | |||
| 40 | my $stash = { | ||
| 41 | package => $package, | ||
| 42 | mapping => $mapping, | ||
| 43 | safe => sub { $_[0] =~ tr/-/_/; $_[0] }, | ||
| 44 | decamelize => sub { decamelize($_[0]) }, | ||
| 45 | mangle_method => sub { | ||
| 46 | my $method = $_[0]; | ||
| 47 | $method =~ s/^get([A-Z])/$1/; | ||
| 48 | decamelize($method); | ||
| 49 | }, | ||
| 50 | path_format => sub { join '/', map { defined $_ ? $_ : '%s' } @{$_[0]} }, | ||
| 51 | keys_sorted_by_value => sub { sort { $_[0]->{$a} <=> $_[0]->{$b} } keys %{$_[0]} }, | ||
| 52 | escape_pod => sub { | ||
| 53 | $_[0] =~ s/T<(.*?)>/B<$1>/g; | ||
| 54 | $_[0] =~ s/([<>])/$podesc{$1}/g; | ||
| 55 | $_[0]; | ||
| 56 | }, | ||
| 57 | }; | ||
| 58 | |||
| 59 | my $tt = Template->new; | ||
| 60 | $tt->process(\<<'TEMPLATE', $stash, \my $output) or die $tt->error; | ||
| 61 | ### BEGIN auto-generated | ||
| 62 | ### This is an automatically generated code, do not edit! | ||
| 63 | ### Scroll down to look for END to add additional methods | ||
| 64 | |||
| 65 | =pod | ||
| 66 | |||
| 67 | =head1 NAME | ||
| 68 | |||
| 69 | WWW::TypePad::[% package %] - [% package %] API methods | ||
| 70 | |||
| 71 | =head1 METHODS | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | use strict; | ||
| 76 | use Any::Moose; | ||
| 77 | extends 'WWW::TypePad::Noun'; | ||
| 78 | |||
| 79 | use Carp (); | ||
| 80 | |||
| 81 | [% FOREACH method IN mapping.methods -%] | ||
| 82 | |||
| 83 | [%- SET canonical = decamelize(method.methodName); | ||
| 84 | SET mangled = mangle_method(method.methodName); | ||
| 85 | SET params = keys_sorted_by_value( method.pathParams ) -%] | ||
| 86 | |||
| 87 | =pod | ||
| 88 | |||
| 89 | [% IF loop.first %]=over 4 | ||
| 90 | [% END %] | ||
| 91 | |||
| 92 | =item [% canonical %] | ||
| 93 | |||
| 94 | my $res = $tp->[% decamelize(package) %]->[% canonical %]([% FOREACH param IN params %]$[% param %][% UNLESS loop.last %], [% END %][% END %]); | ||
| 95 | |||
| 96 | [% escape_pod(method.docString) %] | ||
| 97 | |||
| 98 | Returns [% escape_pod(method.returnObjectType.name) || 'hash reference' %] which contains following properties. | ||
| 99 | |||
| 100 | =over 8 | ||
| 101 | |||
| 102 | [% FOREACH prop IN method.returnObjectType.properties -%] | ||
| 103 | =item [% prop.name %] | ||
| 104 | |||
| 105 | ([% escape_pod(prop.type) %]) [% escape_pod(prop.docString) %] | ||
| 106 | |||
| 107 | [% END -%] | ||
| 108 | |||
| 109 | =back | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | sub [% canonical %] { | ||
| 114 | my $api = shift; | ||
| 115 | my @args; | ||
| 116 | [% FOREACH param IN params -%] | ||
| 117 | push @args, shift; # [% param %] | ||
| 118 | [% END -%] | ||
| 119 | my $uri = sprintf '/[% path_format(method.pathChunks) %].json', @args; | ||
| 120 | $api->base->call("[% method.httpMethod %]", $uri, @_); | ||
| 121 | } | ||
| 122 | |||
| 123 | [% IF canonical != mangled %] | ||
| 124 | sub [% mangled %] { | ||
| 125 | my $self = shift; | ||
| 126 | Carp::carp("'[% mangled %]' is deprecated. Use '[% canonical %]' instead."); | ||
| 127 | $self->[% canonical %](@_); | ||
| 128 | } | ||
| 129 | [% END -%] | ||
| 130 | [% END # FOREACH method -%] | ||
| 131 | |||
| 132 | =pod | ||
| 133 | |||
| 134 | =back | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | ### END auto-generated | ||
| 139 | TEMPLATE | ||
| 140 | |||
| 141 | return $output; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub slurp { | ||
| 145 | my $file = shift; | ||
| 146 | open my $fh, "<", $file or return; | ||
| 147 | join '', <$fh>; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub stub_for { | ||
| 151 | my $package = shift; | ||
| 152 | my $accessor_name = decamelize($package); | ||
| 153 | return <<EOT; | ||
| 154 | package WWW::TypePad::$package; | ||
| 155 | |||
| 156 | use strict; | ||
| 157 | use warnings; | ||
| 158 | |||
| 159 | # Install an accessor into WWW::TypePad to access an instance of this class | ||
| 160 | # bound to the WWW::TypePad instance. | ||
| 161 | sub WWW::TypePad::$accessor_name { __PACKAGE__->new( base => \$_[0] ) } | ||
| 162 | |||
| 163 | ### BEGIN auto-generated | ||
| 164 | |||
| 165 | ### END auto-generated | ||
| 166 | |||
| 167 | 1; | ||
| 168 | EOT | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | 1; |