Class: IpFilter::Providers::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/ip_filter/providers/s3.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeS3

Returns a new instance of S3.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ip_filter/providers/s3.rb', line 9

def initialize
  @bucket_name = IpFilter.configuration.s3_bucket_name
  AWS.config(
    access_key_id: IpFilter.configuration.s3_access_key_id,
    secret_access_key: IpFilter.configuration.s3_secret_access_key
  )
  @remote = AWS::S3.new
  @bucket = create_bucket
  @urls = {}
  return self
end

Instance Attribute Details

#bucket_nameObject

Returns the value of attribute bucket_name.



7
8
9
# File 'lib/ip_filter/providers/s3.rb', line 7

def bucket_name
  @bucket_name
end

#remoteObject

Returns the value of attribute remote.



7
8
9
# File 'lib/ip_filter/providers/s3.rb', line 7

def remote
  @remote
end

#urlsObject

Returns the value of attribute urls.



7
8
9
# File 'lib/ip_filter/providers/s3.rb', line 7

def urls
  @urls
end

Instance Method Details

#create_bucketObject



21
22
23
24
25
26
27
# File 'lib/ip_filter/providers/s3.rb', line 21

def create_bucket
  bucket = remote.buckets[bucket_name]
  unless bucket.exists?
    bucket = remote.buckets.create(bucket_name)
  end
  return bucket
end

#download!(name = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ip_filter/providers/s3.rb', line 38

def download!(name = nil)
  if name.nil?
    name = File.basename(IpFilter.configuration.geo_ip_dat)
  end
  geoip_db = @bucket.objects[name]
  File.open(IpFilter.configuration.geo_ip_dat, 'wb') do |file|
    geoip_db.read do |chunk|
      file.write(chunk)
    end
  end
end

#upload!Object



29
30
31
32
33
34
35
36
# File 'lib/ip_filter/providers/s3.rb', line 29

def upload!
  IpFilter.database_files.each do |file|
    file_name = File.basename(file)
    obj = @bucket.objects[file_name].write(file: file)
    urls[file_name] = obj.url_for(:read, expires: Time.now.to_i + 840_000)
  end
  urls
end