Module: AwsImageshack::PublicControllerMethods

Defined in:
lib/aws_imageshack.rb

Instance Method Summary collapse

Instance Method Details

#aws_imageshack(*args) ⇒ Object

options

- size : Size of the file input
- style_css : Style apply to the file input (replace the default css)
- class_css : Class apply to the file input
- width     : Witdh of image in the preview
- height    : Height of image in the preview
- position  : Position for the preview ( left, right, top, left)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/aws_imageshack.rb', line 32

def aws_imageshack(*args)
  options = args.extract_options!

  @aws_params = options[:params]
  @aws_api_key = options[:api_key]  
  @aws_options = options[:options]  
  if @aws_params[:aws] and @aws_params[:aws][:fileupload]
    image_link = aws_imageshack_save(@aws_params, @aws_api_key)
    responds_to_parent do
      render :update do |page|
        page.replace 'aws_upload_form', :inline => aws_image_shack_form(image_link)
      end
    end
    return image_link
  end
end

#aws_imageshack_save(params, api_key) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/aws_imageshack.rb', line 49

def aws_imageshack_save(params, api_key)
  require "net/http"
  require "net/http/post/multipart"

  name = params[:aws][:fileupload].original_filename
  directory = 'tmp/uploads'
  `mkdir "#{directory}"` if !File.exists?(directory)
  path = File.join(directory, name)
  File.open(path, "wb") { |f| f.write(params[:aws][:fileupload].read)}
  extension = File.extname( path ).sub( /^\./, '' ).downcase
  mime_type = params[:aws][:fileupload].content_type

  url = URI.parse('http://www.imageshack.us/upload_api.php')
  File.open(path) do |filedata|
    req = Net::HTTP::Post::Multipart.new url.path,
      "fileupload" => UploadIO.new(filedata, mime_type, path),
      "key" => api_key
    res = Net::HTTP.start(url.host, url.port) {|http| http.request(req)}
    image_link = res.body.scan(/<image_link>(.*)<\/image_link>/)
    return image_link[0].to_s
  end
end

#responds_to_parent(&block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/aws_imageshack.rb', line 7

def responds_to_parent(&block)
  yield 
  if performed?
    script = if location = erase_redirect_results
                "document.location.href = '#{self.class.helpers.escape_javascript location.to_s}'"
              else
                response.body || ''
              end

    erase_results
    response.headers['Content-Type'] = 'text/html; charset=UTF-8'
    render :text => "<html><body><script type='text/javascript' charset='utf-8'>
var loc = document.location;
with(window.parent) { setTimeout(function() { window.eval('#{self.class.helpers.escape_javascript script}'); window.loc && loc.replace('about:blank'); }, 1) }
</script></body></html>"
  end
end