File Coverage

blib/lib/AWS/Lambda/Context.pm
Criterion Covered Total %
statement 26 37 70.2
branch 1 2 50.0
condition 5 22 22.7
subroutine 7 15 46.6
pod 0 11 0.0
total 39 87 44.8


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