line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Wedata::Item; |
2
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
115
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
4
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
64
|
|
5
|
1
|
|
|
1
|
|
7
|
use base qw/Class::Accessor::Fast/; |
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
1161
|
|
6
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/database name resource_url updated_at created_at created_by/); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub new { |
9
|
|
|
|
|
|
|
my($class, %params) = @_; |
10
|
|
|
|
|
|
|
my $self = bless { |
11
|
|
|
|
|
|
|
data => $params{data} || {}, |
12
|
|
|
|
|
|
|
}, $class; |
13
|
|
|
|
|
|
|
foreach my $k (qw/database name resource_url updated_at created_at created_by/) { |
14
|
|
|
|
|
|
|
$self->set($k, $params{$k}) if ($params{$k}); |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
$self; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub set_data { |
20
|
|
|
|
|
|
|
my($self, $key, $value) = @_; |
21
|
|
|
|
|
|
|
$self->{data}->{$key} = $value; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub update { |
25
|
|
|
|
|
|
|
my($self, @data) = @_; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $item_id = _id_from_resource_url($self->resource_url); |
28
|
|
|
|
|
|
|
my $params = { |
29
|
|
|
|
|
|
|
api_key => $self->database->{api_key}, |
30
|
|
|
|
|
|
|
database_name => $self->database->{name}, |
31
|
|
|
|
|
|
|
id => $item_id, |
32
|
|
|
|
|
|
|
data => {@data}, |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
my $req = _make_update_request($params); |
35
|
|
|
|
|
|
|
my $response = $self->database->{ua}->request($req); |
36
|
|
|
|
|
|
|
if ($response->is_success) { |
37
|
|
|
|
|
|
|
$self->{data} = {}; |
38
|
|
|
|
|
|
|
while (my($k, $v) = each(%{ $params->{data} })) { |
39
|
|
|
|
|
|
|
$self->set_data($k, $v); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
else { |
43
|
|
|
|
|
|
|
croak 'Faild to update item:' . $response->status_line; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _make_update_request { |
48
|
|
|
|
|
|
|
my($params) = @_; |
49
|
|
|
|
|
|
|
my $kv = []; |
50
|
|
|
|
|
|
|
while (my($k, $v) = each(%{ $params->{data} })) { |
51
|
|
|
|
|
|
|
push @$kv, "data[$k]=$v"; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
my $content = ''; |
54
|
|
|
|
|
|
|
$content = join '&', |
55
|
|
|
|
|
|
|
"api_key=$params->{api_key}", |
56
|
|
|
|
|
|
|
@$kv |
57
|
|
|
|
|
|
|
; |
58
|
|
|
|
|
|
|
# my $url = join '/', $WebService::Wedata::URL_BASE, 'databases', $params->{database_name}, 'items', $params->{id}; |
59
|
|
|
|
|
|
|
my $url = join '/', $WebService::Wedata::URL_BASE, 'items', $params->{id}; |
60
|
|
|
|
|
|
|
my $req = HTTP::Request->new( |
61
|
|
|
|
|
|
|
PUT => $url, |
62
|
|
|
|
|
|
|
HTTP::Headers->new( |
63
|
|
|
|
|
|
|
'content-type' => 'application/x-www-form-urlencoded', |
64
|
|
|
|
|
|
|
'content-length' => length($content), |
65
|
|
|
|
|
|
|
), |
66
|
|
|
|
|
|
|
$content, |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
$req; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub delete { |
72
|
|
|
|
|
|
|
my($self) = @_; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $item_id = _id_from_resource_url($self->resource_url); |
75
|
|
|
|
|
|
|
my $params = { |
76
|
|
|
|
|
|
|
api_key => $self->database->{api_key}, |
77
|
|
|
|
|
|
|
id => $item_id, |
78
|
|
|
|
|
|
|
}; |
79
|
|
|
|
|
|
|
my $req = _make_delete_request($params); |
80
|
|
|
|
|
|
|
my $response = $self->database->{ua}->request($req); |
81
|
|
|
|
|
|
|
if ($response->is_success) { |
82
|
|
|
|
|
|
|
$self->name(''); |
83
|
|
|
|
|
|
|
$self->database({}); |
84
|
|
|
|
|
|
|
$self->{data} = []; |
85
|
|
|
|
|
|
|
return; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
else { |
88
|
|
|
|
|
|
|
croak 'Faild to delete item:' . $response->status_line; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _make_delete_request { |
93
|
|
|
|
|
|
|
my($params) = @_; |
94
|
|
|
|
|
|
|
my $content = "api_key=$params->{api_key}"; |
95
|
|
|
|
|
|
|
my $url = join '/', $WebService::Wedata::URL_BASE, 'items', $params->{id}; |
96
|
|
|
|
|
|
|
my $req = HTTP::Request->new( |
97
|
|
|
|
|
|
|
DELETE => $url, |
98
|
|
|
|
|
|
|
HTTP::Headers->new( |
99
|
|
|
|
|
|
|
'content-type' => 'application/x-www-form-urlencoded', |
100
|
|
|
|
|
|
|
'content-length' => length($content), |
101
|
|
|
|
|
|
|
), |
102
|
|
|
|
|
|
|
$content, |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
$req; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub _id_from_resource_url { |
108
|
|
|
|
|
|
|
my $url = shift; |
109
|
|
|
|
|
|
|
$url =~ s!^http://wedata\.net/items/(\d+)$!$1!; |
110
|
|
|
|
|
|
|
$url; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
1; |
114
|
|
|
|
|
|
|
__END__ |