开发者

How do I use this Ruby code?

I found this Ruby code to help use the Imageshack API on CodeSnippets. I'm new to Ruby on Rails, so I know how to use models, views and controllers generally, but I'm not sure how I just use this code. Would it be a module? Thanks for reading.

Usage:

pic_online = ShackMirror.new(local_path_of_pic)

pic_online.url # => returns direct link on 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::es开发者_如何学运维cape(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


The usage information seems pretty straight-forward.

pic_online = ShackMirror.new(local_path_of_pic)
pic_online.url

The purpose of the code seems to be to upload a local image file to imageshack and give you back the URL for the image once complete. The first line does the image upload and the second line returns the URL of the image.

The only thing extra you seem to need is to place your image shack ID into the class by updating the SHACK_ID constant.


This isn't a helper file, it's a library file. So, if you want to use it, you can just drop it in the lib folder.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜