Permalink
Browse files

refactoring

  • Loading branch information...
1 parent f4df30b commit baad5b30d7cfb8209c09fd023187a83e9d2dedf9 @memememomo committed Feb 15, 2011
@@ -9,25 +9,15 @@ use WebService::Simple::ATND::Response;
our $VERSION = '0.01';
__PACKAGE__->config(
- base_url => "http://api.atnd.org/",
+ base_url => "http://api.atnd.org/",
+ response_parser => 'JSON',
);
sub get {
my ($self, $query) = @_;
-
- $query->{format} = 'xml';
-
- if ( ref $query eq 'WebService::Simple::ATND::Query::Events' ) {
- return WebService::Simple::ATND::Response->parse_events(
- $self->SUPER::get("events/", $query->as_hashref)
- );
- } elsif ( ref $query eq 'WebService::Simple::ATND::Query::Users' ) {
- return WebService::Simple::ATND::Response->parse_users(
- $self->SUPER::get("events/users/", $query->as_hashref)
- );
- } else {
- return undef;
- }
+ return WebService::Simple::ATND::Response->parse_response(
+ $self->SUPER::get($query->as_args),
+ );
}
sub query_events {
@@ -15,6 +15,7 @@ sub new {
$args{start} ||= 1;
$args{count} ||= 10;
+ $args{format} = 'json';
bless \%args, $class;
}
@@ -29,6 +30,8 @@ sub create_users {
return WebService::Simple::ATND::Query::Users->new(@_);
}
+sub api_base { return '' }
+
sub query_names { return []; }
sub as_hashref {
@@ -42,10 +45,16 @@ sub as_hashref {
}
$hash{start} = $self->{start};
$hash{count} = $self->{count};
+ $hash{format} = $self->{format};
return \%hash;
}
+sub as_args {
+ my ($self) = @_;
+ return ($self->api_base(), $self->as_hashref());
+}
+
1;
__END__
@@ -19,6 +19,8 @@ my @query_names = qw(
owner_twitter_id
);
+sub api_base { return "events/" }
+
sub query_names { return \@query_names; }
no strict 'refs';
@@ -15,6 +15,7 @@ my @query_names = qw(
owner_twitter_id
);
+sub api_base { return "events/users/" }
sub query_names { return \@query_names; }
@@ -1,8 +1,6 @@
package WebService::Simple::ATND::Response;
use strict;
use warnings;
-use WebService::Simple::ATND::Response::Events;
-use WebService::Simple::ATND::Response::Users;
our $VERSION = '0.01';
@@ -13,17 +11,11 @@ sub new {
return bless { %args }, $class;
}
-sub parse_events {
+sub parse_response {
my ($class, $response) = @_;
- return WebService::Simple::ATND::Response::Events->new($response);
+ return $class->new($response->parse_response);
}
-sub parse_users {
- my ($class, $response) = @_;
- return WebService::Simple::ATND::Response::Users->new($response);
-}
-
-
sub results_returned {
my ($self) = @_;
return $self->{results_returned};
@@ -39,19 +31,6 @@ sub events {
return $self->{events};
}
-sub _init_response {
- my ($hash) = @_;
-
- my %res = (
- results_returned => $hash->{results_returned}->{content},
- results_available => $hash->{results_available}->{content},
- events => [],
- );
-
- return \%res;
-}
-
-
1;
__END__
@@ -1,32 +0,0 @@
-package WebService::Simple::ATND::Response::Events;
-use strict;
-use warnings;
-use base qw(WebService::Simple::ATND::Response);
-
-sub new {
- my ($class, $response) = @_;
-
- my $hash = $response->parse_response;
- my %res = %{ WebService::Simple::ATND::Response::_init_response($hash) };
-
- if (ref $hash->{events}->{event} ne 'ARRAY') {
- $hash->{events}->{event} = [$hash->{events}->{event}];
- }
-
- for my $e (@{ $hash->{events}->{event} }) {
- my %event;
- for my $name (qw(event_id title catch description event_url started_at ended_at url limit address place lat lon owner_id owner_nickname owner_twitter_id accepted waiting updated_at)) {
- if ( ref $e->{$name} eq 'HASH' ) {
- $event{$name} = $e->{$name}->{content};
- } else {
- $event{$name} = $e->{$name};
- }
- }
- push @{ $res{events} }, \%event;
- }
-
- return $class->SUPER::new({ %res });
-}
-
-
-1;
@@ -1,50 +0,0 @@
-package WebService::Simple::ATND::Response::Users;
-use strict;
-use warnings;
-use base qw(WebService::Simple::ATND::Response);
-use Data::Dumper;
-
-
-sub new {
- my ($class, $response) = @_;
-
- my $hash = $response->parse_response;
- my %res = %{ WebService::Simple::ATND::Response::_init_response($hash) };
-
- if (ref $hash->{events}->{event} ne 'ARRAY') {
- $hash->{events}->{event} = [$hash->{events}->{event}];
- }
-
- for my $e (@{ $hash->{events}->{event} }) {
- my %event;
- for my $name (qw(event_id title event_url limit accepted waiting updated_at)) {
- if ( ref $e->{$name} eq 'HASH' ) {
- $event{$name} = $e->{$name}->{content};
- } else {
- $event{$name} = $e->{$name};
- }
- }
-
- $event{users} = [];
- if (ref $e->{users}->{user} eq 'ARRAY') {
- for my $u (@{ $e->{users}->{user} }) {
- my %users;
- for my $name (qw(user_id nickname twitter_id status)) {
- if ( ref $u->{$name} eq 'HASH' ) {
- $users{$name} = $u->{$name}->{content};
- } else {
- $users{$name} = $u->{$name};
- }
- }
- push @{ $event{users} }, \%users;
- }
- }
-
- push @{ $res{events} }, \%event;
- }
-
- return $class->SUPER::new({ %res });
-}
-
-
-1;
View
@@ -84,6 +84,7 @@ my $atnd = WebService::Simple::ATND->new;
my $res = $atnd->get($query);
like($res->events->[0]->{title}, qr/ATNDデモイベント/, "users_event_id");
+ ok(ref $res->events->[0]->{users} eq 'ARRAY');
}
done_testing();

0 comments on commit baad5b3

Please sign in to comment.