class HAL::S3::DirectoryIndex

Attributes

relation_name[R]
self_url[R]
uri[R]

Public Class Methods

new(uri, relation_name: 'items', self_url: nil) click to toggle source
# File lib/hal/s3.rb, line 10
def initialize(uri, relation_name: 'items', self_url: nil)
  @uri           = URI(uri)
  @relation_name = relation_name
  @self_url      = self_url

  options = {}
  options[:endpoint] = "https://s3-#{ENV['AWS_REGION']}.amazonaws.com" if ENV['AWS_REGION']
  @s3 = Aws::S3::Client.new(options)
end

Public Instance Methods

to_hash() click to toggle source
# File lib/hal/s3.rb, line 20
def to_hash
  data = {
    '_links' => {'self' => {'href' => self_url}}
  }
  data['_links'][relation_name] = hrefs
  data
end
to_json(pretty: false) click to toggle source
# File lib/hal/s3.rb, line 28
def to_json(pretty: false)
  pretty ? JSON.pretty_unparse(to_hash) : to_hash.to_json
end

Private Instance Methods

hrefs() click to toggle source
# File lib/hal/s3.rb, line 47
def hrefs
  paths.map do |path|
    {href: "https://#{uri.host}/#{path}"}
  end
end
list(delimiter: '/', prefix: nil) click to toggle source
# File lib/hal/s3.rb, line 34
def list(delimiter: '/', prefix: nil)
  @s3.list_objects(bucket: uri.host, delimiter: delimiter, prefix: prefix).common_prefixes.map(&:prefix)
end
paths() click to toggle source
# File lib/hal/s3.rb, line 38
def paths
  head, tail = uri.path.split('*').map{|n| n[1..-1]}

  collections = list(prefix: head)
  collections.map do |c|
    list(delimiter: tail, prefix: c).first
  end.compact
end