| 1 | package Finance::GDAX::API::URL; | ||
| 2 | our $VERSION = '0.02'; | ||
| 3 | use 5.20.0; | ||
| 4 | use warnings; | ||
| 5 | use Moose; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Finance::GDAX::API::URL - URL assembly for GDAX REST API | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use Finanace::GDAX::API::URL; | ||
| 14 | my $url = Finance::GDAX::API::URL->new->testing; | ||
| 15 | `wget $url/test_thing`; | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | This class builds URLs for Finance::GDAX::API classes | ||
| 20 | |||
| 21 | =head1 ATTRIBUTES | ||
| 22 | |||
| 23 | =head2 C<debug> | ||
| 24 | |||
| 25 | Bool that sets debug mode (will use sandbox). Defaults to true (1). | ||
| 26 | |||
| 27 | =head2 C<production> | ||
| 28 | |||
| 29 | The base URI for production requests, including the https:// | ||
| 30 | |||
| 31 | =head2 C<testing> | ||
| 32 | |||
| 33 | The base URI for testing requests to the GDAX sandbox, including the | ||
| 34 | https:// | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | has 'production' => (is => 'rw', | ||
| 39 | isa => 'Str', | ||
| 40 | default => 'https://api.gdax.com', | ||
| 41 | ); | ||
| 42 | has 'testing' => (is => 'rw', | ||
| 43 | isa => 'Str', | ||
| 44 | default => 'https://api-public.sandbox.gdax.com', | ||
| 45 | ); | ||
| 46 | has 'debug' => (is => 'rw', | ||
| 47 | isa => 'Bool', | ||
| 48 | default => 1, | ||
| 49 | ); | ||
| 50 | has '_sections' => (is => 'rw', | ||
| 51 | isa => 'ArrayRef', | ||
| 52 | default => sub {[]}, | ||
| 53 | ); | ||
| 54 | |||
| 55 | |||
| 56 | =head1 METHODS | ||
| 57 | |||
| 58 | =head2 C<get> | ||
| 59 | |||
| 60 | Returns a string of the assembled URL | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub get { | ||
| 65 | my $self = shift; | ||
| 66 | my $url = join '/', @{$self->_sections}; | ||
| 67 | $url = '/'.$url; | ||
| 68 | if ($self->debug) { | ||
| 69 | return $self->testing.$url; | ||
| 70 | } else { | ||
| 71 | return $self->production.$url; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | =head2 C<add> | ||
| 76 | |||
| 77 | Adds to the URL, each will be separated with a '/' | ||
| 78 | |||
| 79 | $url->add('products'); | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub add { | ||
| 84 | my ($self, $thing) = @_; | ||
| 85 | $thing =~ s/^\/+//; | ||
| 86 | push @{$self->_sections}, $thing; | ||
| 87 | } | ||
| 88 | |||
| 89 | __PACKAGE__->meta->make_immutable; | ||
| 90 | 1; | ||
| 91 | |||
| 92 | |||
| 93 | =head1 AUTHOR | ||
| 94 | |||
| 95 | Mark Rushing <mark@orbislumen.net> | ||
| 96 | |||
| 97 | =head1 COPYRIGHT AND LICENSE | ||
| 98 | |||
| 99 | This software is copyright (c) 2017 by Home Grown Systems, SPC. | ||
| 100 | |||
| 101 | This is free software; you can redistribute it and/or modify it under | ||
| 102 | the same terms as the Perl 5 programming language system itself. | ||
| 103 | |||
| 104 | =cut | ||
| 105 |