Class: Imgurapi::Api::Image

Inherits:
Base
  • Object
show all
Defined in:
lib/imgurapi/api/image.rb

Instance Attribute Summary

Attributes inherited from Base

#session

Instance Method Summary collapse

Methods inherited from Base

#communication

Instance Method Details

#image(id) ⇒ Object



6
7
8
9
10
# File 'lib/imgurapi/api/image.rb', line 6

def image(id)
  raise 'Please provide a valid image identificator' if id.nil? || !id.is_a?(String) || id == '' || !!(id =~ /[^\w]/)

  Imgurapi::Image.new communication.call(:get, "image/#{id}")
end

#image_delete(id) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/imgurapi/api/image.rb', line 30

def image_delete(id)
  if id.kind_of? Imgurapi::Image
    id = id.id
  end

  raise 'Please provide a valid image identificator' if id.nil? || !id.is_a?(String) || id == '' || !!(id =~ /[^\w]/)

  communication.call(:delete, "image/#{id}")
end

#image_upload(local_file) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/imgurapi/api/image.rb', line 13

def image_upload(local_file)
  file_type = FileType.new(local_file)

  image = if file_type.url?
            local_file
          else
            raise 'File must be an image' unless file_type.image?

            file = local_file.respond_to?(:read) ? local_file : File.open(local_file, 'rb')

            Base64.encode64(file.read)
          end

  Imgurapi::Image.new communication.call(:post, 'image', image: image)
end