Skip to content

Commit d772ba8

Browse files
committed
Get it working in lambda
1 parent f960844 commit d772ba8

File tree

8 files changed

+157
-7
lines changed

8 files changed

+157
-7
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
.aws-sam
12
/fixtures/
23
/tmp/
34

4-
node_modules
5+
node_modules

Diff for: Dockerfile

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ WORKDIR /var/task
66

77
RUN gem install json -v '2.3.1'
88

9-
COPY . .
9+
COPY Gemfile Gemfile.lock ./
1010

1111
RUN bundle config set deployment 'true' && \
1212
bundle config set without 'development test' && \
1313
bundle install
1414

15-
CMD [ "source.SnippetExtractor.process_request." ]
15+
COPY . .
16+
17+
CMD ["bin/run.run"]

Diff for: Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ end
88
gem 'json'
99
gem 'mandate'
1010
gem 'rake'
11+
gem 'zeitwerk'
1112

1213
group :test do
1314
gem 'minitest', '~> 5.10', '!= 5.10.2'
1415
gem 'minitest-stub-const'
1516
gem 'mocha'
1617
gem 'simplecov', '~> 0.17.0'
18+
gem 'httparty'
1719
end

Diff for: Gemfile.lock

+10
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,39 @@ GEM
22
remote: https://rubygems.org/
33
specs:
44
docile (1.3.2)
5+
httparty (0.18.1)
6+
mime-types (~> 3.0)
7+
multi_xml (>= 0.5.2)
58
json (2.3.1)
69
mandate (0.3.0)
10+
mime-types (3.3.1)
11+
mime-types-data (~> 3.2015)
12+
mime-types-data (3.2021.0225)
713
minitest (5.14.2)
814
minitest-stub-const (0.6)
915
mocha (1.11.2)
16+
multi_xml (0.6.0)
1017
rake (13.0.1)
1118
simplecov (0.17.1)
1219
docile (~> 1.1)
1320
json (>= 1.8, < 3)
1421
simplecov-html (~> 0.10.0)
1522
simplecov-html (0.10.2)
23+
zeitwerk (2.4.2)
1624

1725
PLATFORMS
1826
ruby
1927

2028
DEPENDENCIES
29+
httparty
2130
json
2231
mandate
2332
minitest (~> 5.10, != 5.10.2)
2433
minitest-stub-const
2534
mocha
2635
rake
2736
simplecov (~> 0.17.0)
37+
zeitwerk
2838

2939
BUNDLED WITH
3040
2.1.4

Diff for: bin/run.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require "zeitwerk"
12
load File.expand_path('../../lib/snippet_extractor.rb', __FILE__)
23

3-
SnippetExtractor.extract(ARGV[0], ARGV[1], ARGV[2])
4+
def run(event:, context:)
5+
SnippetExtractor.process_request(event: event, context: context)
6+
end

Diff for: lib/snippet_extractor/process_request.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@ def call
1414
headers: {
1515
'Content-Length': snippet.bytesize,
1616
'Content-Type': 'application/plain; charset=utf-8',
17-
'Content-disposition': 'attachment;snippet.txt'
1817
},
1918
isBase64Encoded: false,
2019
body: snippet
2120
}
2221
end
2322

2423
def source_code
25-
event['source_code']
24+
event['queryStringParameters']['source_code']
2625
end
2726

2827
def language
29-
event['language']
28+
event['queryStringParameters']['language']
3029
end
3130
end
3231
end

Diff for: template.yaml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: >
4+
sam-app
5+
6+
Sample SAM Template for sam-app
7+
8+
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
9+
Globals:
10+
Function:
11+
Timeout: 3
12+
13+
Resources:
14+
SnippetExtractorFunction:
15+
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
16+
Properties:
17+
PackageType: Image
18+
Events:
19+
SnippetExtractor:
20+
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
21+
Properties:
22+
Path: /extract
23+
Method: get
24+
Metadata:
25+
DockerTag: ruby2.7-v1
26+
DockerContext: ./
27+
Dockerfile: Dockerfile
28+
29+
Outputs:
30+
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
31+
# Find out more about other implicit resources you can reference within SAM
32+
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
33+
SnippetExtractorApi:
34+
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
35+
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/extract_snippet/"
36+
SnippetExtractorFunction:
37+
Description: "Hello World Lambda Function ARN"
38+
Value: !GetAtt HelloWorldFunction.Arn
39+
SnippetExtractorIamRole:
40+
Description: "Implicit IAM Role created for Hello World function"
41+
Value: !GetAtt HelloWorldFunctionRole.Arn

Diff for: test/e2e_test.rb

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
require "test_helper"
2+
3+
require_relative '../../hello_world/app'
4+
5+
class HelloWorldTest < Minitest::Test
6+
def event
7+
{
8+
body: 'eyJ0ZXN0IjoiYm9keSJ9',
9+
resource: '/{proxy+}',
10+
path: '/path/to/resource',
11+
httpMethod: 'POST',
12+
isBase64Encoded: true,
13+
queryStringParameters: {
14+
foo: 'bar'
15+
},
16+
pathParameters: {
17+
proxy: '/path/to/resource'
18+
},
19+
stageVariables: {
20+
baz: 'qux'
21+
},
22+
headers: {
23+
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
24+
'Accept-Encoding' => 'gzip, deflate, sdch',
25+
'Accept-Language' => 'en-US,en;q=0.8',
26+
'Cache-Control' => 'max-age=0',
27+
'CloudFront-Forwarded-Proto' => 'https',
28+
'CloudFront-Is-Desktop-Viewer' => 'true',
29+
'CloudFront-Is-Mobile-Viewer' => 'false',
30+
'CloudFront-Is-SmartTV-Viewer' => 'false',
31+
'CloudFront-Is-Tablet-Viewer' => 'false',
32+
'CloudFront-Viewer-Country' => 'US',
33+
'Host' => '1234567890.execute-api.us-east-1.amazonaws.com',
34+
'Upgrade-Insecure-Requests' => '1',
35+
'User-Agent' => 'Custom User Agent String',
36+
'Via' => '1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)',
37+
'X-Amz-Cf-Id' => 'cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==',
38+
'X-Forwarded-For' => '127.0.0.1, 127.0.0.2',
39+
'X-Forwarded-Port' => '443',
40+
'X-Forwarded-Proto' => 'https'
41+
},
42+
requestContext: {
43+
accountId: '123456789012',
44+
resourceId: '123456',
45+
stage: 'prod',
46+
requestId: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
47+
requestTime: '09/Apr/2015:12:34:56 +0000',
48+
requestTimeEpoch: 1428582896000,
49+
identity: {
50+
cognitoIdentityPoolId: 'null',
51+
accountId: 'null',
52+
cognitoIdentityId: 'null',
53+
caller: 'null',
54+
accessKey: 'null',
55+
sourceIp: '127.0.0.1',
56+
cognitoAuthenticationType: 'null',
57+
cognitoAuthenticationProvider: 'null',
58+
userArn: 'null',
59+
userAgent: 'Custom User Agent String',
60+
user: 'null'
61+
},
62+
path: '/prod/path/to/resource',
63+
resourcePath: '/{proxy+}',
64+
httpMethod: 'POST',
65+
apiId: '1234567890',
66+
protocol: 'HTTP/1.1'
67+
}
68+
}
69+
end
70+
71+
def mock_response
72+
Object.new.tap do |mock|
73+
mock.expects(:code).returns(200)
74+
mock.expects(:body).returns('1.1.1.1')
75+
end
76+
end
77+
78+
def expected_result
79+
{
80+
statusCode: 200,
81+
body: {
82+
message: 'Hello World!',
83+
location: '1.1.1.1'
84+
}.to_json
85+
}
86+
end
87+
88+
def test_lambda_handler
89+
HTTParty.expects(:get).with('http://checkip.amazonaws.com/').returns(mock_response)
90+
assert_equal expected, lambda_handler(event: event, context: '')
91+
end
92+
end

0 commit comments

Comments
 (0)