| 1 | package WebService::OANDA::ExchangeRates; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Types::Standard qw{ ArrayRef Int Str StrMatch }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Type::Utils qw{ declare as where coerce from via enum}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Types::URI qw{ Uri }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use WebService::OANDA::ExchangeRates::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | $VERSION = "0.003"; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 5 | has base_url => ( | |||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||
| 16 | isa => Uri, | ||||||||||||||||||||||||||||
| 17 | coerce => Uri->coercion, | ||||||||||||||||||||||||||||
| 18 | default => 'https://www.oanda.com/rates/api/v1/', | ||||||||||||||||||||||||||||
| 19 | required => 1, | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | 5 | has proxy => ( | |||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||
| 23 | isa => Uri, | ||||||||||||||||||||||||||||
| 24 | coerce => Uri->coercion, | ||||||||||||||||||||||||||||
| 25 | predicate => 1, | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | 5 | has timeout => ( is => 'ro', isa => Int, predicate => 1 ); | |||||||||||||||||||||||||||
| 28 | 5 | has api_key => ( is => 'ro', isa => Str, required => 1 ); | |||||||||||||||||||||||||||
| 29 | 5 | has user_agent => ( is => 'ro', lazy => 1, builder => 1, init_arg => undef ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 5 | has _rates_validator => ( is => 'ro', required => 1, builder => 1 ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | sub _build__rates_validator { | ||||||||||||||||||||||||||
|
Subroutine
_build__rates_validator: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # TYPES FOR VALIDATOR | ||||||||||||||||||||||||||||
| 36 | 4 | my $Currency = declare as StrMatch[qr{^[A-Z]{3}$}]; | |||||||||||||||||||||||||||
| 37 | 4 | my $Quotes = declare as ArrayRef[$Currency]; | |||||||||||||||||||||||||||
| 38 | 4 | ▶ | coerce $Quotes, from Str, via { [$_] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 4 | my $Date = declare as StrMatch[qr{^\d\d\d\d-\d\d-\d\d$}]; | |||||||||||||||||||||||||||
| 40 | 4 | my $Fields = declare as ArrayRef[Str]; | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | coerce $Fields, from Str, via { [$_] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 42 | 4 | my $DecimalPlaces = StrMatch[qr{^(?:\d+|all)$}]; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 4 | my %validators = ( | |||||||||||||||||||||||||||
| 45 | base_currency => $Currency, | ||||||||||||||||||||||||||||
| 46 | quote => $Quotes, | ||||||||||||||||||||||||||||
| 47 | date => $Date, | ||||||||||||||||||||||||||||
| 48 | start => $Date, | ||||||||||||||||||||||||||||
| 49 | end => $Date, | ||||||||||||||||||||||||||||
| 50 | fields => $Fields, | ||||||||||||||||||||||||||||
| 51 | decimal_places => $DecimalPlaces, | ||||||||||||||||||||||||||||
| 52 | data_set => Str, | ||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 10 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 56 | 10 | my %params = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 10 | my %result = (); | |||||||||||||||||||||||||||
| 59 | die "missing required parameter: base_currency" | ||||||||||||||||||||||||||||
| 60 | 10 | ▶ | unless exists $params{base_currency}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 9 | foreach my $key ( keys %params ) { | |||||||||||||||||||||||||||
| 63 | 12 | my $type = $validators{$key}; | |||||||||||||||||||||||||||
| 64 | 12 | ▶ | die "invalid parameter: $key" unless $type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 11 | my $val = $params{$key}; | |||||||||||||||||||||||||||
| 66 | 11 | ▶ | $val = $type->coerce($val) if $type->has_coercion; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 11 | ▶ | if ( ! $type->check($val) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 7 | ▶ | $val = JSON::XS::encode_json($val) if ref $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 7 | die "invalid value: $key = ($val)"; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 4 | $result{$key} = $val; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 1 | return \%result; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 4 | } | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 4 | ▶ | sub _build_user_agent { | ||||||||||||||||||||||||||
|
Subroutine
_build_user_agent: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 79 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 4 | my %options = ( agent => sprintf '%s/%s', __PACKAGE__, $VERSION ); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # LWP::UA forces hostname verification by default in newer versions | ||||||||||||||||||||||||||||
| 84 | # turn off for simplification; | ||||||||||||||||||||||||||||
| 85 | 4 | ▶ | $options{ssl_opts} = { verify_hostname => 0 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | if $self->base_url->scheme eq 'https'; | ||||||||||||||||||||||||||||
| 87 | 4 | ▶ | $options{timeout} = $self->timeout if $self->has_timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 4 | my $ua = LWP::UserAgent->new(%options); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # set auth header | ||||||||||||||||||||||||||||
| 92 | 4 | $ua->default_header( | |||||||||||||||||||||||||||
| 93 | Authorization => sprintf 'Bearer %s', $self->api_key | ||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # set the proxy if needed | ||||||||||||||||||||||||||||
| 97 | # LWP:UserAgent will use PERL_LWP_ENV_PROXY if set automatically | ||||||||||||||||||||||||||||
| 98 | 4 | ▶ | $ua->proxy( $self->base_url->scheme, $self->proxy ) if $self->has_proxy; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 4 | return $ua; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # GET /currencies.json | ||||||||||||||||||||||||||||
| 104 | 2 | ▶ | sub get_currencies { | ||||||||||||||||||||||||||
|
Subroutine
get_currencies: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 106 | 2 | my %params = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2 | my $response = $self->_get_request('currencies.json', \%params); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # convert arrayref[hashref] into hashref | ||||||||||||||||||||||||||||
| 111 | 2 | ▶ | if ( $response->is_success && exists $response->data->{currencies}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$response->is_success and exists $response->data->{"currencies"}
MC/DC50%1/2
$response->is_success and exists $response->data->{"currencies"}
$response->is_success
exists $response->data->{"currencies"}
Truth table66%2/3
A and B
A= $response->is_successB= exists $response->data->{"currencies"}
| |||||||||||||||||||||||||||||
| 112 | $response->data({ | ||||||||||||||||||||||||||||
| 113 | 2 | map { $_->{code} => $_->{description} } | |||||||||||||||||||||||||||
| 114 | 1 | @{$response->data->{currencies}} | |||||||||||||||||||||||||||
| 115 | }); | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 2 | return $response; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # GET /rates/XXX.json | ||||||||||||||||||||||||||||
| 122 | 10 | ▶ | sub get_rates { | ||||||||||||||||||||||||||
|
Subroutine
get_rates: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | 10 | my $params = $self->_rates_validator->(@_); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1 | my $base_currency = delete $params->{base_currency}; | |||||||||||||||||||||||||||
| 127 | 1 | return $self->_get_request(['rates', $base_currency . '.json'], $params); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # GET /remaining_quotes.json | ||||||||||||||||||||||||||||
| 131 | 1 | ▶ | sub get_remaining_quotes { | ||||||||||||||||||||||||||
|
Subroutine
get_remaining_quotes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 133 | 1 | return $self->_get_request('remaining_quotes.json'); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 4 | ▶ | sub _get_request { | ||||||||||||||||||||||||||
|
Subroutine
_get_request: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 137 | 4 | my ( $self, $path, $params ) = @_; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 4 | my $uri = $self->base_url->clone->canonical; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # build the new path | ||||||||||||||||||||||||||||
| 142 | 4 | ▶ | $path = [$path] unless ref $path eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 4 | my @new_path = grep { $_ ne '' } ($uri->path_segments, @{$path}); | |||||||||||||||||||||||||||
| 144 | 4 | $uri->path_segments( @new_path ); | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # set query params | ||||||||||||||||||||||||||||
| 147 | 4 | ▶ | $params = {} unless defined $params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 4 | $uri->query_form(%{$params}); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 4 | my $response = $self->user_agent->get( $uri->as_string ); | |||||||||||||||||||||||||||
| 151 | 4 | return WebService::OANDA::ExchangeRates::Response->new( | |||||||||||||||||||||||||||
| 152 | http_response => $response ); | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 5 | 1; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||