Sha256: 34d9469decf1ad148a00c26403f613564d05c18b67ae685ed09e29ba23b59280

Contents?: true

Size: 1.99 KB

Versions: 8

Compression:

Stored size: 1.99 KB

Contents

module Heirloom

  class Reader

    def initialize(args)
      @config = args[:config]
      @name = args[:name]
      @domain = "heirloom_#{@name}"
      @id = args[:id]
      @logger = @config.logger
    end

    def exists?
      if domain_exists? && show.any?
        @logger.debug "Found entry for #{@id} in SimpleDB."
        true
      else
        @logger.debug "Entry for #{@id} not found in SimpleDB."
        false
      end
    end

    def regions
      data = show.keys.map do |key|
        key.gsub('-s3-url', '') if key =~ /-s3-url$/
      end
      data.compact
    end

    def get_bucket(args)
      @logger.debug "Looking for bucket in #{args[:region]} for #{@id}"
      url = get_url(args)
      if url
        bucket = url.gsub('s3://', '').split('/').first
        @logger.debug "Found bucket #{bucket}."
        bucket
      else
        @logger.debug "Bucket not found."
        nil
      end
    end

    def get_key(args)
      url = get_url(args)
      if url
        bucket_path = get_bucket :region => args[:region]
        bucket = url.gsub('s3://', '').gsub(bucket_path, '')
        bucket.slice!(0)
        bucket
      else
        nil
      end
    end

    def count
      sdb.count @domain
    end

    def show
      query = sdb.select "select * from #{@domain} where itemName() = '#{@id}'"
      items = query[@id] ? query[@id] : {}
      Hash.new.tap do |hash|
        items.each_pair.map do |key,value|
          hash[key] = value.first
        end
      end
    end

    private

    def domain_exists?
      sdb.domain_exists? @domain
    end

    def get_url(args)
      return nil unless exists?
      @logger.debug "Looking for #{args[:region]} endpoint for #{@id}"
      url = "#{args[:region]}-s3-url"
      if show[url]
        @logger.debug "Found #{url} for #{@id}."
        show[url]
      else
        @logger.debug "#{args[:region]} endpoint for #{@id} not found."
        nil
      end
    end

    def sdb
      @sdb ||= AWS::SimpleDB.new :config => @config
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
heirloom-0.6.1 lib/heirloom/archive/reader.rb
heirloom-0.6.0rc1 lib/heirloom/archive/reader.rb
heirloom-0.5.0rc4 lib/heirloom/archive/reader.rb
heirloom-0.5.0rc3 lib/heirloom/archive/reader.rb
heirloom-0.5.0rc2 lib/heirloom/archive/reader.rb
heirloom-0.5.0rc1 lib/heirloom/archive/reader.rb
heirloom-0.4.1rc1 lib/heirloom/archive/reader.rb
heirloom-0.4.0 lib/heirloom/archive/reader.rb