File Coverage

blib/lib/AWS/Lambda/Context.pm
Criterion Covered Total %
statement 29 40 72.5
branch 1 2 50.0
condition 5 22 22.7
subroutine 8 16 50.0
pod 0 12 0.0
total 43 92 46.7


line stmt bran cond sub pod time code
1             package AWS::Lambda::Context;
2 20     20   574221 use 5.026000;
  20         124  
3 20     20   119 use strict;
  20         53  
  20         807  
4 20     20   160 use warnings;
  20         32  
  20         1750  
5              
6 20     20   163 use Time::HiRes qw(time);
  20         40  
  20         230  
7              
8             sub new {
9 7     7 0 500744 my $proto = shift;
10 7   33     224 my $class = ref $proto || $proto;
11 7         23 my %args;
12 7 50 33     161 if (@_ == 1 && ref $_[0] eq 'HASH') {
13 0         0 %args = %{$_[0]};
  0         0  
14             } else {
15 7         253 %args = @_;
16             }
17 7   50     63 my $deadline_ms = $args{deadline_ms} // die 'deadline_ms is required';
18 7   50     59 my $invoked_function_arn = $args{invoked_function_arn} // '';
19 7   50     77 my $aws_request_id = $args{aws_request_id} // '';
20 7         40 my $trace_id = $args{trace_id};
21 7         33 my $tenant_id = $args{tenant_id};
22 7         136 my $self = bless +{
23             deadline_ms => +$deadline_ms,
24             invoked_function_arn => $invoked_function_arn,
25             aws_request_id => $aws_request_id,
26             trace_id => $trace_id,
27             tenant_id => $tenant_id,
28             }, $class;
29              
30 7         71 return $self;
31             }
32              
33             sub get_remaining_time_in_millis {
34 0     0 0 0 my $self = shift;
35 0         0 return $self->{deadline_ms} - time() * 1000;
36             }
37              
38             sub function_name {
39 0   0 0 0 0 return $ENV{AWS_LAMBDA_FUNCTION_NAME} // die 'function_name is not found';
40             }
41              
42             sub function_version {
43 0   0 0 0 0 return $ENV{AWS_LAMBDA_FUNCTION_VERSION} // die 'function_version is not found';
44             }
45              
46             sub invoked_function_arn {
47 1     1 0 7 my $self = shift;
48 1         45 return $self->{invoked_function_arn};
49             }
50              
51             sub memory_limit_in_mb {
52 0   0 0 0 0 return +$ENV{AWS_LAMBDA_FUNCTION_MEMORY_SIZE} // die 'memory_limit_in_mb is not found';
53             }
54              
55             sub aws_request_id {
56 6     6 0 23701 my $self = shift;
57 6         106 return $self->{aws_request_id};
58             }
59              
60             sub log_group_name {
61 0   0 0 0 0 return $ENV{AWS_LAMBDA_LOG_GROUP_NAME} // die 'log_group_name is not found';
62             }
63              
64             sub log_stream_name {
65 0   0 0 0 0 return $ENV{AWS_LAMBDA_LOG_STREAM_NAME} // die 'log_stream_name is not found';
66             }
67              
68             sub identity {
69 0     0 0 0 return undef; # TODO
70             }
71              
72             sub client_context {
73 0     0 0 0 return undef; # TODO
74             }
75              
76             sub tenant_id {
77 1     1 0 4 my $self = shift;
78 1         18 return $self->{tenant_id};
79             }
80              
81             1;
82             =encoding utf-8
83              
84             =head1 NAME
85              
86             AWS::Lambda::Context - It's Perl port of the AWS Lambda Context.
87              
88             =head1 SYNOPSIS
89              
90             sub handle {
91             my ($payload, $context) = @_;
92             # $context is an instance of AWS::Lambda::Context
93             my $result = {
94             # The name of the Lambda function.
95             function_name => $context->function_name,
96              
97             # The version of the function.
98             function_version => $context->function_version,
99            
100             # The Amazon Resource Name (ARN) used to invoke the function.
101             # Indicates if the invoker specified a version number or alias.
102             invoked_function_arn => $context->invoked_function_arn,
103              
104             # The amount of memory configured on the function.
105             memory_limit_in_mb => $context->memory_limit_in_mb,
106              
107             # The identifier of the invocation request.
108             aws_request_id => $context->aws_request_id,
109              
110             # The log group for the function.
111             log_group_name => $context->log_group_name,
112              
113             # The log stream for the function instance.
114             log_stream_name => $context->log_stream_name,
115              
116             # The tenant id for the function.
117             tenant_id => $context->tenant_id,
118             };
119             return $result;
120             }
121              
122             =head1 LICENSE
123              
124             The MIT License (MIT)
125              
126             Copyright (C) ICHINOSE Shogo.
127              
128             =head1 AUTHOR
129              
130             ICHINOSE Shogo Eshogo82148@gmail.comE
131              
132             =cut