summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-09-13 16:14:34 +0200
committerfranck cuny <franck@lumberjaph.net>2010-09-13 16:14:34 +0200
commit164f7f5b484f5236407904acf27de4a055503022 (patch)
treeffd3579481dddd3a81fe9cc529139cc9b1b1eb82
parentsimple oauth middleware (diff)
downloadnet-http-spore-164f7f5b484f5236407904acf27de4a055503022.tar.gz
some POD and examples
-rw-r--r--eg/github.pl43
-rw-r--r--eg/twitter.pl44
-rw-r--r--lib/Net/HTTP/Spore.pm41
-rw-r--r--lib/Net/HTTP/Spore/Core.pm4
4 files changed, 127 insertions, 5 deletions
diff --git a/eg/github.pl b/eg/github.pl
new file mode 100644
index 0000000..4053d67
--- /dev/null
+++ b/eg/github.pl
@@ -0,0 +1,43 @@
+use strict;
+use warnings;
+
+use Net::HTTP::Spore;
+use Getopt::Long;
+
+use Config::GitLike::Git;
+use Git::Repository;
+
+GetOptions(
+    'spec=s' => \my $specification,
+    'name=s' => \my $name,
+    'desc=s' => \my $desc,
+);
+
+print ">> creating repository $name on github\n";
+
+my $c = Config::GitLike::Git->new();
+$c->load;
+
+my $login = $c->get(key => 'github.user');
+my $token = $c->get(key => 'github.token');
+
+my $github = Net::HTTP::Spore->new_from_spec($specification);
+$github->enable('Format::JSON');
+$github->enable(
+    'Auth::Basic',
+    username => $login . '/token',
+    password => $token,
+);
+
+my $remote = "git\@github.com:" . $login . "/" . $name . ".git";
+
+my $res = $github->create_repo(format => 'json', payload => {name => $name, description => $desc});
+
+print ">> repository $remote created\n";
+
+my $r = Git::Repository->create(init => $name);
+my @cmd = ('remote', 'add', 'origin', $remote);
+$r->run(@cmd);
+
+print ">> repository cloned to $name\n";
+print ">> done!\n";
diff --git a/eg/twitter.pl b/eg/twitter.pl
new file mode 100644
index 0000000..965e5c8
--- /dev/null
+++ b/eg/twitter.pl
@@ -0,0 +1,44 @@
+use strict;
+use warnings;
+use Net::HTTP::Spore;
+
+use Encode;
+use Getopt::Long;
+
+GetOptions(
+    'spec=s'            => \my $specification,
+    'consumer_key=s'    => \my $consumer_key,
+    'consumer_secret=s' => \my $consumer_secret,
+    'token=s'           => \my $token,
+    'token_secret=s'    => \my $token_secret,
+);
+
+my $client = Net::HTTP::Spore->new_from_spec($specification);
+
+$client->enable('Format::JSON');
+$client->enable(
+    'Auth::OAuth',
+    consumer_key    => $consumer_key,
+    consumer_secret => $consumer_secret,
+    token           => $token,
+    token_secret    => $token_secret,
+);
+
+my $timeline = $client->public_timeline( format => 'json' );
+if ( $timeline->status == 200 ) {
+    my $tweets = $timeline->body;
+    print ">> Timeline\n";
+    foreach my $tweet (@$tweets) {
+        print $tweet->{user}->{screen_name} . " says " . encode_utf8($tweet->{text}) . "\n";
+    }
+    print "\n\n";
+}
+
+my $friends_timeline = $client->friends_timeline(format => 'json', include_rts => 1);
+if  ($friends_timeline->code == 200) {
+    my $tweets = $friends_timeline->body;
+    print ">> Friend timeline\n";
+    foreach my $tweet (@$tweets) {
+        print $tweet->{user}->{screen_name} . " says " . encode_utf8($tweet->{text}) . "\n";
+    }
+}
diff --git a/lib/Net/HTTP/Spore.pm b/lib/Net/HTTP/Spore.pm
index 88ce418..127754f 100644
--- a/lib/Net/HTTP/Spore.pm
+++ b/lib/Net/HTTP/Spore.pm
@@ -1,5 +1,7 @@
 package Net::HTTP::Spore;
 
+# ABSTRACT: SPORE client
+
 use Moose;
 
 use IO::All;
@@ -14,7 +16,7 @@ our $VERSION = 0.01;
 sub new_from_spec {
     my ($class, $spec_file, %args) = @_;
 
-    if (! -f $spec_file) {
+    unless (-f $spec_file) {
         Carp::confess ("$spec_file does not exists");
     }
 
@@ -29,13 +31,15 @@ sub new_from_spec {
         Carp::confess( "unable to parse JSON spec: " . $_ );
     };
 
-    my $spore_class =
+    my ($spore_class, $spore_object);
+
+    # XXX should we let the possibility to override this super class, or add
+    # another superclasses?
+    $spore_class =
       Class::MOP::Class->create_anon_class(
           superclasses => ['Net::HTTP::Spore::Core']);
 
-    my $spore_object;
     try {
-
         my $api_base_url;
         if ( $spec->{api_base_url} && !$args{api_base_url} ) {
             $args{api_base_url} = $spec->{api_base_url};
@@ -64,5 +68,32 @@ sub _add_methods {
     $class;
 }
 
-
 1;
+
+=head1 SYNOPSIS
+
+    my $client = Net::HTTP::Spore->new_from_spec('twitter.json');
+
+    $client->enable('Auth::OAuth');
+    $client->enable('Format::JSON');
+
+    my $timeline = $client->public_timeline(format => 'json');
+    if ($timeline->status == 200) {
+        my $tweets = $timeline->body;
+        foreach my $tweet (@$tweets) {
+            print $tweet->{user}->{screen_name}. " says ".$tweet->{text}."\n";
+        }
+    }
+
+    my $friends_timeline = $client->friends_timeline(format => 'json');
+
+=head1 DESCRIPTION
+
+
+=head2 METHODS
+
+=over 4
+
+=item B<new_from_spec>($specification_file, %args)
+
+=back
diff --git a/lib/Net/HTTP/Spore/Core.pm b/lib/Net/HTTP/Spore/Core.pm
index 2251af8..bf368f8 100644
--- a/lib/Net/HTTP/Spore/Core.pm
+++ b/lib/Net/HTTP/Spore/Core.pm
@@ -3,3 +3,7 @@ package Net::HTTP::Spore::Core;
 use Net::HTTP::Spore::Meta;
 
 1;
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION