| 1 | package Moo::Google::Services; | ||
| 2 | $Moo::Google::Services::VERSION = '0.03'; | ||
| 3 | |||
| 4 | # ABSTRACT: generate classes, attributes and methods for appropriate API methods using Moose::Meta::Class | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | |||
| 8 | # because of: | ||
| 9 | # class must be not immutable | ||
| 10 | # use Mouse::Meta::Class; | ||
| 11 | # use Mouse::Meta::Attribute; | ||
| 12 | |||
| 13 | use Data::Dumper; | ||
| 14 | use Data::Printer; | ||
| 15 | |||
| 16 | has 'debug' => ( is => 'rw', default => 0, lazy => 1 ); | ||
| 17 | |||
| 18 | # has 'client' => ( is => 'ro', default => sub { require Moo::Google::Client; Moo::Google::Client->new(); }, handles => [qw(api_query)], lazy => 1); | ||
| 19 | |||
| 20 | # has 'util' => ( is => 'ro', default => sub { require Moo::Google::Util; Moo::Google::Util->new(); }, handles => [qw(substitute_placeholders)], lazy => 1); | ||
| 21 | |||
| 22 | has 'discovery' => ( | ||
| 23 | is => 'ro', | ||
| 24 | default => sub { | ||
| 25 | require Moo::Google::Discovery; | ||
| 26 | Moo::Google::Discovery->new( debug => shift->debug ); | ||
| 27 | }, | ||
| 28 | lazy => 1 | ||
| 29 | ); | ||
| 30 | |||
| 31 | # use Moo::Google::Discovery; | ||
| 32 | # my $self->discovery = Moo::Google::Discovery->new(debug=>shift->debug); | ||
| 33 | |||
| 34 | # extends 'Moo::Google::Client'; | ||
| 35 | |||
| 36 | |||
| 37 | sub generate_one { | ||
| 38 | my ( $self, $object, $p ) = @_; # $p = parameter(s) | ||
| 39 | |||
| 40 | warn Dumper ref($p) if ( $self->debug ); | ||
| 41 | |||
| 42 | my $api; | ||
| 43 | my $version; | ||
| 44 | |||
| 45 | if ($p) { | ||
| 46 | $api = $p; | ||
| 47 | } | ||
| 48 | elsif ( ref($p) eq 'HASH' ) { | ||
| 49 | $api = $p->{api}; | ||
| 50 | } | ||
| 51 | else { | ||
| 52 | die "Wrong parameters type. Supported are HASH or SCALAR"; | ||
| 53 | } | ||
| 54 | |||
| 55 | warn $api if ( $self->debug ); | ||
| 56 | |||
| 57 | if ( $self->discovery->exists($api) ) { | ||
| 58 | $version = $self->discovery->latestStableVersion($api); | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | die | ||
| 62 | "No such service or its currently unsupported by Google API discovery"; | ||
| 63 | } | ||
| 64 | |||
| 65 | #my $base_class = 'Moo::Google::'; # $object | ||
| 66 | warn "Generating Resources for " . ref($object) . " class" | ||
| 67 | if ( $self->debug ); | ||
| 68 | my $base_class = ref($object); # $object | ||
| 69 | my $service_name = $api; | ||
| 70 | my $service_class_name = ucfirst $api; | ||
| 71 | |||
| 72 | # warn $service_name; | ||
| 73 | # # warn $s->{version}[0]; | ||
| 74 | my $service_class = Moose::Meta::Class->create( | ||
| 75 | join( '::', $base_class, $service_class_name ) ); # Calendar | ||
| 76 | my $service_description = $self->discovery->getRest( | ||
| 77 | { api => $service_name, version => $version } ); | ||
| 78 | warn $service_name | ||
| 79 | . " resources :" | ||
| 80 | . Dumper $service_description->{resources} | ||
| 81 | if ( $self->debug ); | ||
| 82 | |||
| 83 | my @resources = keys %{ $service_description->{resources} }; | ||
| 84 | |||
| 85 | for my $resource_name (@resources) { | ||
| 86 | my $resource_class_name = ucfirst $resource_name; | ||
| 87 | my $resource_class = Moose::Meta::Class->create( | ||
| 88 | join( | ||
| 89 | '::', $base_class, $service_class_name, $resource_class_name | ||
| 90 | ) | ||
| 91 | ); # Calendar:Events | ||
| 92 | |||
| 93 | $service_class->add_attribute( | ||
| 94 | $resource_class_name => { | ||
| 95 | is => 'ro', | ||
| 96 | default => sub { $resource_class->new_object }, | ||
| 97 | lazy => 1 | ||
| 98 | } | ||
| 99 | ); | ||
| 100 | |||
| 101 | my $methods_hash = | ||
| 102 | $service_description->{resources}{$resource_name}{methods} | ||
| 103 | ; # return like { 'get' => 'HASH(0x3deeb48)', 'list' => 'HASH(0x3e18698)', ... } | ||
| 104 | # warn Dumper $methods_hash; | ||
| 105 | |||
| 106 | my @methods = keys %$methods_hash; | ||
| 107 | |||
| 108 | for my $method_name (@methods) { | ||
| 109 | $resource_class->add_method( | ||
| 110 | $method_name => sub { | ||
| 111 | my ( $self, $params ) = @_; | ||
| 112 | $object->request( | ||
| 113 | join( '::', | ||
| 114 | $base_class, $service_class_name, | ||
| 115 | $resource_class_name, $method_name ), | ||
| 116 | $params | ||
| 117 | ); | ||
| 118 | } | ||
| 119 | ); | ||
| 120 | } | ||
| 121 | |||
| 122 | } | ||
| 123 | |||
| 124 | ## add attributes to this class | ||
| 125 | |||
| 126 | # adding to Moo::Google::Services | ||
| 127 | |||
| 128 | # $self->meta->add_attribute( | ||
| 129 | |||
| 130 | $object->meta->add_attribute( | ||
| 131 | $service_class_name => { | ||
| 132 | is => 'ro', | ||
| 133 | default => sub { $service_class->new_object }, | ||
| 134 | lazy => 1 | ||
| 135 | } | ||
| 136 | ); | ||
| 137 | |||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | sub generate_all { | ||
| 142 | my $self = shift; | ||
| 143 | my $services = $self->discovery->availableAPIs(); | ||
| 144 | |||
| 145 | # warn "All services: ".Dumper $services; | ||
| 146 | |||
| 147 | for my $s (@$services) { | ||
| 148 | $self->generate_one( $s->{name} ); | ||
| 149 | } | ||
| 150 | |||
| 151 | } | ||
| 152 | |||
| 153 | # sub request { | ||
| 154 | # my ($self, $caller, $params) = @_; | ||
| 155 | # # my $caller = (caller(0))[3]; | ||
| 156 | # warn "Caller: ".$caller; | ||
| 157 | # my $api_q_data = $self->discovery->getMethodMeta($caller); | ||
| 158 | # $api_q_data->{options} = $params->{options}; | ||
| 159 | # delete $params->{options}; | ||
| 160 | # $api_q_data->{path} = $self->substitute_placeholders($api_q_data->{path}, $params); | ||
| 161 | # warn Dumper $api_q_data; | ||
| 162 | # $self->api_query($api_q_data); # path, httpMethod | ||
| 163 | # }; | ||
| 164 | |||
| 165 | sub substitute_placeholders { | ||
| 166 | my ( $self, $string, $parameters ) = @_; | ||
| 167 | |||
| 168 | # find all parameters in string | ||
| 169 | my @matches = $string =~ /{([a-zA-Z_]+)}/g; | ||
| 170 | for my $prm (@matches) { | ||
| 171 | |||
| 172 | # warn $prm; | ||
| 173 | if ( defined $parameters->{$prm} ) { | ||
| 174 | my $s = $parameters->{$prm}; | ||
| 175 | warn "Value of " . $prm . " took from passed parameters: " . $s; | ||
| 176 | $string =~ s/{$prm}/$s/g; | ||
| 177 | } | ||
| 178 | elsif ( defined $self->$prm ) { | ||
| 179 | my $s = $self->$prm; | ||
| 180 | warn "Value of " . $prm . " took from class attributes: " . $s; | ||
| 181 | $string =~ s/{$prm}/$s/g; | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | die "cant replace " . $prm . " placeholder: no source"; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | return $string; | ||
| 188 | } | ||
| 189 | |||
| 190 | # has 'Calendar' => ( is => 'ro', default => sub { | ||
| 191 | # require Moo::Google::Calendar; | ||
| 192 | # Moo::Google::Calendar->new; | ||
| 193 | # }); | ||
| 194 | |||
| 195 | 1; | ||
| 196 | |||
| 197 | __END__ |