Class: CloudFormationTool::CloudFormation::LambdaCode

Inherits:
Object
  • Object
show all
Includes:
Storable
Defined in:
lib/cloud_formation_tool/cloud_formation/lambda_code.rb

Constant Summary

Constants included from CloudFormationTool

VERSION

Instance Method Summary collapse

Methods included from Storable

#make_filename, #upload

Methods included from CloudFormationTool

#aws_config, #awsas, #awscf, #awscreds, #awsec2, #awss3, #cf_bucket_name, #find_profile, #profile, #region, #s3_bucket_name

Constructor Details

#initialize(url) ⇒ LambdaCode

Returns a new instance of LambdaCode.



9
10
11
12
13
14
15
# File 'lib/cloud_formation_tool/cloud_formation/lambda_code.rb', line 9

def initialize(url)
  log "Downloading Lambda code from #{url}"
  res = fetch(url)
  
  @s3_url = URI(upload(make_filename(url.split('.').last), res.body, res['content-type']))
  log "uploaded Lambda function to #{@s3_url}"
end

Instance Method Details

#fetch(uri_str, limit = 10) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cloud_formation_tool/cloud_formation/lambda_code.rb', line 17

def fetch(uri_str, limit = 10)
  raise ArgumentError, 'too many HTTP redirects' if limit == 0
  response = Net::HTTP.get_response(URI(uri_str))
  case response
  when Net::HTTPSuccess then
    response
  when Net::HTTPRedirection then
    location = response['location']
    log "redirected to #{location}"
    fetch(location, limit - 1)
  else
    raise CloudFormationTool::Errors::AppError, "Error downloading #{url}: #{response.value}"
  end
end

#to_cloudformationObject



32
33
34
35
36
37
# File 'lib/cloud_formation_tool/cloud_formation/lambda_code.rb', line 32

def to_cloudformation
  {
    'S3Bucket' => @s3_url.hostname.split('.').first,
    'S3Key' => @s3_url.path[1..-1]
  }
end