[Jifty-commit] r4504 - in Net-Jifty: .

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Nov 20 14:20:07 EST 2007


Author: sartak
Date: Tue Nov 20 14:20:07 2007
New Revision: 4504

Modified:
   Net-Jifty/   (props changed)
   Net-Jifty/lib/Net/Jifty.pm

Log:
 r45381 at onn:  sartak | 2007-11-20 14:20:01 -0500
 More improvements. get_sid, escape, better ->method, etc


Modified: Net-Jifty/lib/Net/Jifty.pm
==============================================================================
--- Net-Jifty/lib/Net/Jifty.pm	(original)
+++ Net-Jifty/lib/Net/Jifty.pm	Tue Nov 20 14:20:07 2007
@@ -112,7 +112,8 @@
 sub BUILD {
     my $self = shift;
 
-    $self->login;
+    $self->login
+        unless $self->sid;
 }
 
 =head2 login
@@ -139,6 +140,8 @@
 
     confess "Unable to log in."
         if $result->{failure};
+
+    $self->get_sid;
 }
 
 =head2 call Action, ARGS
@@ -173,9 +176,13 @@
 =head2 method Method, URL[, ArgsRef]
 
 This will perform a GET, POST, PUT, DELETE, etc using the internal
-L<LWP::UserAgent> object. Your URL already has C<http://your.site/=/> prepended
-to it, and C<.yml> appended to it, so you only need to pass something like
-C<model/YourApp.Model.Foo/name>.
+L<LWP::UserAgent> object.
+
+Your URL may be a string or an array reference (which will have its parts
+properly escaped and joined with C</>). Your URL already has
+C<http://your.site/=/> prepended to it, and C<.yml> appended to it, so you only
+need to pass something like C<model/YourApp.Model.Foo/name>, or
+C<["model", "YourApp.Model.Foo", "name"]>.
 
 This will return the data structure returned by the Jifty application, or throw
 an error.
@@ -184,18 +191,36 @@
 
 sub method {
     my $self   = shift;
-    my $method = shift;
+    my $method = lc(shift);
     my $url    = shift;
     my $args   = shift;
 
+    $url = $self->join_url(@$url)
+        if ref($url) eq 'ARRAY';
+
     # remove trailing /
     $url =~ s{/+$}{};
 
-    #my $res = $self->ua->$method(
-    my $res; use DDS; Dump(
-        $self->site . '/=/' . $url . '.yml',
-        $args
-    );
+    my $res;
+
+    if ($method eq 'get' || $method eq 'head') {
+        my $uri = $self->site . '/=/' . $url . '.yml?';
+
+        while (my ($key, $value) = each %$args) {
+            $uri .= '&' . join '=', map { $self->escape($_) } $key, $value;
+        }
+
+        # it's easier than keeping a flag of "did we already append?"
+        $uri =~ s/\?&/?/;
+
+        $res = $self->ua->$method($uri);
+    }
+    else {
+        $res = $self->ua->$method(
+            $self->site . '/=/' . $url . '.yml',
+            $args
+        );
+    }
 
     if ($res->is_success) {
         return YAML::Load( Encode::decode_utf8($res->content) );
@@ -216,6 +241,18 @@
     $self->method('post', @_);
 }
 
+=head2 get URL, ArgsRef
+
+This will get the specified URL, using the arguments. See the documentation for
+C<method>.
+
+=cut
+
+sub get {
+    my $self = shift;
+    $self->method('get', @_);
+}
+
 =head2 act Action, Args
 
 Perform the specified action, using the specified arguments.
@@ -227,7 +264,7 @@
     my $action = $self->canonicalize_action(shift);
     my %args   = @_;
 
-    return $self->post("action/$action", \%args);
+    return $self->post(["action", $action], \%args);
 }
 
 =head2 create Model, FIELDS
@@ -241,7 +278,7 @@
     my $model = $self->canonicalize_model(shift);
     my %fields = @_;
 
-    return $self->post("model/$model", \%fields);
+    return $self->post(["model", $model], \%fields);
 }
 
 =head2 delete Model, Key => Value
@@ -256,7 +293,7 @@
     my $key    = shift;
     my $value  = shift;
 
-    return $self->method(delete => "model/$model/$key/$value");
+    return $self->method(delete => ["model", $model, $key, $value]);
 }
 
 =head2 update Model, Key => Value, FIELDS
@@ -272,7 +309,22 @@
     my $value  = shift;
     my %fields = @_;
 
-    return $self->method(put => "model/$model/$key/$value", \%fields);
+    return $self->method(put => ["model", $model, $key, $value], \%fields);
+}
+
+=head2 read Model, Key => Value
+
+Find some Model where Key => Value and return it.
+
+=cut
+
+sub read {
+    my $self   = shift;
+    my $model  = $self->canonicalize_model(shift);
+    my $key    = shift;
+    my $value  = shift;
+
+    return $self->get(["model", $model, $key, $value]);
 }
 
 =head2 canonicalize_package Type, Package
@@ -316,6 +368,49 @@
     return $self->canonicalize_package('Model', @_);
 }
 
+=head2 get_sid
+
+Retrieves the SID from the LWP::UserAgent object
+
+=cut
+
+sub get_sid {
+    my $self = shift;
+    my $cookie = $self->cookie_name;
+
+    my $sid;
+    $sid = $1
+        if $self->ua->cookie_jar->as_string =~ /\Q$cookie\E=([^;]+)/;
+
+    $self->sid($sid);
+}
+
+=head2 join_url Fragments
+
+Encodes the fragments and joins them with C</>.
+
+=cut
+
+sub join_url {
+    my $self = shift;
+
+    return join '/', map { $self->escape($_) } @_
+}
+
+=head2 escape Strings
+
+URI escapes each string
+
+=cut
+
+sub escape {
+    my $self = shift;
+
+    return map { s/([^a-zA-Z0-9_.!~*'()-])/uc sprintf("%%%02X", ord $1)/eg; $_ }
+           map { Encode::encode_utf8($_) }
+           @_
+}
+
 =head1 SEE ALSO
 
 L<Jifty>, L<Net::Hiveminder>


More information about the Jifty-commit mailing list