Froody::Response::String - create a response from a string


Froody documentation Contained in the Froody distribution.

Index


Code Index:

NAME

Top

Froody::Response::String - create a response from a string

SYNOPSIS

Top

  my $response = Froody::Response::String->new()
                                         ->structure($froody_method)
                                         ->set_string($string)
  print $response->render;

DESCRIPTION

Top

This is a concrete implementation of Froody::Response. It takes its input from a valid Perl string, which you can set with set_string:

  my $frs = Froody::Response::String->new();
  $frs->structure($froody_method);
  $frs->set_string( <<ENDOFRSP );
  <rsp stat="ok">
    <monger>L\x{e9}on Brocard</monger>
  </rsp>
  ENDOFRSP

Note that there's no XML declaration header there - one will be added automatically. You can also set the bytes directly:

  my $frs = Froody::Response::String->new();
  $frs->structure($froody_method);
  $frs->set_bytes( <<ENDOFRSP );
  <?xml version="1.0" encoding="utf-8" ?>
  <rsp stat="ok">
    <monger>L\x{c3}\x{a9}on Brocard</monger>
  </rsp>
  ENDOFRSP

In this case you are required to include the XML declaration header (as we can't create one for you as we have no idea what encoding scheme the bytes are using)

Converting other Responses to Froody::Response::String objects

Once you've loaded this class you can automatically convert other Froody::Response class instances to Froody::Response::String objects with the as_string method.

  use Froody::Response::String;
  my $string = Froody::Response::Terse
      ->new()
      ->structure($froody_method)
      ->content(...)
      ->as_string;
  print ref($string);  # prints "Froody::Response::String"

BUGS

Top

None known.

Please report any bugs you find via the CPAN RT system. http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody

AUTHOR

Top

Copyright Fotango 2005. All rights reserved.

Please see the main Froody documentation for details of who has worked on this project.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Top

Froody, Froody::Response


Froody documentation Contained in the Froody distribution.
package Froody::Response::String;
use base qw(Froody::Response);

use warnings;
use strict;

use Encode;

# set_string is documented
sub set_string {
  my $self = shift;
  $self->{bytes} = qq{<?xml version="1.0" encoding="utf-8" ?>\n};
  $self->{bytes} .= Encode::encode("utf-8", shift());
  return $self;
}

# set_bytes is documented
sub set_bytes {
  my $self = shift;
  $self->{bytes} = shift;
  return $self;
}

# just return the bytes
sub render { my $self = shift; $self->{bytes} }

# rendering this class
# as_string is documented
sub as_string { return $_[0] }
sub Froody::Response::as_string
{
  my $self = shift;
  my $str = Froody::Response::String->new();
  $str->set_bytes($self->render);
  return $str;
}

1;