~percona-toolkit-dev/percona-toolkit/pt-stalk-sleep-collect-option

« back to all changes in this revision

Viewing changes to lib/Percona/WebAPI/Representation.pm

  • Committer: Daniel Nichter
  • Date: 2013-06-19 21:23:55 UTC
  • mfrom: (582.1.5 release-2.2.3)
  • Revision ID: daniel@percona.com-20130619212355-nf6bmx23j3b76afe
Tags: 2.2.3
Merge release-2.2.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This program is copyright 2012-2013 Percona Inc.
 
2
# Feedback and improvements are welcome.
 
3
#
 
4
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 
5
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 
6
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
7
#
 
8
# This program is free software; you can redistribute it and/or modify it under
 
9
# the terms of the GNU General Public License as published by the Free Software
 
10
# Foundation, version 2; OR the Perl Artistic License.  On UNIX and similar
 
11
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
 
12
# licenses.
 
13
#
 
14
# You should have received a copy of the GNU General Public License along with
 
15
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
16
# Place, Suite 330, Boston, MA  02111-1307  USA.
 
17
# ###########################################################################
 
18
# Percona::WebAPI::Representation package
 
19
# ###########################################################################
 
20
{
 
21
package Percona::WebAPI::Representation;
 
22
 
 
23
eval {
 
24
   require JSON;
 
25
};
 
26
 
 
27
require Exporter;
 
28
our @ISA       = qw(Exporter);
 
29
our @EXPORT_OK = qw(
 
30
   as_hashref
 
31
   as_json
 
32
   as_config
 
33
);
 
34
 
 
35
sub as_hashref {
 
36
   my ($resource, %args) = @_;
 
37
 
 
38
   # Copy the object into a new hashref.
 
39
   my $as_hashref = { %$resource };
 
40
 
 
41
   # Delete the links because they're just for client-side use
 
42
   # and the caller should be sending this object, not getting it.
 
43
   # But sometimes for testing we want to keep the links.
 
44
   if ( !defined $args{with_links} || !$args{with_links} ) {
 
45
      delete $as_hashref->{links};
 
46
   }
 
47
 
 
48
   return $as_hashref;
 
49
}
 
50
 
 
51
sub as_json {
 
52
   my ($resource, %args) = @_;
 
53
 
 
54
   my $json = $args{json} || JSON->new;
 
55
   $json->allow_blessed([]);
 
56
   $json->convert_blessed([]);
 
57
 
 
58
   my $text = $json->encode(
 
59
      ref $resource eq 'ARRAY' ? $resource : as_hashref($resource, %args)
 
60
   );
 
61
   if ( $args{json} && $text ) {  # for testing
 
62
      chomp($text);
 
63
      $text .= "\n";
 
64
   }
 
65
   return $text;
 
66
}
 
67
 
 
68
sub as_config {
 
69
   my $resource = shift;
 
70
   if ( !$resource->isa('Percona::WebAPI::Resource::Config') ) {
 
71
      die "Only Config resources can be represented as config.\n";
 
72
   }
 
73
   my $as_hashref = as_hashref($resource);
 
74
   my $options    = $as_hashref->{options};
 
75
   my $config     = join("\n",
 
76
      map { defined $options->{$_} ?  "$_=$options->{$_}" : "$_" }
 
77
      sort keys %$options
 
78
   ) . "\n";
 
79
   return $config;
 
80
}
 
81
 
 
82
1;
 
83
}
 
84
# ###########################################################################
 
85
# End Percona::WebAPI::Representation package
 
86
# ###########################################################################