Class: Imgurruby::Imgur

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

Instance Method Summary collapse

Constructor Details

#initialize(key, addr = nil, port = nil) ⇒ Imgur

Returns a new instance of Imgur.



10
11
12
13
14
15
# File 'lib/imgurruby.rb', line 10

def initialize(key, addr = nil, port = nil)
	@api_key = key
	@host = 'api.imgur.com'
	@proxy_addr = addr
	@proxy_port = port
end

Instance Method Details

#upload(file) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/imgurruby.rb', line 17

def upload(file)
	# Image Data Variable
	imagedata = nil

	# Temp File
	tmpfile = file

	# File Name
	file_name = tmpfile.original_filename

	# Read File Binary
	File.open(tmpfile.tempfile.path, "rb") do |file|
		# Base64 Encode Image Data
		imagedata = Base64.encode64 file.read
	end

	# Begin file upload
	Net::HTTP::Proxy(@proxy_addr, @proxy_port).start(@host,80) {|http|
		res = Net::HTTP.post_form(URI.parse('http://api.imgur.com/2/upload.json'), {'image' => imagedata, 'key' => @api_key})
		json_data = res.body
		@result = JSON.parse(json_data)
		if @result["error"].nil?
			@msg = @result["upload"]["links"]["original"]
		else
			@msg = @result["error"]["message"]
		end
	}
end

#urlObject

Return msg or url



47
48
49
# File 'lib/imgurruby.rb', line 47

def url
	@msg
end