File Coverage

blib/lib/Stancer/Device.pm
Criterion Covered Total %
statement 28 28 100.0
branch 14 14 100.0
condition 15 15 100.0
subroutine 9 9 100.0
pod 1 1 100.0
total 67 67 100.0


line stmt bran cond sub pod time code
1             package Stancer::Device;
2              
3 17     17   290078 use 5.020;
  17         88  
4 17     17   136 use strict;
  17         37  
  17         482  
5 17     17   90 use warnings;
  17         36  
  17         1723  
6              
7             # ABSTRACT: Device representation
8             our $VERSION = '1.0.3'; # VERSION
9              
10 17     17   846 use Stancer::Core::Types qw(:network Maybe Str);
  17         44  
  17         4134  
11              
12 17     17   145 use Moo;
  17         41  
  17         245  
13              
14             extends 'Stancer::Core::Object';
15              
16 17     17   10705 use namespace::clean;
  17         40  
  17         171  
17              
18             has '+_integer' => (
19             default => sub{ [qw(port)] },
20             );
21              
22              
23             has city => (
24             is => 'rw',
25             isa => Maybe[Str],
26 1     1   134 builder => sub { $_[0]->_attribute_builder('country') },
27             lazy => 1,
28             predicate => 1,
29             trigger => sub { $_[0]->_add_modified('city') },
30             );
31              
32              
33             has country => (
34             is => 'rw',
35             isa => Maybe[Str],
36 3     3   147 builder => sub { $_[0]->_attribute_builder('country') },
37             lazy => 1,
38             predicate => 1,
39             trigger => sub { $_[0]->_add_modified('country') },
40             );
41              
42              
43             has http_accept => (
44             is => 'rw',
45             isa => Maybe[Str],
46             trigger => sub { $_[0]->_add_modified('http_accept') },
47             );
48              
49              
50             has ip => (
51             is => 'rw',
52             isa => Maybe[IpAddress],
53             coerce => sub {
54             return if not defined $_[0];
55             return if $_[0] eq '1';
56             return if $_[0] eq q//;
57             return $_[0];
58             },
59             trigger => sub { $_[0]->_add_modified('ip') },
60             );
61              
62              
63             has languages => (
64             is => 'rw',
65             isa => Maybe[Str],
66             trigger => sub { $_[0]->_add_modified('languages') },
67             );
68              
69              
70             has port => (
71             is => 'rw',
72             isa => Maybe[Port],
73             coerce => sub {
74             return if not defined $_[0];
75             return if $_[0] eq q//;
76             return $_[0];
77             },
78             trigger => sub { $_[0]->_add_modified('port') },
79             );
80              
81              
82             has user_agent => (
83             is => 'rw',
84             isa => Maybe[Str],
85             trigger => sub { $_[0]->_add_modified('user_agent') },
86             );
87              
88              
89             sub hydrate_from_env {
90 25     25 1 19419 my $this = shift;
91              
92 25 100 100     186 $this->http_accept($ENV{HTTP_ACCEPT}) if defined $ENV{HTTP_ACCEPT} and not defined $this->http_accept;
93 25 100 100     391 $this->ip($ENV{SERVER_ADDR}) if defined $ENV{SERVER_ADDR} and not defined $this->ip;
94 25 100 100     169 $this->languages($ENV{HTTP_ACCEPT_LANGUAGE}) if defined $ENV{HTTP_ACCEPT_LANGUAGE} and not defined $this->languages;
95 25 100 100     308 $this->port($ENV{SERVER_PORT}) if defined $ENV{SERVER_PORT} and not defined $this->port;
96 25 100 100     149 $this->user_agent($ENV{HTTP_USER_AGENT}) if defined $ENV{HTTP_USER_AGENT} and not defined $this->user_agent;
97              
98 25 100       573 Stancer::Exceptions::InvalidIpAddress->throw() if not defined $this->ip;
99 16 100       368 Stancer::Exceptions::InvalidPort->throw() if not defined $this->port;
100              
101 12         116 return $this;
102             }
103              
104             1;
105              
106             __END__
107              
108             =pod
109              
110             =encoding UTF-8
111              
112             =head1 NAME
113              
114             Stancer::Device - Device representation
115              
116             =head1 VERSION
117              
118             version 1.0.3
119              
120             =head1 ATTRIBUTES
121              
122             =head2 C<city>
123              
124             Read/Write string.
125              
126             Customer's city.
127              
128             =head2 C<country>
129              
130             Read/Write string.
131              
132             Customer's country.
133              
134             =head2 C<http_accept>
135              
136             Read/Write string.
137              
138             Customer's browser acceptance.
139              
140             =head2 C<ip>
141              
142             Read/write IP address.
143              
144             Customer's IP address.
145              
146             May be an IPv4 (aka 212.27.48.10) or an IPv6 (2a01:e0c:1::1).
147              
148             =head2 C<languages>
149              
150             Read/Write string.
151              
152             Customer's browser accepted languages.
153              
154             =head2 C<port>
155              
156             Read/Write integer.
157              
158             Customer's port.
159              
160             =head2 C<user_agent>
161              
162             Read/Write string.
163              
164             Customer's browser user agent.
165              
166             =head1 METHODS
167              
168             =head2 C<< Stancer::Device->new() : I<self> >>
169              
170             =head2 C<< Stancer::Device->new(I<%args>) : I<self> >>
171              
172             =head2 C<< Stancer::Device->new(I<\%args>) : I<self> >>
173              
174             You may not need to create yourself a device instance, it will automatically be created for you.
175              
176             # Get an empty new device
177             my $new = Stancer::Device->new();
178              
179             This object needs a valid IP address (IPv4 or IPv6) ans a valid port, it will automatically used environment
180             variables as created by Apache or nginx (aka C<SERVER_ADDR> and C<SERVER_PORT>).
181              
182             If variables are not available or if you are using a proxy, you must give IP and port at object instanciation.
183              
184             my $device = Stancer::Device->new(ip => $ip, port => $port);
185              
186             =head2 C<< Stancer::Device->hydrate_from_env() : I<self> >>
187              
188             Hydrate frpm environment.
189              
190             =head1 USAGE
191              
192             =head2 Logging
193              
194              
195              
196             We use the L<Log::Any> framework for logging events.
197             You may tell where it should log using any available L<Log::Any::Adapter> module.
198              
199             For example, to log everything to a file you just have to add a line to your script, like this:
200             #! /usr/bin/env perl
201             use Log::Any::Adapter (File => '/var/log/payment.log');
202             use Stancer::Device;
203              
204             You must import C<Log::Any::Adapter> before our libraries, to initialize the logger instance before use.
205              
206             You can choose your log level on import directly:
207             use Log::Any::Adapter (File => '/var/log/payment.log', log_level => 'info');
208              
209             Read the L<Log::Any> documentation to know what other options you have.
210              
211             =cut
212              
213             =head1 SECURITY
214              
215             =over
216              
217             =item *
218              
219             Never, never, NEVER register a card or a bank account number in your database.
220              
221             =item *
222              
223             Always uses HTTPS in card/SEPA in communication.
224              
225             =item *
226              
227             Our API will never give you a complete card/SEPA number, only the last four digits.
228             If you need to keep track, use these last four digit.
229              
230             =back
231              
232             =cut
233              
234             =head1 BUGS
235              
236             Please report any bugs or feature requests on the bugtracker website
237             L<https://gitlab.com/wearestancer/library/lib-perl/-/issues> or by email to
238             L<bug-stancer@rt.cpan.org|mailto:bug-stancer@rt.cpan.org>.
239              
240             When submitting a bug or request, please include a test-file or a
241             patch to an existing test-file that illustrates the bug or desired
242             feature.
243              
244             =head1 AUTHOR
245              
246             Joel Da Silva <jdasilva@cpan.org>
247              
248             =head1 COPYRIGHT AND LICENSE
249              
250             This software is Copyright (c) 2018-2024 by Stancer / Iliad78.
251              
252             This is free software, licensed under:
253              
254             The Artistic License 2.0 (GPL Compatible)
255              
256             =cut