Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Switched from HTTParty to Curb, HTTParty was messing up on file uploads

  • Loading branch information...
commit 71eff27dcb100a4596464eaa2744dda62965a2db 1 parent 338027b
@jdp jdp authored
View
8 Rakefile
@@ -9,9 +9,11 @@ begin
gem.description = %Q{An interface to the Imgur API}
gem.email = "jdp34@njit.edu"
gem.homepage = "http://github.com/jdp/imgur"
- gem.authors = ["jdp"]
- gem.add_development_dependency "bacon"
- gem.add_development_dependency "yard"
+ gem.authors = ["Justin Poliey"]
+ gem.add_development_dependency "bacon", ">= 1.1.0"
+ gem.add_development_dependency "yard", ">= 0.2.3.5"
+ gem.add_development_dependency "curb", ">= 0.5.4.0"
+ gem.add_development_dependency "crack", ">= 0.1.4"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
View
87 lib/imgur.rb
@@ -1,5 +1,6 @@
-require 'httparty'
-require 'pp'
+require 'curb'
+require 'crack/json'
+require 'cgi'
# @author Justin Poliey
# Imgur module
@@ -14,33 +15,9 @@ def initialize message
end
end
-
- # Image data container
- class Image
-
- attr_accessor :image_hash, :delete_hash
- attr_accessor :original_image
- attr_accessor :large_thumbnail, :small_thumbnail
- attr_accessor :imgur_page, :delete_page
-
- def initialize image_info
- image_hash = image_info["image_hash"]
- delete_hash = image_info["delete_hash"]
- original_image = image_info["original_image"]
- large_thumbnail = image_info["large_thumbnail"]
- small_thumbnail = image_info["small_thumbnail"]
- imgur_page = image_info["imgur_page"]
- delete_page = image_info["delete_page"]
- end
-
- end
# Imgur API interface
class API
- include HTTParty
-
- base_uri 'imgur.com/api'
- format :json
# Creates a new Imgur API instance
#
@@ -50,17 +27,31 @@ def initialize api_key
@api_key = api_key
end
- # Uploads an image from local disk, a Base64 encoded string, or a URL
+ # Uploads an image from local disk
#
- # @param [String] image The image data. If uploading from local disk, prefix the filename with an @
- # @return [Image] Image data
- def upload image
- options = { :query => { :key => @api_key, :image => image } }
- response = self.class.post('/upload.json', options)
- #raise ImgurError, response["rsp"]["error_msg"] if response["rsp"]["stat"] == "fail"
- puts response
- #Image.new(response["rsp"]["image"])
- response
+ # @param [String] image_filename The filename of the image on disk to upload
+ # @raise [ImgurError]
+ # @return [Hash] Image data
+ 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
+
+ # Uploads a file from a remote URL
+ #
+ # @param [String] image_url The URL of the image to upload
+ # @raise [ImgurError]
+ # @return [Hash] Image data
+ 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
# Returns a set of images in gallery format based on your specifications
@@ -70,22 +61,26 @@ def upload image
# @option params [String] :view Date limit, values are `:week`, `:month`, or `:all`
# @option params [Integer] :count (20) Number of images to return between 0 and 50
# @option params [Integer] :page (1) Which page of images to display
- # @return [Array<Image>] Array of image data objects
+ # @raise [ImgurError]
+ # @return [Array<Hash>] Array of image data hashes
def gallery params = {}
- options = { :query => { :key => @api_key } }
- options[:query].merge!(params)
- response = self.class.get('/gallery.json', options)
+ 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| Image.new(info) }
+ response["images"].to_a.collect { |hash, info| info }
end
# Returns statistics for a specific image, like size, type, and bandwidth usage
#
# @param [String] image_hash Imgur's hash of the image
+ # @raise [ImgurError]
# @return [Hash] Image statistics
def image_stats image_hash
- options = { :query => { :key => @api_key } }
- response = self.class.get("/stats/#{image_hash}.json", options)
+ 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
@@ -96,9 +91,9 @@ def image_stats image_hash
# @param [String] delete_hash Imgur's delete hash of the image
# @return [Boolean] Whether or not the image was deleted
def delete image_hash
- options = { :query => { :key => @api_key } }
- response = self.class.get("/delete/#{image_hash}.json", options)
- raise ImgurError, response["rsp"]["error_msg"] if response["rsp"]["stat"] == "fail"
+ 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
View
30 spec/cycle_spec.rb
@@ -1,13 +1,37 @@
require 'spec_helper'
-describe "An remote image" do
+describe "A local image" do
+
+ it "should first be uploaded" do
+ should.not.raise(Imgur::ImgurError) do
+ @image = $imgur.upload_file "spec/spec_image.jpg"
+ end
+ end
+
+ it "should be a hash" do
+ @image.should.satisfy { |o| o.is_a?(Hash) }
+ end
+
+ it "should then be deleted" do
+ $imgur.delete(@image["delete_hash"]).should.equal true
+ end
+
+end
+
+describe "A remote image" do
it "should first be uploaded" do
- @image = $imgur.upload "http://media.fukung.net/images/20568/4e808393622518e616761bc4a4362f69.jpg"
+ should.not.raise(Imgur::ImgurError) do
+ @image = $imgur.upload_from_url "http://media.fukung.net/images/20568/4e808393622518e616761bc4a4362f69.jpg"
+ end
+ end
+
+ it "should be a hash" do
+ @image.should.satisfy { |o| o.is_a?(Hash) }
end
it "should then be deleted" do
- $imgur.delete @image.delete_hash
+ $imgur.delete(@image["delete_hash"]).should.equal true
end
end
View
10 spec/gallery_spec.rb
@@ -5,7 +5,11 @@
before do
@gallery = $imgur.gallery
end
-
+
+ it "should be an array" do
+ @gallery.should.satisfy { |o| o.is_a?(Array) }
+ end
+
it "should have 20 images" do
@gallery.size.should.equal 20
end
@@ -18,6 +22,10 @@
@gallery = $imgur.gallery :sort => :latest, :count => 30
end
+ it "should be an array" do
+ @gallery.should.satisfy { |o| o.is_a?(Array) }
+ end
+
it "should have 30 images" do
@gallery.size.should.equal 30
end
View
4 spec/image_stats_spec.rb
@@ -5,6 +5,10 @@
before do
@image_stats = $imgur.image_stats "h9Guq"
end
+
+ it "should be hash of statistics" do
+ @image_stats.should.satisfy { |h| h.is_a?(Hash) }
+ end
it "should be 387,469 bytes in size" do
@image_stats["size"].to_i.should.equal 387469
Please sign in to comment.
Something went wrong with that request. Please try again.