| 1 | package WebService::Simple; |
|---|
| 2 | |
|---|
| 3 | use warnings; |
|---|
| 4 | use strict; |
|---|
| 5 | use Carp; |
|---|
| 6 | use URI::Escape; |
|---|
| 7 | use LWP::UserAgent; |
|---|
| 8 | use URI::Escape; |
|---|
| 9 | use WebService::Simple::Response; |
|---|
| 10 | |
|---|
| 11 | our $VERSION = '0.01'; |
|---|
| 12 | |
|---|
| 13 | sub new { |
|---|
| 14 | my ($class, $base_url, $param) = @_; |
|---|
| 15 | croak "paramater base_url is required" unless $base_url; |
|---|
| 16 | my $self = bless { |
|---|
| 17 | base_url => $base_url, |
|---|
| 18 | param => $param, |
|---|
| 19 | }, $class; |
|---|
| 20 | $self; |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | sub get { |
|---|
| 24 | my ($self, $request_param) = @_; |
|---|
| 25 | my $url = $self->_make_url($request_param); |
|---|
| 26 | my $ua = LWP::UserAgent->new; |
|---|
| 27 | my $response = $ua->get($url); |
|---|
| 28 | croak "can't get the request" unless $response->is_success; |
|---|
| 29 | return $response; |
|---|
| 30 | } |
|---|
| 31 | sub _make_url{ |
|---|
| 32 | my ($self, $request_param) = @_; |
|---|
| 33 | my $base_url = $self->{base_url}; |
|---|
| 34 | my $url = $base_url =~ /\?$/ ? $base_url : $base_url . "?"; |
|---|
| 35 | my @params; |
|---|
| 36 | map {push(@params, "$_=" . URI::Escape::uri_escape_utf8($self->{param}->{$_}))} |
|---|
| 37 | keys %{$self->{param}}; |
|---|
| 38 | map {push(@params, "$_=" . URI::Escape::uri_escape_utf8($request_param->{$_}))} |
|---|
| 39 | keys %$request_param; |
|---|
| 40 | my $str = join("&",@params); |
|---|
| 41 | $url .= $str; |
|---|
| 42 | return $url; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | 1; |
|---|
| 46 | __END__ |
|---|
| 47 | |
|---|
| 48 | =head1 NAME |
|---|
| 49 | |
|---|
| 50 | WebService::Simple - Simple interface to any Web Service APIs |
|---|
| 51 | |
|---|
| 52 | =head1 VERSION |
|---|
| 53 | |
|---|
| 54 | This document describes WebService::Simple version 0.01 |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | =head1 SYNOPSIS |
|---|
| 58 | |
|---|
| 59 | use WebService::Simple; |
|---|
| 60 | |
|---|
| 61 | my $flickr = WebService::Simple->new( |
|---|
| 62 | "http://api.flickr.com/services/rest/", |
|---|
| 63 | { api_key => "your_api_key", } |
|---|
| 64 | ); |
|---|
| 65 | my $response = |
|---|
| 66 | $flickr->get( { method => "flickr.test.echo", name => "value" } ); |
|---|
| 67 | my $ref = $response->parse_xml( { forcearray => [], keyattr => [] } ); |
|---|
| 68 | print $ref->{name} . "\n"; |
|---|
| 69 | |
|---|
| 70 | =head1 DESCRIPTION |
|---|
| 71 | |
|---|
| 72 | WebService::Simple provides you a simple interface to any Web Servcie APIs |
|---|
| 73 | |
|---|
| 74 | =head1 METHODS |
|---|
| 75 | |
|---|
| 76 | =over 4 |
|---|
| 77 | |
|---|
| 78 | =item new(I<%args>) |
|---|
| 79 | |
|---|
| 80 | my $flickr = WebService::Simple->new( |
|---|
| 81 | "http://api.flickr.com/services/rest/", |
|---|
| 82 | { api_key => "your_api_key", } |
|---|
| 83 | ); |
|---|
| 84 | |
|---|
| 85 | Create and return a new WebService::Simple object. |
|---|
| 86 | "new" Method requires an base_url of Web Service API. |
|---|
| 87 | |
|---|
| 88 | =item get(I<%args>) |
|---|
| 89 | |
|---|
| 90 | my $response = |
|---|
| 91 | $flickr->get( { method => "flickr.test.echo", name => "value" } ); |
|---|
| 92 | |
|---|
| 93 | Get the WebService::Simple::Response object. |
|---|
| 94 | |
|---|
| 95 | =back |
|---|
| 96 | |
|---|
| 97 | =head1 AUTHOR |
|---|
| 98 | |
|---|
| 99 | Yusuke Wada C<< <yusuke@kamawada.com> >> |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | =head1 LICENCE AND COPYRIGHT |
|---|
| 103 | |
|---|
| 104 | Copyright (c) 2008, Yusuke Wada C<< <yusuke@kamawada.com> >>. All rights reserved. |
|---|
| 105 | |
|---|
| 106 | This module is free software; you can redistribute it and/or |
|---|
| 107 | modify it under the same terms as Perl itself. See L<perlartistic>. |
|---|
| 108 | |
|---|