Module: Worochi::Helper

Defined in:
lib/worochi/helper.rb

Overview

Contains any global helper methods not specific to any individual service.

Class Method Summary collapse

Class Method Details

.is_s3_path?(path) ⇒ Boolean

Check if a given path is an S3 path.

Parameters:

  • path (String)

Returns:

  • (Boolean)


38
39
40
# File 'lib/worochi/helper.rb', line 38

def is_s3_path?(path)
  !s3_prefix_re.match(path).nil?
end

.s3_url(path) ⇒ URI::HTTPS

Given an S3 path, return the full URL for the corresponding object determined using the AWS SDK. AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID should be present in ENV. The string should be formatted as: ‘s3:(bucket_name:)path/to/file`.

Examples:

Pre-configured bucket name

Worochi::Config.s3_bucket = 'worochi'
Worochi::Helper.s3_url('s3:test/path')
# => #<URI::HTTPS URL:https://worochi.s3.amazonaws.com/test/path?AWSAccessKeyId=...>

Custom bucket name

Worochi::Helper.s3_url('s3:bucket-name:a/b')
# => #<URI::HTTPS URL:https://bucket-name.s3.amazonaws.com/a/b?AWSAccessKeyId=...>

Invalid syntax

Worochi::Helper.s3_url('www.a.com/b.txt')
# => Worochi::Error

Parameters:

  • path (String)

    S3 path

Returns:

  • (URI::HTTPS)

    URI of the file on S3

Raises:



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/worochi/helper.rb', line 22

def s3_url(path)
  raise Error, 'Invalid S3 path' unless is_s3_path?(path)
  path = path.sub(s3_prefix_re, '')
  if match = /^(.*)\:/.match(path)
    bucket = match[1]
    path = path.sub(/^(.*)\:/, '')
  end
  bucket ||= Config.s3_bucket
  raise Error, 'S3 bucket name is not defined' if bucket.nil?
  AWS::S3.new.buckets[bucket].objects[path].url_for(:read)
end