-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefensio.pm
161 lines (133 loc) · 4.29 KB
/
Defensio.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package Defensio;
use Carp;
use LWP::UserAgent;
use Badger::Codec::URI;
use JSON::XS;
use strict;
# You shouldn't modify these values unless you really know what you are doing. And then again...
my $API_VERSION = '2.0';
my $API_HOST = "http://api.defensio.com";
# You should't modify anything below this line.
my $LIB_VERSION = "0.9";
my $ROOT_NODE = "defensio-result";
my $FORMAT = "json";
my $USER_AGENT = "Defensio-Perl 0.9";
my $CLIENT = 'Defensio-Perl | 0.9 | Jason Pope | jpope@websense.com';
my $ua;
my $codec ;
sub new{
my ($class, %params) = @_;
$ua = LWP::UserAgent->new;
$ua->agent($USER_AGENT);
$ua->timeout(30);
$codec = Badger::Codec::URI->new();
return undef unless $params{api_key};
my $this=
{
'api_key' => $params{'api_key'},
'client' => $params{'client'} || $CLIENT,
'format' => $params{'format'} || 'json',
'service_type' => $params{'service_type'} || 'app',
'protocol' => $params{'protocol'} || 'http',
'platform' => $params{'platform'} || 'defensio-perl',
'port' => $params{'port'} || 80,
'async' => $params{'async'} || 'true',
'async-callback' => $params{'async-callback'},
};
bless ($this, $class);
return $this;
}
# Get information about the api key
sub get_user{
my $this = shift;
$this->call('get',$this->api_url );
}
# Create and analyze a new document
# @param [Hash] data The parameters to be sent to Defensio. Keys can either be Strings or Symbols
# @return [Hash] the values returned by Defensio
sub post_document{
my ($this, %data) = @_;
return $this->call ('post', $this->api_url("documents"), \%data);
}
# Get the status of an existing document
# @param [String] signature The signature of the document to modify
# @return [Hash] the values returned by Defensio
sub get_document{
my ($this, $signature) = @_;
$this->call ('get', $this->api_url("documents", $signature));
}
# Modify the properties of an existing document
# @param [String] signature The signature of the document to modify
# @param [Hash] data The parameters to be sent to Defensio. Keys can either be Strings or Symbols
# @return [Hash] the values returned by Defensio
sub put_document{
my ($this,$signature, %data) = @_;
return $this->call ('put', $this->api_url("documents", $signature), \%data);
}
# Get basic statistics for the current user
# @return [Hash] the values returned by Defensio
sub get_basic_stats{
my $this = shift;
return $this->call ('get', $this->api_url("basic-stats"));
}
# Get more exhaustive statistics for the current user
# @param [Hash] data The parameters to be sent to Defensio. Keys can either be Strings or Symbols
# @return [Hash] the values returned by Defensio
sub get_extended_stats{
my ($this, %data) = @_;
return $this->call('get', $this->api_url("extended-stats"), \%data);
}
# Filter a set of values based on a pre-defined dictionary
sub post_profanity_filter{
my ($this, %data) = @_;
return $this->call ('post', $this->api_url("profanity-filter"), \%data);
}
sub call{
my ($this, $method, $url, $data) = @_;
my $response, $data;
my $postdata = '';
foreach my $key ( keys %{$data} )
{
$postdata .= "$key=" .$codec->encode($data->{$key})."&";
}
foreach my $key ( keys %{$this} )
{
$postdata .= "$key=$this->{$key}&";
}
if(lc($method) =~ /get|delete|post|put/){
$response = $this->http_request(uc($method), $url, $postdata);
}
else{
$response = undef;
confess ("ArgumentError: Invalid HTTP method: $method");
}
if( $response =~ m/{.*}/)
{
$data = decode_json($response);
if($data->{$ROOT_NODE}->{status} =~ /fail/)
{
confess ("Request Failed: " . $data->{$ROOT_NODE}->{message});
}
return $data->{$ROOT_NODE};
}
confess("Invalid Response: $response");
return undef;
}
sub http_request{
my ($this, $method,$url, $data) = @_;
my $response;
$url .= $data if(lc($method) =~ /put/);
my $req = HTTP::Request->new($method => $url);
$req->content($data) if $data;
$response = $ua->request($req);
return $response->content;
}
sub api_url{
my ($this, $action, $id) = @_;
my $path = "$API_HOST/$API_VERSION/users/$this->{api_key}";
$path .= "/$action" if $action;
$path .= "/$id" if $id;
$path .= ".$FORMAT";
return $path;
}
1;