Class: Admin::Image::S3

Inherits:
Object
  • Object
show all
Includes:
S3
Defined in:
lib/ecrire/app/models/admin/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ S3

Returns a new instance of S3.



60
61
62
63
64
65
66
67
# File 'lib/ecrire/app/models/admin/image.rb', line 60

def initialize(options={})
  @errors = ActiveModel::Errors.new(self)
  @access_key = options.fetch('access_key', "")
  @secret_key = options.fetch('secret_key', "")
  @path = options.fetch('path', "")

  @bucket = service.bucket(options.fetch('bucket', 'ecrire'))
end

Instance Attribute Details

#access_keyObject (readonly)

Returns the value of attribute access_key.



58
59
60
# File 'lib/ecrire/app/models/admin/image.rb', line 58

def access_key
  @access_key
end

#bucketObject (readonly)

Returns the value of attribute bucket.



58
59
60
# File 'lib/ecrire/app/models/admin/image.rb', line 58

def bucket
  @bucket
end

#errorsObject (readonly)

Returns the value of attribute errors.



58
59
60
# File 'lib/ecrire/app/models/admin/image.rb', line 58

def errors
  @errors
end

#pathObject (readonly)

Returns the value of attribute path.



58
59
60
# File 'lib/ecrire/app/models/admin/image.rb', line 58

def path
  @path
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



58
59
60
# File 'lib/ecrire/app/models/admin/image.rb', line 58

def secret_key
  @secret_key
end

Instance Method Details

#configuration_hashObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ecrire/app/models/admin/image.rb', line 69

def configuration_hash
  config = {
    'access_key' => access_key,
    'secret_key' => secret_key,
    'bucket' => bucket.name
  }

  unless path.blank?
    config['path'] = path
  end

  config
end

#connectObject



83
84
85
86
87
88
89
90
# File 'lib/ecrire/app/models/admin/image.rb', line 83

def connect
  begin
    @bucket.retrieve
    @connected = true
  rescue Error::ResponseError, ArgumentError, SocketError => e
    errors.add :remote, e
  end
end

#connected?Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
# File 'lib/ecrire/app/models/admin/image.rb', line 99

def connected?
  if @connected.nil?
    connect
  end
  !!@connected
end

#serviceObject



92
93
94
95
96
97
# File 'lib/ecrire/app/models/admin/image.rb', line 92

def service
  @service ||= Service.new(access_key_id: access_key,
                         secret_access_key: secret_key,
                         use_ssl: true
                        )
end