| 1 | =pod | ||
| 2 | |||
| 3 | =encoding utf-8 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | B<Yandex::Audience> - a simple API for Yandex.Audience | ||
| 8 | |||
| 9 | It contains very few number of API-calls now. | ||
| 10 | |||
| 11 | =head1 VERSION | ||
| 12 | |||
| 13 | version 0.01 | ||
| 14 | |||
| 15 | =head1 SYNOPSYS | ||
| 16 | |||
| 17 | use Yandex::Audience; | ||
| 18 | |||
| 19 | my $Token = 'AgAAAAAAELGSBIXHdBAPDm-6sJ7Sbao7J-pmaU7'; #Auth token | ||
| 20 | my $YaAudience= Yandex::Audience->new( -token => $Token); | ||
| 21 | |||
| 22 | #Get list of existing segments | ||
| 23 | my $Segments = $YaAudience->getListOfSegments(); | ||
| 24 | |||
| 25 | #Upload a content of String in CSV-fromat | ||
| 26 | my $Segment = $YaAudience->uploadCSV($CSV); | ||
| 27 | |||
| 28 | #Upload CSV-file | ||
| 29 | my $Segment = $YaAudience->uploadCSV('real1500_md5.csv'); | ||
| 30 | |||
| 31 | #Save the uploaded content as a segment | ||
| 32 | my $SegmentStatus = $YaAudience->saveSegment( segment => $Segment->{id}, | ||
| 33 | name => 'Litres'.$Segment->{id}, | ||
| 34 | hashed => 1, | ||
| 35 | ) if exists $Segment->{id}; | ||
| 36 | |||
| 37 | #Delete a segment | ||
| 38 | my $Result = $YaAudience->deleteSegment(9243581); | ||
| 39 | |||
| 40 | =head1 METHODS | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | package Yandex::Audience; | ||
| 45 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 46 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 47 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 48 | 1 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 49 | 1 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 50 | 1 | ▶ | use HTTP::Request::Common qw(POST); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 51 | 0 | ▶ | use JSON::XS; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 52 | use File::Basename; | ||
| 53 | |||
| 54 | use constant APIVERSION => 1; | ||
| 55 | use constant BASEURL => 'https://api-audience.yandex.ru/v'; | ||
| 56 | |||
| 57 | our $VERSION = '0.01'; | ||
| 58 | |||
| 59 | sub new { | ||
| 60 | my $class = shift; | ||
| 61 | my %opt = @_; | ||
| 62 | my $self = {}; | ||
| 63 | $self->{token} = $opt{token} || croak "Specify token param"; | ||
| 64 | $self->{timeout} = 10; #For LWP::UserAgent | ||
| 65 | |||
| 66 | my $ua = LWP::UserAgent->new(); | ||
| 67 | $ua->timeout($self->{timeout}); | ||
| 68 | $ua->default_header(Authorization => 'OAuth ' . $self->{token}); | ||
| 69 | $ua->default_header('Content-Type' => 'application/json'); | ||
| 70 | $self->{ua} = $ua; | ||
| 71 | |||
| 72 | bless $self, $class; | ||
| 73 | return $self; | ||
| 74 | } | ||
| 75 | |||
| 76 | =head2 getListOfSegments() | ||
| 77 | |||
| 78 | Returns a list of existing segments available to the user. | ||
| 79 | |||
| 80 | my $Segments = $YaAudience->get_list_of_segments(); | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub getListOfSegments { | ||
| 85 | my $self = shift; | ||
| 86 | my $url = &BASEURL . &APIVERSION . '/management/segments'; | ||
| 87 | |||
| 88 | my $response = $self->{ua}->get($url); | ||
| 89 | return undef unless $response->is_success && $response->content; | ||
| 90 | |||
| 91 | my $json; | ||
| 92 | eval { | ||
| 93 | $json = JSON::XS->new->utf8->decode( $response->content ); | ||
| 94 | }; | ||
| 95 | return undef if $@; | ||
| 96 | |||
| 97 | if (exists $json->{segments}) { | ||
| 98 | for my $Segment (@{$json->{segments}}) { | ||
| 99 | $self->_prepareJSON($Segment); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | return $json->{segments}; | ||
| 103 | } | ||
| 104 | |||
| 105 | =head2 uploadCSV() | ||
| 106 | |||
| 107 | Upload a CSV-file with data and create a segment. | ||
| 108 | Returns Hash with Id of segment(s) and it's statuses. | ||
| 109 | |||
| 110 | my $Segment = $YaAudience->uploadCSV('real1500_md5.csv'); | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub uploadCSV { | ||
| 115 | my $self = shift; | ||
| 116 | my $file = shift; | ||
| 117 | |||
| 118 | my $url = &BASEURL . &APIVERSION . '/management/segments/upload_csv_file'; | ||
| 119 | my $request; | ||
| 120 | if (!chomp $file && -e $file) { | ||
| 121 | $request = POST ($url, Content_Type => 'form-data', Content => ['file', [$file]]); | ||
| 122 | } else { | ||
| 123 | $request = POST ($url, Content_Type => 'form-data', Content => [file => [undef, 'crm.csv', Content=>$file, Content_Type => 'text/csv'] ]); | ||
| 124 | } | ||
| 125 | my $response = $self->{ua}->request($request); | ||
| 126 | |||
| 127 | return undef unless $response->is_success && $response->content; | ||
| 128 | |||
| 129 | my $json; | ||
| 130 | eval { | ||
| 131 | $json = JSON::XS->new->utf8->decode( $response->content ); | ||
| 132 | }; | ||
| 133 | return undef if $@; | ||
| 134 | |||
| 135 | $self->_prepareJSON($json->{segment}) if exists $json->{segment}; | ||
| 136 | return $json->{segment}; | ||
| 137 | } | ||
| 138 | |||
| 139 | =head2 saveSegment() | ||
| 140 | |||
| 141 | Saves a segment generated from a file with user data. | ||
| 142 | |||
| 143 | my $SegmentStatus = $YaAudience->saveSegment( -segment => $Segment->{id}, #Id of a segment | ||
| 144 | -name => 'Litres'.$Segment->{id}, #A name of a segment | ||
| 145 | -hashed => 1, #1 if data contains hashed fields | ||
| 146 | ) if exists $Segment->{id}; | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub saveSegment { | ||
| 151 | my $self = shift; | ||
| 152 | my %opt = @_; | ||
| 153 | my $SegmentID = $opt{segment} || Carp::carp('id of a segment must be defined'); | ||
| 154 | my $SegmentName = exists $opt{name} ? $opt{name} : 'Segment' . $SegmentID; | ||
| 155 | my $SegmentType = exists $opt{type} ? $opt{type} : 'crm'; | ||
| 156 | my $IsHashed = exists $opt{hashed} && $opt{hashed} ? 1 : 0; | ||
| 157 | |||
| 158 | my $url = &BASEURL . &APIVERSION . '/management/segment/' . $SegmentID . '/confirm'; | ||
| 159 | |||
| 160 | my %JSON = ( | ||
| 161 | segment => { | ||
| 162 | id => $SegmentID, | ||
| 163 | name => $SegmentName, | ||
| 164 | hashed => $IsHashed, | ||
| 165 | content_type => $SegmentType, | ||
| 166 | } | ||
| 167 | ); | ||
| 168 | |||
| 169 | my $response = $self->{ua}->post( $url, Content => encode_json(\%JSON) ); | ||
| 170 | |||
| 171 | return undef unless $response->is_success && $response->content; | ||
| 172 | |||
| 173 | my $json; | ||
| 174 | eval { | ||
| 175 | $json = JSON::XS->new->utf8->decode( $response->content ); | ||
| 176 | }; | ||
| 177 | return undef if $@; | ||
| 178 | |||
| 179 | $self->_prepareJSON($json->{segment}) if exists $json->{segment}; | ||
| 180 | return $json->{segment}; | ||
| 181 | |||
| 182 | return 1; | ||
| 183 | } | ||
| 184 | |||
| 185 | =head2 deleteSegment() | ||
| 186 | |||
| 187 | Deletes the specified segment (or segments) | ||
| 188 | Returns arrayref to scalars: 1 if success, 0 otherwise. | ||
| 189 | |||
| 190 | my $Result = $YaAudience->deleteSegment($SegmentID); #$SegmentID - can be scalar or arrayref to scalars, or arrayref to hashes with id of segment(s). | ||
| 191 | or | ||
| 192 | my $Result = $YaAudience->deleteSegment( [9254200, 9254215] ); | ||
| 193 | =cut | ||
| 194 | |||
| 195 | sub deleteSegment { | ||
| 196 | my $self = shift; | ||
| 197 | my $SegmentID = shift // Carp::carp('id of the segment must be defined'); | ||
| 198 | my @Result; | ||
| 199 | |||
| 200 | if (ref $SegmentID eq 'ARRAY') { | ||
| 201 | if (ref $SegmentID->[0] eq 'HASH' && exists $SegmentID->[0]->{id}) { #Perhaps it's array with structures of `yandex.segment` type | ||
| 202 | for (@$SegmentID) { | ||
| 203 | push @Result, $self->_deleteSegment($_->{id}); | ||
| 204 | } | ||
| 205 | } else { #It must be array of scalars | ||
| 206 | for (@$SegmentID) { | ||
| 207 | push @Result, $self->_deleteSegment($_); | ||
| 208 | } | ||
| 209 | } | ||
| 210 | } else { #It must be scalar | ||
| 211 | return $self->_deleteSegment($SegmentID); | ||
| 212 | } | ||
| 213 | return \@Result; | ||
| 214 | } | ||
| 215 | |||
| 216 | =head2 _deleteSegment() | ||
| 217 | |||
| 218 | The internal method. Deletes a segment. | ||
| 219 | Returns scalar: 1 if success, 0 otherwise. | ||
| 220 | |||
| 221 | my $Result = $YaAudience->_deleteSegment($SegmentID); #$SegmentID - scalar, contains id of segment. | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | sub _deleteSegment { | ||
| 226 | my $self = shift; | ||
| 227 | my $SegmentID = shift // Carp::carp('id of the segment must be defined'); | ||
| 228 | |||
| 229 | my $url = &BASEURL . &APIVERSION . '/management/segment/' . $SegmentID; | ||
| 230 | my $response = $self->{ua}->delete($url); | ||
| 231 | |||
| 232 | return 0 unless $response->is_success && $response->content; | ||
| 233 | |||
| 234 | my $json; | ||
| 235 | eval { | ||
| 236 | $json = JSON::XS->new->utf8->decode( $response->content ); | ||
| 237 | }; | ||
| 238 | return undef if $@; | ||
| 239 | return $json->{success} eq JSON::XS::true ? 1 : 0; | ||
| 240 | } | ||
| 241 | |||
| 242 | =head2 _prepareJSON() | ||
| 243 | |||
| 244 | The internal method. Returns hash with values of type JSON::Boolean changed to Scalars (1 and 0). | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | sub _prepareJSON { | ||
| 249 | my $self = shift; | ||
| 250 | my $json = shift; | ||
| 251 | |||
| 252 | for (keys %{$json}) { | ||
| 253 | if (JSON::XS::is_bool($json->{$_})) { | ||
| 254 | $json->{$_} = ($json->{$_} eq JSON::XS::true) ? 1 : 0; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | return $json; | ||
| 258 | } | ||
| 259 | |||
| 260 | =head1 AUTHOR | ||
| 261 | |||
| 262 | Dmitry Marin, C<< <mcorvax at cpan.org> >> | ||
| 263 | |||
| 264 | |||
| 265 | =head1 BUGS | ||
| 266 | |||
| 267 | Please report any bugs or feature requests to C<bug-yandex-audience at rt.cpan.org>, or through | ||
| 268 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Yandex-Audience>. I will be notified, and then you'll | ||
| 269 | automatically be notified of progress on your bug as I make changes. | ||
| 270 | |||
| 271 | |||
| 272 | =head1 SUPPORT | ||
| 273 | |||
| 274 | You can find documentation for this module with the perldoc command. | ||
| 275 | |||
| 276 | perldoc Yandex::Audience | ||
| 277 | |||
| 278 | |||
| 279 | You can also look for information at: | ||
| 280 | |||
| 281 | =over 4 | ||
| 282 | |||
| 283 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 284 | |||
| 285 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Yandex-Audience> | ||
| 286 | |||
| 287 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 288 | |||
| 289 | L<http://annocpan.org/dist/Yandex-Audience> | ||
| 290 | |||
| 291 | =item * CPAN Ratings | ||
| 292 | |||
| 293 | L<https://cpanratings.perl.org/d/Yandex-Audience> | ||
| 294 | |||
| 295 | =item * Search CPAN | ||
| 296 | |||
| 297 | L<https://metacpan.org/release/Yandex-Audience> | ||
| 298 | |||
| 299 | =back | ||
| 300 | |||
| 301 | |||
| 302 | =head1 LICENSE AND COPYRIGHT | ||
| 303 | |||
| 304 | This software is Copyright (c) 2019 by Dmitry Marin. | ||
| 305 | |||
| 306 | This is free software, licensed under: | ||
| 307 | |||
| 308 | The Artistic License 2.0 (GPL Compatible) | ||
| 309 | |||
| 310 | =cut | ||
| 311 | |||
| 312 | 1; # End of Yandex::Audience | ||