Rails переписать, как переписать этот код в код ruby? - PullRequest
0 голосов
/ 01 апреля 2012

У меня есть промежуточное ПО этого класса:

class RedirectIt
require "net/https"
require "uri"
require 'open-uri'

  APP_DOMAIN = 'http://www.mydomain.com'
  def initialize(app)
    @app = app
  end

  def call(env)
        request = Rack::Request.new(env)
        response = Rack::Response.new(env)
        response.headers['Cache-Control'] = "public, max-age=#{84.hours.to_i}"
        response.headers['Content-Type'] = 'image/png'
        response.headers['Content-Disposition'] = 'inline'
        response.body = "#{open('http://s3-eu-west-1.amazonaws.com/bucket/asdas.png').read}"
  end

end

Проблема только в том, что он выдает ошибку:

Started GET "/?view=boks" for 127.0.0.1 at 2012-04-01 04:07:58 +0200

NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]=):

Я что-то не так делаю?Я попытался переписать этот код, который я имел в контроллере:

def image_proxy
  image_url = "http://s3-eu-west-1.amazonaws.com/bucket#{request.path}"
  response.headers['Cache-Control'] = "public, max-age=#{84.hours.to_i}"
  response.headers['Content-Type'] = 'image/png'
  response.headers['Content-Disposition'] = 'inline'
  render :text => open(image_url, "rb").read
end

1 Ответ

0 голосов
/ 01 апреля 2012

Решение.

    #PROXY BILLEDER
    status, headers, response = @app.call(env)
    headers['Cache-Control'] = "public, max-age=#{84.hours.to_i}"
    headers['Content-Type'] = 'image/png'
    headers['Content-Disposition'] = 'inline'
    response_body = "#{(open('http://s3-eu-west-1.amazonaws.com/mybucket#{request.path()}')).read}"
    [status, headers, response_body]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...