class S3Helper

Attributes

bucket[RW]
bucket_name[RW]

Public Class Methods

new(config = {}) click to toggle source
# File lib/gooddata_eloqua/helpers/s3.rb, line 6
def initialize config = {}

  public_key = config[:public_key]
  private_key = config[:private_key] || config[:secret_key]
  self.bucket_name = config[:bucket] || 'eloqua_connector'

  AWS.config(:access_key_id => public_key, :secret_access_key => private_key)

  @s3 = AWS::S3::Client.new(region: 'us-east-1')

  bucket_exists = @s3.list_buckets.include? @bucket_name

  if bucket_exists == false
    bucket = @s3.create_bucket(:bucket_name => @bucket_name)
  else
    bucket = bucket_exists
  end

  raise 'ERROR! :public_key, :private_key must be passed for configuration.' unless public_key && private_key

end

Public Instance Methods

delete(file) click to toggle source
# File lib/gooddata_eloqua/helpers/s3.rb, line 133
def delete file
  @s3.delete_object(bucket_name: @bucket_name, key: file)
end
download(file) click to toggle source
# File lib/gooddata_eloqua/helpers/s3.rb, line 47
def download file
  begin
    puts "#{Time.now} => Downloading:S3_Bucket#{@bucket_name}: \"#{file}\""
    resp = @s3.get_object(bucket_name: @bucket_name, key:file)
    resp[:data]
  rescue
    nil
  end
end
exists?(file) click to toggle source
# File lib/gooddata_eloqua/helpers/s3.rb, line 122
def exists? file
  begin
    @s3.get_object(bucket_name: @bucket_name, key:file)
    true
  rescue AWS::S3::Errors::NoSuchKey
    false
  end
end
Also aliased as: include?
get_config(config = {}) click to toggle source
# File lib/gooddata_eloqua/helpers/s3.rb, line 57
def get_config config = {}

  file = config[:file] || 'eloqua_connector_config.json'

  if self.exists? file
    json = JSON.parse(self.download(file), :symbolize_names => true)

    File.open(file,'w'){ |f| JSON.dump(json, f) }

    self.download(file)

    json

  else

    json = {
        :id => SecureRandom.uuid,
        :updated => Time.now.to_s,
        :initial_load_get_multiple => false,
        :initial_load_get_changes => false
    }

    File.open(file,'w'){ |f| JSON.dump(json, f) }

    self.upload(file)

    json

  end

end
include?(file)
Alias for: exists?
latest_config() click to toggle source
# File lib/gooddata_eloqua/helpers/s3.rb, line 115
def latest_config
  if File.exists?('eloqua_connector_config.json')
    File.delete('eloqua_connector_config.json')
  end
  self.download('eloqua_connector_config.json')
end
set_config(config = {}) click to toggle source
# File lib/gooddata_eloqua/helpers/s3.rb, line 89
def set_config config = {}

  if config[:file]
    file = config.delete(:file)
  else
    file = 'eloqua_connector_config.json'
  end

  if self.exists? file
    json = JSON.parse(self.download(file), :symbolize_names => true)
  else
    json = Hash.new
  end

  new_json = json.merge(config)

  new_json[:updated] = Time.now.to_s

  File.open(file,'w'){ |f| JSON.dump(new_json, f) }

  self.upload(file)

  new_json

end
test() click to toggle source
# File lib/gooddata_eloqua/helpers/s3.rb, line 28
def test
  begin
    self.exists? 'tmp_dumb_file'
    puts "#{Time.now} => SETUP: Connect to AWS S3 Bucket:#{self.bucket_name}...success!"
    true
  rescue
    false
  end
end
upload(file) click to toggle source
# File lib/gooddata_eloqua/helpers/s3.rb, line 38
def upload file
  puts "#{Time.now} => Uploading:S3_Bucket#{@bucket_name}: \"#{file}\""
  resp = @s3.put_object(
      data: IO.read(file),
      bucket_name: @bucket_name,
      key: file
  )
end