Search code examples
rubyfileexternal

File not found error when loading a file from a website in Ruby


I'm trying to call resources (images, for example.) from my website to avoid constant updates. Thus far, I've tried just using this:

 @sprite.bitmap = Bitmap.new("http://www.minscandboo.com/minscgame/001-Title01.jpg")

But, this just gives "File not found error". What is the correct method for achieving this?


Solution

  • Try using Net::HTTP to get a local file first:

    require 'net/http'
    
    Net::HTTP.start("minscandboo.com") { |http|
      resp = http.get("/miscgame/001-Title01.jpg")
      open("local-game-image.jpg", "wb") { |file|
        file.write(resp.body)
       }
    }
    
    # ...
    
    @sprite.bitmap = Bitmap.new("local-game-image.jpg")