Class: Imgur::API

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

Overview

Imgur API interface

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ API

Creates a new Imgur API instance

Parameters:



26
27
28
# File 'lib/imgur.rb', line 26

def initialize api_key
	@api_key = api_key
end

Instance Method Details

#delete(image_hash) ⇒ Boolean

Deletes the image with the specified delete hash. Delete hashes are not the same as image hashes, they are two separate hashes.

Parameters:

  • delete_hash (String)

    Imgur’s delete hash of the image

Returns:

  • (Boolean)

    Whether or not the image was deleted



93
94
95
96
97
98
# File 'lib/imgur.rb', line 93

def delete image_hash
	c = Curl::Easy.new("http://imgur.com/api/delete/#{image_hash}.json?key=#{@api_key}")
	c.http_get
	response = Crack::JSON.parse c.body_str
	response["rsp"]["stat"] == "ok"
end

Returns a set of images in gallery format based on your specifications

Parameters:

  • params (Hash) (defaults to: {})

    Query string parameters to pass

Options Hash (params):

  • :sort (String)

    Sort order, values are ‘:latest` or `:popular`

  • :view (String)

    Date limit, values are ‘:week`, `:month`, or `:all`

  • :count (Integer) — default: 20

    Number of images to return between 0 and 50

  • :page (Integer) — default: 1

    Which page of images to display

Returns:

  • (Array<Hash>)

    Array of image data hashes

Raises:



66
67
68
69
70
71
72
73
# File 'lib/imgur.rb', line 66

def gallery params = {}
	post_fields = params.collect { |k, v| CGI::escape(k.to_s) + '=' + CGI::escape(v.to_s) }.join('&')
	c = Curl::Easy.new("http://imgur.com/api/gallery.json?#{post_fields}")
	c.http_get
	response = Crack::JSON.parse c.body_str
	raise ImgurError, response["error"]["error_msg"] if response.key?("error")
	response["images"].to_a.collect { |hash, info| info }
end

#image_stats(image_hash) ⇒ Hash

Returns statistics for a specific image, like size, type, and bandwidth usage

Parameters:

  • image_hash (String)

    Imgur’s hash of the image

Returns:

  • (Hash)

    Image statistics

Raises:



80
81
82
83
84
85
86
# File 'lib/imgur.rb', line 80

def image_stats image_hash
	c = Curl::Easy.new("http://imgur.com/api/stats/#{image_hash}.json")
	c.http_get
	response = Crack::JSON.parse c.body_str
	raise ImgurError, response["error"]["error_msg"] if response.key?("error")
	response["stats"]
end

#upload_file(image_filename) ⇒ Hash

Uploads an image from local disk

Parameters:

  • image_filename (String)

    The filename of the image on disk to upload

Returns:

  • (Hash)

    Image data

Raises:



35
36
37
38
39
40
41
42
# File 'lib/imgur.rb', line 35

def upload_file image_filename
	c = Curl::Easy.new("http://imgur.com/api/upload.json")
	c.multipart_form_post = true
	c.http_post(Curl::PostField.content('key', @api_key), Curl::PostField.file('image', image_filename))
	response = Crack::JSON.parse c.body_str
	raise ImgurError, response["rsp"]["error_msg"] if response["rsp"]["stat"] == "fail"
	response["rsp"]["image"]
end

#upload_from_url(image_url) ⇒ Hash

Uploads a file from a remote URL

Parameters:

  • image_url (String)

    The URL of the image to upload

Returns:

  • (Hash)

    Image data

Raises:



49
50
51
52
53
54
55
# File 'lib/imgur.rb', line 49

def upload_from_url image_url
	c = Curl::Easy.new("http://imgur.com/api/upload.json")
	c.http_post(Curl::PostField.content('key', @api_key), Curl::PostField.content('image', image_url))
	response = Crack::JSON.parse c.body_str
	raise ImgurError, response["rsp"]["error_msg"] if response["rsp"]["stat"] == "fail"
	response["rsp"]["image"]
end