CodeSnippets에서 Imageshack API를 사용하는 데 도움이되는 this Ruby code이 발견되었습니다. Ruby on Rails에 익숙하지 않아 모델, 뷰 및 컨트롤러를 일반적으로 사용하는 방법을 알고 있지만이 코드를 어떻게 사용하는지 잘 모르겠습니다. 모듈일까요? 읽어 주셔서 감사합니다.이 Ruby 코드는 어떻게 사용합니까?
사용법 :
pic_online = ShackMirror.new (local_path_of_pic)
pic_online.url # =이> Imageshack에 직접 링크를 반환합니다.
require 'rubygems'
require 'hpricot'
require 'net/http'
require 'uri'
require 'cgi'
require 'mime/types'
class ShackMirror
SHACK_ID = "REPLACE WITH YOUR OWN IMAGESHACK ID"
USER_AGENT = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/419 (KHTML, like Gecko) Safari/419.3"
BOUNDARY = '----------PuSHerInDaBUSH_$'
attr_reader :url
def initialize(img)
raise NonImageTypeError, 'Expected image file.' unless img =~ /jpe?g|png|gif|bmp|tif|tiff|swf$/
@img = img
@url, @hosturi, @res = "","",""
@header, @params = {}, {}
@header['Cookie'] = "myimages=#{SHACK_ID}"
@header['User-Agent'] = USER_AGENT
@params['uploadtype'] = 'on'
@params['brand'] = ''
@params['refer'] = ''
@params['MAX_FILE_SIZE'] = '13145728'
@params['optimage'] = '0'
@params['rembar'] = '1'
transfer
getdirect
end
protected
def prepare_multipart (params)
fp = []
params.each do |k,v|
if v.respond_to?(:read)
fp.push(FileParam.new(k,v.path,v.read))
else fp.push(Param.new(k,v))
end
end
query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--"
return query
end
def prepFile(path_to_file)
file = File.new(path_to_file)
@header['Content-Type'] = "multipart/form-data, boundary=" + BOUNDARY + " "
@params['url'] = 'paste image url here'
@params['fileupload'] = file
$query = prepare_multipart(@params)
file.close
end
def locate(path)
path !~ /^http/ ? "local" : "remote"
end
def upload(query, headers={})
Net::HTTP.start(@hosturi.host) do | http |
http.post(@hosturi.path, query, headers);
end
end
def transload(url)
@header['Content-Type'] = 'form-data'
@params['url'] = url
@params['fileupload'] = ''
postreq = Net::HTTP::Post.new(@hosturi.path, @header)
postreq.set_form_data(@params)
return Net::HTTP.new(@hosturi.host, @hosturi.port).start { |http| http.request(postreq) }
end
def transfer
case locate(@img)
when "local"
@hosturi = URI.parse('http://load.imageshack.us/index.php')
prepFile(@img)
@res = upload($query,@header)
when "remote"
@hosturi = URI.parse('http://imageshack.us/transload.php')
@res = transload(@img)
end
end
def getdirect
doc = Hpricot(@res.body)
@url = (doc/"//input").last['value']
end
end
class Param
attr_accessor :k, :v
def initialize(k, v)
@k = k
@v = v
end
def to_multipart
return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n"
end
end
class FileParam
attr_accessor :k, :filename, :content
def initialize(k, filename, content)
@k = k
@filename = filename
@content = content
end
def to_multipart
return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{filename}\"\r\n" +
"Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n"
end
end
제안 된 응용 프로그램에 대한 자세한 정보를 제공 할 수 있습니까? Imageshack으로 뭔가하고 싶니? 그렇다면, 무엇? – bjg
죄송합니다. 포함시켜야합니다. 그래, 사용자가 Imageshack API를 사용하여 이미지를 업로드 할 수 있기를 바랍니다. – ben