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

北慕城南 提交于 2019-12-09 19:36:58

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/31842209/how-can-i-make-simple-http-requests-within-a-ruby-shoes-gui-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!