| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::MorphIO; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # Pragmas. | 
| 4 | 3 |  |  | 3 |  | 63469 | use strict; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 66 |  | 
| 5 | 3 |  |  | 3 |  | 14 | use warnings; | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 79 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | # Modules. | 
| 8 | 3 |  |  | 3 |  | 2000 | use Class::Utils qw(set_params); | 
|  | 3 |  |  |  |  | 67695 |  | 
|  | 3 |  |  |  |  | 65 |  | 
| 9 | 3 |  |  | 3 |  | 2797 | use Encode qw(encode_utf8); | 
|  | 3 |  |  |  |  | 33614 |  | 
|  | 3 |  |  |  |  | 237 |  | 
| 10 | 3 |  |  | 3 |  | 21 | use Error::Pure qw(err); | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 114 |  | 
| 11 | 3 |  |  | 3 |  | 1927 | use IO::Barf qw(barf); | 
|  | 3 |  |  |  |  | 1681 |  | 
|  | 3 |  |  |  |  | 45 |  | 
| 12 | 3 |  |  | 3 |  | 2175 | use LWP::Simple qw(get); | 
|  | 3 |  |  |  |  | 170069 |  | 
|  | 3 |  |  |  |  | 26 |  | 
| 13 | 3 |  |  | 3 |  | 616 | use URI; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 70 |  | 
| 14 | 3 |  |  | 3 |  | 15 | use URI::Escape qw(uri_escape); | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 1519 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # Version. | 
| 17 |  |  |  |  |  |  | our $VERSION = 0.03; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | # Constructor. | 
| 20 |  |  |  |  |  |  | sub new { | 
| 21 | 7 |  |  | 7 | 1 | 8182 | my ($class, @params) = @_; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | # Create object. | 
| 24 | 7 |  |  |  |  | 14 | my $self = bless {}, $class; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | # Morph.io API key. | 
| 27 | 7 |  |  |  |  | 20 | $self->{'api_key'} = undef; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | # Project. | 
| 30 | 7 |  |  |  |  | 10 | $self->{'project'} = undef; | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | # Select. | 
| 33 | 7 |  |  |  |  | 14 | $self->{'select'} = 'SELECT * FROM data'; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | # Web URI of service. | 
| 36 | 7 |  |  |  |  | 10 | $self->{'web_uri'} = 'https://morph.io/'; | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | # Process params. | 
| 39 | 7 |  |  |  |  | 25 | set_params($self, @params); | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | # Check API key. | 
| 42 | 5 | 100 |  |  |  | 62 | if (! defined $self->{'api_key'}) { | 
| 43 | 1 |  |  |  |  | 4 | err "Parameter 'api_key' is required."; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | # Check project. | 
| 47 | 4 | 100 |  |  |  | 10 | if (! defined $self->{'project'}) { | 
| 48 | 1 |  |  |  |  | 4 | err "Parameter 'project' is required."; | 
| 49 |  |  |  |  |  |  | } | 
| 50 | 3 | 100 |  |  |  | 16 | if ($self->{'project'} !~ m/\/$/ms) { | 
| 51 | 1 |  |  |  |  | 3 | $self->{'project'} .= '/'; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | # Web URI. | 
| 55 | 3 | 100 |  |  |  | 11 | if ($self->{'web_uri'} !~ m/\/$/ms) { | 
| 56 | 1 |  |  |  |  | 3 | $self->{'web_uri'} .= '/'; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | # Object. | 
| 60 | 3 |  |  |  |  | 9 | return $self; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | # Get CSV file. | 
| 64 |  |  |  |  |  |  | sub csv { | 
| 65 | 0 |  |  | 0 | 1 |  | my ($self, $output_file) = @_; | 
| 66 |  |  |  |  |  |  | my $uri = URI->new($self->{'web_uri'}.$self->{'project'}. | 
| 67 |  |  |  |  |  |  | 'data.csv?key='.$self->{'api_key'}.'&query='. | 
| 68 | 0 |  |  |  |  |  | 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 |  |  |  |  |  |  | my $uri = URI->new($self->{'web_uri'}.$self->{'project'}. | 
| 76 | 0 |  |  |  |  |  | '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__ |