How can I make simple http requests within a ruby Shoes GUI application?

I'm trying to make a simple http request to a local rails application running at localhost:3000. The code for my application is very much in prototype phase as I am only trying to get raw functionality before I try anything else.

The rails app simply returns JSON that looks like this before being parsed

"{"response":"foo is blank"}"

Here's the code I'm trying right now:

require 'net/http'

def get_response
  return Net::HTTP.get(URI.parse("http://localhost:3000"))
end

Shoes.app(:width => 280, :height => 350) do

  flow do
    stack do
      button "make http request and display results in an alert box" do
        x = get_response
        alert x
      end# of button
    end# of stack
  end# of flow
end

I've also tried to put the method inside the Shoes.app block to no avail. When executed, this creates all the elements that it should create, but nothing happens when I click the button.

I know you're not supposed to be able to do anything practical with ruby shoes, but I'm trying anyway. Thanks for your time


The problem for me when I tried to run your code was that the Shoes app couldn't connect locally using localhost . Instead of using localhost , use your computer's local IP address.

So just change this:

Net::HTTP.get(URI.parse("http://localhost:3000"))

to this:

Net::HTTP.get(URI.parse("http://10.1.4.123:3000"))

if your computer's local IP is 10.1.4.123 .

To see the error logs that confirm this is happening for you, insert this line at the top of your shoes app: Shoes.show_log

Or simply enter cmd + / on a Mac or ctrl + / in Windows(?) when you are running your *.rb file in Shoes.app

For me the error that showed up was:

Connection refused - connect(2) for "::1" port 3000

链接地址: http://www.djcxy.com/p/79718.html

上一篇: 为什么开源许可证的激增?

下一篇: 如何在ruby Shoes GUI应用程序中创建简单的http请求?