File Coverage

blib/lib/WebService/MorphIO.pm
Criterion Covered Total %
statement 43 55 78.1
branch 8 10 80.0
condition n/a
subroutine 10 13 76.9
pod 3 3 100.0
total 64 81 79.0


line stmt bran cond sub pod time code
1             package WebService::MorphIO;
2              
3             # Pragmas.
4 4     4   132764 use strict;
  4         8  
  4         139  
5 4     4   18 use warnings;
  4         9  
  4         117  
6              
7             # Modules.
8 4     4   22725 use Class::Utils qw(set_params);
  4         97322  
  4         87  
9 4     4   17623 use Encode qw(encode_utf8);
  4         110839  
  4         432  
10 4     4   84 use Error::Pure qw(err);
  4         10  
  4         186  
11 4     4   6043 use IO::Barf qw(barf);
  4         6121  
  4         90  
12 4     4   6881 use LWP::Simple qw(get);
  4         432359  
  4         147  
13 4     4   913 use URI;
  4         10  
  4         104  
14 4     4   24 use URI::Escape qw(uri_escape);
  4         8  
  4         2371  
15              
16             # Version.
17             our $VERSION = 0.02;
18              
19             # Constructor.
20             sub new {
21 7     7 1 10209 my ($class, @params) = @_;
22              
23             # Create object.
24 7         23 my $self = bless {}, $class;
25              
26             # Morph.io API key.
27 7         30 $self->{'api_key'} = undef;
28              
29             # Project.
30 7         13 $self->{'project'} = undef;
31              
32             # Select.
33 7         16 $self->{'select'} = 'SELECT * FROM data';
34              
35             # Web URI of service.
36 7         14 $self->{'web_uri'} = 'https://morph.io/';
37              
38             # Process params.
39 7         30 set_params($self, @params);
40              
41             # Check API key.
42 5 100       77 if (! defined $self->{'api_key'}) {
43 1         4 err "Parameter 'api_key' is required.";
44             }
45              
46             # Check project.
47 4 100       11 if (! defined $self->{'project'}) {
48 1         6 err "Parameter 'project' is required.";
49             }
50 3 100       20 if ($self->{'project'} !~ m/\/$/ms) {
51 1         4 $self->{'project'} .= '/';
52             }
53              
54             # Web URI.
55 3 100       15 if ($self->{'web_uri'} !~ m/\/$/ms) {
56 1         7 $self->{'web_uri'} .= '/';
57             }
58              
59             # Object.
60 3         13 return $self;
61             }
62              
63             # Get CSV file.
64             sub csv {
65 0     0 1   my ($self, $output_file) = @_;
66 0           my $uri = URI->new($self->{'web_uri'}.$self->{'project'}.
67             'data.csv?key='.$self->{'api_key'}.'&query='.
68             uri_escape($self->{'select'}));
69 0           return $self->_save($uri, $output_file);
70             }
71              
72             # Get sqlite file.
73             sub sqlite {
74 0     0 1   my ($self, $output_file) = @_;
75 0           my $uri = URI->new($self->{'web_uri'}.$self->{'project'}.
76             'data.sqlite?key='.$self->{'api_key'});
77 0           return $self->_save($uri, $output_file);
78             }
79              
80             # Save file.
81             sub _save {
82 0     0     my ($self, $uri, $output_file) = @_;
83 0           my $content = get($uri->as_string);
84 0 0         if (! $content) {
85 0           err "Cannot get '".$uri->as_string."'.";
86             }
87 0           barf($output_file, encode_utf8($content));
88 0           return;
89             }
90              
91             1;
92              
93             __END__