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.



57
58
59
60
61
62
63
64
# File 'lib/ecrire/app/models/admin/image.rb', line 57

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.



55
56
57
# File 'lib/ecrire/app/models/admin/image.rb', line 55

def access_key
  @access_key
end

#bucketObject (readonly)

Returns the value of attribute bucket.



55
56
57
# File 'lib/ecrire/app/models/admin/image.rb', line 55

def bucket
  @bucket
end

#errorsObject (readonly)

Returns the value of attribute errors.



55
56
57
# File 'lib/ecrire/app/models/admin/image.rb', line 55

def errors
  @errors
end

#pathObject (readonly)

Returns the value of attribute path.



55
56
57
# File 'lib/ecrire/app/models/admin/image.rb', line 55

def path
  @path
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



55
56
57
# File 'lib/ecrire/app/models/admin/image.rb', line 55

def secret_key
  @secret_key
end

Instance Method Details

#configuration_hashObject



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

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



80
81
82
83
84
85
86
87
# File 'lib/ecrire/app/models/admin/image.rb', line 80

def connect
  begin
    @bucket.retrieve
    @connected = true
  rescue Error::ResponseError, ArgumentError, SocketError => e
    errors.add :remote, "Couldn't connect to S3."
  end
end

#connected?Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
# File 'lib/ecrire/app/models/admin/image.rb', line 96

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

#serviceObject



89
90
91
92
93
94
# File 'lib/ecrire/app/models/admin/image.rb', line 89

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