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