Class: Imgur::API
- Inherits:
-
Object
- Object
- Imgur::API
- Defined in:
- lib/imgur.rb
Overview
Imgur API interface
Instance Method Summary collapse
-
#delete(image_hash) ⇒ Boolean
Deletes the image with the specified delete hash.
-
#gallery(params = {}) ⇒ Array<Hash>
Returns a set of images in gallery format based on your specifications.
-
#image_stats(image_hash) ⇒ Hash
Returns statistics for a specific image, like size, type, and bandwidth usage.
-
#initialize(api_key) ⇒ API
constructor
Creates a new Imgur API instance.
-
#upload_file(image_filename) ⇒ Hash
Uploads an image from local disk.
-
#upload_from_url(image_url) ⇒ Hash
Uploads a file from a remote URL.
Constructor Details
#initialize(api_key) ⇒ API
Creates a new Imgur API instance
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.
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 |
#gallery(params = {}) ⇒ Array<Hash>
Returns a set of images in gallery format based on your specifications
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
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
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
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 |