#!/usr/bin/env perl
package WWW::SFDC::Apex;
# ABSTRACT: Interface to the salesforce.com Apex SOAP Api

use 5.12.0;
use strict;
use warnings;

our $VERSION = '0.35'; # VERSION

use Log::Log4perl ':easy';
use SOAP::Lite;

use WWW::SFDC::Apex::ExecuteAnonymousResult;

use Moo;
with "WWW::SFDC::Role::SessionConsumer";

has 'uri',
    is => 'ro',
    default=> "http://soap.sforce.com/2006/08/apex";

sub _extractURL {
    return $_[1]->{serverUrl} =~ s{/u/}{/s/}r;
}



sub compileAndTest {
  my ($self, @names) = @_;

  return $self->_call(
    'compileAndTest',
    map {\SOAP::Data->name(classes => $_)} @names
    );
}


sub compileClasses {
  my ($self, @names) = @_;

  return $self->_call(
    'compileClasses',
    SOAP::Data->value(map {SOAP::Data->name(scripts => $_)} @names)
    );
}


sub compileTriggers {
  my ($self, @names) = @_;

  return $self->_call(
    'compileTriggers',
    map {\SOAP::Data->name(classes => $_)} @names
    );
}


sub executeAnonymous {
  my ($self, $code, %options) = @_;

  my ($result, $headers) = $self->_call(
    'executeAnonymous',
    SOAP::Data->name(string => $code),
    (
      $options{debug}
        ? SOAP::Header->name('DebuggingHeader' => \SOAP::Data->name(
            debugLevel => 'DEBUGONLY'
          ))->uri($self->uri)
        : ()
    ),
  );

  return WWW::SFDC::Apex::ExecuteAnonymousResult->new(
    _result => $result,
    _headers => $headers
  );
}


sub runTests {
  my ($self, @names) = @_;

  return $self->_call(
    'runTests',
    map {\SOAP::Data->name(classes => $_)} @names
    );
}


sub wsdlToApex {
    ...
}

1;

__END__

=pod

=head1 NAME

WWW::SFDC::Apex - Interface to the salesforce.com Apex SOAP Api

=head1 VERSION

version 0.35

=head1 METHODS

=head2 compileAndTest

=head2 compileClasses

=head2 compileTriggers

=head2 executeAnonymous

Returns a WWW::SFDC::Apex::ExecuteAnonymousResult containing the results of the
executeAnonymous call. You must manually check whether this succeeded.

=head2 runTests

=head2 wsdlToApex

Unimplemented

=head1 AUTHOR

Alexander Brett <alexander.brett@sophos.com> L<http://alexander-brett.co.uk>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2015 by Sophos Limited L<https://www.sophos.com/>.

This is free software, licensed under:

  The MIT (X11) License

The full text of the license can be found in the
F<LICENSE> file included with this distribution.

=cut
