Class: Imgur

Inherits:
Object
  • Object
show all
Defined in:
lib/imgur.rb

Constant Summary collapse

ImgurException =
Class.new Exception
IMAGE_SIZES =
{ :small => :s, :medium => :m, :large => :l }

Class Method Summary collapse

Class Method Details

.client_id=(client_id) ⇒ Object



11
12
13
# File 'lib/imgur.rb', line 11

def self.client_id=( client_id )
  @@client_id = client_id
end

.copy(url) ⇒ Object



28
29
30
# File 'lib/imgur.rb', line 28

def self.copy( url )
  upload :url => url
end

.delete(delete_hash) ⇒ Object



40
41
42
43
44
# File 'lib/imgur.rb', line 40

def self.delete( delete_hash )
  url = generate_url :hash => delete_hash, :method => :image 
  response = JSON http.delete( url, {}, header ).body
  response["success"] == true
end

.get(hash, options = {}) ⇒ Object



36
37
38
# File 'lib/imgur.rb', line 36

def self.get( hash, options = {} )
  http.get_content url_for hash, options
end

.upload(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/imgur.rb', line 15

def self.upload( options = {} )
  data = options[:data] ||
    options[:url] ||
    Base64.encode64(open(options[:filename]).read)
  post_data = { :image => data }
  post_url = generate_url :method => :upload
  response = JSON http.post(post_url, post_data, header).body
  raise ImgurException.new response["data"]["error"] if response["data"]["error"]
  { :hash => response["data"]["id"],
    :delete_hash => response["data"]["deletehash"],
    :filetype => response["data"]["link"].match(/\.(\w*)\Z/)[1] }
end

.upload_file(filename) ⇒ Object



32
33
34
# File 'lib/imgur.rb', line 32

def self.upload_file( filename )
  upload :filename => filename
end

.url_for(hash, options = {}) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/imgur.rb', line 46

def self.url_for( hash, options = {} )
  size = IMAGE_SIZES[options[:size].to_sym] if options[:size]
  filetype = options[:filetype] || :png
  if filetype == :detect
    filetype = json_get( :hash => hash, :method => :image )["data"]["link"].match(/\.(\w*)\Z/)[1]
  end
  "http://i.imgur.com/#{hash}#{size}.#{filetype}"
end