| 1 | package Search::Typesense; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: Perl interface to the Typesense search engine | ||||||||||
| 4 | |||||||||||
| 5 | 4 | ▶ | use v5.16.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 4 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | with 'Search::Typesense::Role::Request'; | |||||||||
| 9 | |||||||||||
| 10 | 4 | ▶ | use Mojo::JSON qw(decode_json encode_json); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 4 | ▶ | use Mojo::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 4 | ▶ | use Mojo::URL; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 4 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 4 | ▶ | use Search::Typesense::Document (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 4 | ▶ | use Search::Typesense::Collection; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 4 | ▶ | use Search::Typesense::Version; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 4 | ▶ | use Search::Typesense::Types qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | ArrayRef | ||||||||||
| 20 | Bool | ||||||||||
| 21 | Enum | ||||||||||
| 22 | HashRef | ||||||||||
| 23 | InstanceOf | ||||||||||
| 24 | NonEmptyStr | ||||||||||
| 25 | PositiveInt | ||||||||||
| 26 | compile | ||||||||||
| 27 | 4 | ); | |||||||||
| 28 | |||||||||||
| 29 | |||||||||||
| 30 | 4 | our $VERSION = '0.08'; | |||||||||
| 31 | |||||||||||
| 32 | has collections => ( | ||||||||||
| 33 | is => 'lazy', | ||||||||||
| 34 | isa => InstanceOf ['Search::Typesense::Collection'], | ||||||||||
| 35 | init_arg => undef, | ||||||||||
| 36 | handles => [qw/search/], | ||||||||||
| 37 | 3 | ▶ | builder => sub { | ||||||||
|
Subroutine
_build_collections: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 3 | my $self = shift; | |||||||||
| 39 | 3 | return Search::Typesense::Collection->new( | |||||||||
| 40 | user_agent => $self->_ua, | ||||||||||
| 41 | url => $self->_url_base, | ||||||||||
| 42 | ); | ||||||||||
| 43 | }, | ||||||||||
| 44 | 4 | ); | |||||||||
| 45 | |||||||||||
| 46 | has documents => ( | ||||||||||
| 47 | is => 'lazy', | ||||||||||
| 48 | isa => InstanceOf ['Search::Typesense::Document'], | ||||||||||
| 49 | init_arg => undef, | ||||||||||
| 50 | 0 | ▶ | builder => sub { | ||||||||
|
Subroutine
_build_documents: not called CC=1 SCAR=1.2
Subroutine
__ANON__: called CC=1 SCAR=1.2
| |||||||||||
| 51 | 0 | my $self = shift; | |||||||||
| 52 | 0 | return Search::Typesense::Document->new( | |||||||||
| 53 | user_agent => $self->_ua, | ||||||||||
| 54 | url => $self->_url_base, | ||||||||||
| 55 | ); | ||||||||||
| 56 | }, | ||||||||||
| 57 | 4 | ); | |||||||||
| 58 | |||||||||||
| 59 | # this sub without a body is called a "forward declaration" and it allows the | ||||||||||
| 60 | # requires() in Search::Typesense::Role::Request to realize that we really do | ||||||||||
| 61 | # provide these methods. In this case, it also prevents the following error | ||||||||||
| 62 | # from being generated by the roles consumed: | ||||||||||
| 63 | # | ||||||||||
| 64 | # Error: You cannot overwrite a locally defined method (_ua) with a reader | ||||||||||
| 65 | # at .../lib/Search/Typesense.pm line 121. | ||||||||||
| 66 | |||||||||||
| 67 | sub _ua; | ||||||||||
| 68 | has _ua => ( | ||||||||||
| 69 | is => 'lazy', | ||||||||||
| 70 | isa => InstanceOf ['Mojo::UserAgent'], | ||||||||||
| 71 | 1 | ▶ | builder => sub { | ||||||||
|
Subroutine
_build__ua: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 1 | my $self = shift; | |||||||||
| 73 | 1 | my $ua = Mojo::UserAgent->new; | |||||||||
| 74 | 1 | my $key = $self->api_key; | |||||||||
| 75 | $ua->on( | ||||||||||
| 76 | 3 | ▶ | start => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 77 | 3 | my ( $ua, $tx ) = @_; | |||||||||
| 78 | 3 | $tx->req->headers->header( | |||||||||
| 79 | 'Content-Type' => 'application/json' ) | ||||||||||
| 80 | ->header( 'X-TYPESENSE-API-KEY' => $key ); | ||||||||||
| 81 | } | ||||||||||
| 82 | 1 | ); | |||||||||
| 83 | 1 | return $ua; | |||||||||
| 84 | }, | ||||||||||
| 85 | 4 | ); | |||||||||
| 86 | |||||||||||
| 87 | sub _url_base; | ||||||||||
| 88 | has _url_base => ( | ||||||||||
| 89 | is => 'lazy', | ||||||||||
| 90 | isa => InstanceOf ['Mojo::URL'], | ||||||||||
| 91 | 3 | ▶ | builder => sub { | ||||||||
|
Subroutine
_build__url_base: called CC=2 SCAR=7.0
| |||||||||||
| 92 | 3 | my $self = shift; | |||||||||
| 93 | 3 | my $url = Mojo::URL->new; | |||||||||
| 94 | 3 | ▶ | $url->scheme( $self->use_https ? 'https' : 'http' ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 3 | $url->host( $self->host ); | |||||||||
| 96 | 3 | $url->port( $self->port ); | |||||||||
| 97 | 3 | return $url; | |||||||||
| 98 | }, | ||||||||||
| 99 | 4 | ); | |||||||||
| 100 | |||||||||||
| 101 | 4 | has use_https => ( | |||||||||
| 102 | is => 'ro', | ||||||||||
| 103 | isa => Bool, | ||||||||||
| 104 | required => 1, | ||||||||||
| 105 | default => 1, | ||||||||||
| 106 | ); | ||||||||||
| 107 | |||||||||||
| 108 | 4 | has api_key => ( | |||||||||
| 109 | is => 'ro', | ||||||||||
| 110 | isa => NonEmptyStr, | ||||||||||
| 111 | required => 1, | ||||||||||
| 112 | ); | ||||||||||
| 113 | |||||||||||
| 114 | 4 | has host => ( | |||||||||
| 115 | is => 'ro', | ||||||||||
| 116 | isa => NonEmptyStr, | ||||||||||
| 117 | required => 1, | ||||||||||
| 118 | ); | ||||||||||
| 119 | |||||||||||
| 120 | 4 | has port => ( | |||||||||
| 121 | is => 'ro', | ||||||||||
| 122 | isa => PositiveInt, | ||||||||||
| 123 | default => 8108, | ||||||||||
| 124 | ); | ||||||||||
| 125 | |||||||||||
| 126 | 3 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 127 | 3 | my $self = shift; | |||||||||
| 128 | 3 | $self->assert_is_running; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | |||||||||||
| 132 | 3 | ▶ | sub assert_is_running { | ||||||||
|
Subroutine
assert_is_running: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 133 | 3 | my $self = shift; | |||||||||
| 134 | 3 | $self->_GET( path => ['health'] ); | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | |||||||||||
| 138 | 1 | ▶ | sub typesense_version { | ||||||||
|
Subroutine
typesense_version: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 139 | 1 | my $self = shift; | |||||||||
| 140 | 1 | ▶ | my $result = $self->_GET( path => ['debug'] ) or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | return Search::Typesense::Version->new( | ||||||||||
| 142 | 1 | version_string => $result->{version} ); | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 4 | 1; | |||||||||
| 146 | |||||||||||
| 147 | __END__ | ||||||||||