Module: Paperclip::Storage::Imgur

Defined in:
lib/paperclip/storage/imgur.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

Specify credentials in a file, path, string or hash. Required fields: client_id, client_secret, refresh_token



11
12
13
14
15
16
17
18
19
20
# File 'lib/paperclip/storage/imgur.rb', line 11

def self.extended(base)
  base.instance_eval do
    imgur_credentials = parse_credentials(@options[:imgur_credentials])
    imgur_options = @options[:imgur_options] || {}
    environment = defined?(Rails) ? Rails.env : imgur_options[:environment].to_s

    # Use credentials for the current Rails environment, if any
    @imgur_credentials = (imgur_credentials[environment] || imgur_credentials).symbolize_keys
  end
end

Instance Method Details

#copy_to_local_file(style, destination_path) ⇒ Object



76
77
78
79
80
81
# File 'lib/paperclip/storage/imgur.rb', line 76

def copy_to_local_file(style, destination_path)
  # TO BE DONE
  #local_file = File.open(destination_path, 'wb')
  #local_file.write(imgur_session.get_file(path(style)))
  #local_file.close
end

#exists?(style_name = default_style) ⇒ Boolean

We have to trust that any Imgur hash stored into *_file_name represents an existing Imgur image. This assumption let us avoid the latency of a network call. If not, someone has touched where he shouldn’t!

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File 'lib/paperclip/storage/imgur.rb', line 25

def exists?(style_name = default_style)
  if original_filename
    true
  else
    false
  end
end

#flush_deletesObject



53
54
55
56
57
58
# File 'lib/paperclip/storage/imgur.rb', line 53

def flush_deletes
  @queued_for_delete.each do |path|
    imgur_session.image.image_delete(path) # Doesn't matter if the image doesn't really exists
  end
  @queued_for_delete = []
end

#flush_writesObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/paperclip/storage/imgur.rb', line 33

def flush_writes
  @queued_for_write.each do |style, file| #style is 'original' etc...

    begin
      image = imgur_session.image.image_upload(file)
      image_id = image.id
    rescue
      # Sometimes there are API or network errors.
      # In this cases, we don't store anything.
      image_id = nil
    end

    # We cannot use update_attribute because it internally calls save, and save calls
    # flush_writes again, and it will end up in a stack overflow due excessive recursion
    instance.update_column :"#{name}_#{:file_name}", image_id
  end
  after_flush_writes
  @queued_for_write = {}
end

#path(style_name = default_style) ⇒ Object

Returns the path of the attachment. It’s exactly the Imgur hash.



72
73
74
# File 'lib/paperclip/storage/imgur.rb', line 72

def path(style_name = default_style)
  original_filename
end

#url(size = default_style) ⇒ Object

Returns the image’s URL. We don’t use imgur_session.find to avoid the latency of a network call.



62
63
64
65
66
67
68
# File 'lib/paperclip/storage/imgur.rb', line 62

def url(size = default_style)
  image_id = instance.send("#{name}_#{:file_name}")

  return @url_generator.for(size, {}) if image_id.nil? || image_id.empty? # Show Paperclip's default missing image path

  ::Imgur::Image.new(id: image_id).url(size)
end