Class: Polipus::Storage::S3Store

Inherits:
Base
  • Object
show all
Defined in:
lib/polipus/storage/s3_store.rb

Instance Attribute Summary

Attributes inherited from Base

#include_query_string_in_uuid

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ S3Store

Returns a new instance of S3Store.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/polipus/storage/s3_store.rb', line 9

def initialize(options = {})
  @options = options
  @except = @options[:except] ||= []
  @semaphore = Mutex.new

  AWS::S3::Base.establish_connection!(
    access_key_id: @options[:access_key_id],
    secret_access_key: @options[:secret_access_key]
  )
  @options[:bucket] = "com.polipus.pages.#{@options[:bucket]}"
  begin
    @bucket = AWS::S3::Bucket.find(@options[:bucket])
  rescue AWS::S3::NoSuchBucket
    create_bucket
  end
end

Instance Method Details

#add(page) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/polipus/storage/s3_store.rb', line 26

def add(page)
  @semaphore.synchronize do
    obj = page.to_hash
    @except.each { |e| obj.delete e.to_s }
    puuid = uuid(page)
    obj['uuid'] = puuid
    data = Zlib::Deflate.deflate(obj.to_json)
    AWS::S3::S3Object.store(puuid, data, @bucket.name)
    puuid
  end
end

#clearObject



63
64
65
66
# File 'lib/polipus/storage/s3_store.rb', line 63

def clear
  AWS::S3::Bucket.delete(@bucket.name, force: true)
  create_bucket
end

#countObject



59
60
61
# File 'lib/polipus/storage/s3_store.rb', line 59

def count
  @bucket.size
end

#eachObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/polipus/storage/s3_store.rb', line 68

def each
  objects = []
  last_key = nil
  loop do
    objects = AWS::S3::Bucket.objects(@bucket.name, marker: last_key)
    break if objects.size == 0
    objects.each do |o|
      page = load_page(o.value)
      yield o.key, page
    end
    last_key   = objects.last.key
  end
end

#exists?(page) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/polipus/storage/s3_store.rb', line 38

def exists?(page)
  AWS::S3::S3Object.exists? uuid(page), @bucket.name
end

#get(page) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/polipus/storage/s3_store.rb', line 42

def get(page)
  @semaphore.synchronize do
    if exists?(page)
      data = AWS::S3::S3Object.find(uuid(page), @bucket.name).value
      return load_page(data)
    end
    nil
  end
end

#remove(page) ⇒ Object



52
53
54
55
56
57
# File 'lib/polipus/storage/s3_store.rb', line 52

def remove(page)
  @semaphore.synchronize do
    exists?(page) && AWS::S3::S3Object.delete(uuid(page), @bucket.name)
    true
  end
end