Class: Clarion::Stores::S3

Inherits:
Base
  • Object
show all
Defined in:
lib/clarion/stores/s3.rb

Instance Method Summary collapse

Constructor Details

#initialize(region:, bucket:, prefix: nil, retry_interval: 0.1, retry_max: 10) ⇒ S3

Returns a new instance of S3.



6
7
8
9
10
11
12
13
# File 'lib/clarion/stores/s3.rb', line 6

def initialize(region:, bucket:, prefix: nil, retry_interval: 0.1, retry_max: 10)
  @region = region
  @bucket = bucket
  @prefix = prefix

  @retry_interval = retry_interval
  @retry_max = retry_max
end

Instance Method Details

#authn_s3_key(authn_id) ⇒ Object



45
46
47
# File 'lib/clarion/stores/s3.rb', line 45

def authn_s3_key(authn_id)
  "#{@prefix}authn/#{authn_id}"
end

#find_authn(id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/clarion/stores/s3.rb', line 25

def find_authn(id)
  retry_count = 0
  json = begin
    s3.get_object(
      bucket: @bucket,
      key: authn_s3_key(id),
    ).body.read
  rescue Aws::S3::Errors::NoSuchKey, Aws::S3::Errors::AccessDenied
    if retry_count < retry_max
      sleep @retry_interval
      retry_count += 1
      retry
    else
      return nil
    end
  end

  Authn.new(**JSON.parse(json, symbolize_names: true))
end

#s3Object



49
50
51
# File 'lib/clarion/stores/s3.rb', line 49

def s3
  @s3 ||= Aws::S3::Client.new(region: @region)
end

#store_authn(authn) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/clarion/stores/s3.rb', line 15

def store_authn(authn)
  s3.put_object(
    bucket: @bucket,
    key: authn_s3_key(authn.id),
    body: "#{authn.to_json(:all)}\n",
    content_type: 'application/json',
  )
  self
end