| 1 | package DocRaptor; | ||
| 2 | |||
| 3 | # This is used by the user agent, and should match up with the version in Build.PL. | ||
| 4 | my $PRETTY_VERSION = '0.2.1'; | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use HTTP::Request::Common qw(POST); | ||
| 8 | use LWP::UserAgent; | ||
| 9 | use LWP::Protocol::https; | ||
| 10 | |||
| 11 | has 'api_key' => ( | ||
| 12 | is => 'rw', | ||
| 13 | isa => 'Str', | ||
| 14 | required => 1, | ||
| 15 | ); | ||
| 16 | |||
| 17 | has 'is_reporting_user_agent' => ( | ||
| 18 | traits => ['Bool'], | ||
| 19 | is => 'rw', | ||
| 20 | isa => 'Bool', | ||
| 21 | default => 1, | ||
| 22 | ); | ||
| 23 | |||
| 24 | my $VERSION = '0.002000'; | ||
| 25 | my $DOC_RAPTOR_URL = 'https://docraptor.com/docs'; | ||
| 26 | my $USER_AGENT_STRING = 'doc_raptor-perl/'.$PRETTY_VERSION.' perl/'.sprintf( "%vd\n", $^V ); | ||
| 27 | |||
| 28 | sub create | ||
| 29 | { | ||
| 30 | my $self = shift; | ||
| 31 | my $options = shift; # a DocRaptor::DocOptions object | ||
| 32 | my %request_options = $self->_coerce_options_to_request_format( $options ); | ||
| 33 | $request_options{'user_credentials'} = $self->api_key; | ||
| 34 | my $request = POST( $DOC_RAPTOR_URL, [%request_options] ); | ||
| 35 | return $self->_user_agent->request($request); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _user_agent | ||
| 39 | { | ||
| 40 | my $self = shift; | ||
| 41 | my $agent_string = $self->is_reporting_user_agent ? $USER_AGENT_STRING : ''; | ||
| 42 | LWP::UserAgent->new( agent => $agent_string, ssl_opts => { verify_hostname => 0 } ); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _coerce_options_to_request_format | ||
| 46 | { | ||
| 47 | my $self = shift; | ||
| 48 | my $options = shift; | ||
| 49 | my %request_options = ( | ||
| 50 | 'doc[document_type]' => $options->document_type, | ||
| 51 | 'doc[name]' => $options->document_name, | ||
| 52 | 'doc[test]' => $options->is_test ? 'true' : 'false', | ||
| 53 | ); | ||
| 54 | if( $options->document_content ) | ||
| 55 | { | ||
| 56 | $request_options{'doc[document_content]'} = $options->document_content; | ||
| 57 | } | ||
| 58 | elsif( $options->document_url ) | ||
| 59 | { | ||
| 60 | $request_options{'doc[document_url]'} = $options->document_url; | ||
| 61 | } | ||
| 62 | else | ||
| 63 | { | ||
| 64 | die( "Must supply a document_url or document_content" ); | ||
| 65 | } | ||
| 66 | |||
| 67 | return %request_options; | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; |