Forum: Ruby on Rails displaying images from this controllers code

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
607d3eb2d8ae3d07c5075e4f56c479c2?d=identicon&s=25 Chris Gallagher (gala)
on 2007-02-16 13:40
Hi,

Im pretty new to rails so please excuse me if this is a trivial question
to most of you :)

Im making use of the following code that I came accross which allows me
to go and feed amazons alexa service a url and it returns a thumbnaill.

class AmazonController < ApplicationController

  layout 'default'
  def index
    require 'cgi'
    require 'openssl'
    require 'base64'
    require 'open-uri'

    access_id = 'XXXXX'
    secret_id = 'XXXXXXXXXX'

    source_url = 'google.com'

    timestamp = Time.now.strftime("%Y-%m-%dT%H:%M:%SZ")
    sig =
Base64.encode64(OpenSSL::HMAC::digest(OpenSSL::Digest::Digest.new('SHA1'),
secret_id, 'Thumbnail' + timestamp)).strip

    url =
"http://ast.amazonaws.com/Xino?Action=Thumbnail&AWS... +
access_id
    url << "&Signature=" + CGI.escape(sig)
    url << "&Timestamp=" + CGI.escape(timestamp)
    url << "&Url=" + source_url

    begin
      doc = open(url).read
    rescue
      puts "Could not access AWS"
      exit
    end

    m = doc.match(/\<aws:thumbnail[^\>]+exists=\"true\"\>(.+?)\<\//i)

    if m && m[1]
      thumb_url = m[1]
      thumb_url.gsub!(/\&amp;/, '&')
      File.open("#{source_url}.jpg", "w") { |f| f.write
open(thumb_url).read }
      puts "Saved to #{source_url}.jpg"
    elsif m && m.match(/exists=\"false\"/)
      puts "No thumbnail for #{source_url}"
    else
      puts "Error"
    end



  end
end


This all works fine when i fire it off as a ruby script and places the
thumbnail in the same directory as the ruby script. what I want now
though is to bring the same code to rails. I have placed it in a
controller as you can see above and I would like now to display these
thumbnails on the front end view.

How does one access this information and bring it up on the front end? I
dont entirely understand how the image is written to disk which could be
part of the problem.

Cheers
This topic is locked and can not be replied to.