Class: Imgur::API

Imgur API interface

Constructor Summary

public API initialize(api_key)

Creates a new Imgur API instance

Meta Tags

Parameters:

[String] api_key

Your API key from http://imgur.com/register/api/

Returns:

[API]

An Imgur API instance

[View source]


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

def initialize api_key
  @api_key = api_key
end

Public Visibility

Public Instance Method Summary

#delete(image_hash)

Deletes the image with the specified delete hash.

Returns: Boolean

#gallery(params = {})

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

Returns: Array<Hash>

#image_stats(image_hash)

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

Returns: Hash

#upload_file(image_filename)

Uploads an image from local disk.

Returns: Hash

#upload_from_url(image_url)

Uploads a file from a remote URL.

Returns: Hash

Public Instance Method Details

delete

public Boolean delete(image_hash)

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

Meta Tags

Returns:

[Boolean]

Whether or not the image was deleted

[View source]


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

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

gallery

image_stats

public Hash image_stats(image_hash)

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

Meta Tags

Parameters:

[String] image_hash

Imgur’s hash of the image

Returns:

[Hash]

Image statistics

Raises:

[ImgurError]
[View source]


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

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

public Hash upload_file(image_filename)

Uploads an image from local disk

Meta Tags

Parameters:

[String] image_filename

The filename of the image on disk to upload

Returns:

[Hash]

Image data

Raises:

[ImgurError]
[View source]


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

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

public Hash upload_from_url(image_url)

Uploads a file from a remote URL

Meta Tags

Parameters:

[String] image_url

The URL of the image to upload

Returns:

[Hash]

Image data

Raises:

[ImgurError]
[View source]


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

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
Generated on Thursday, October 15 2009 at 05:19:33 PM by YARD 0.2.3.5 (ruby-1.8.7).