2

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

boulder_ruby
  • 38,457
  • 9
  • 79
  • 100
  • I'm not really sure what you are asking here. Could you please try to clarify? – jkeuhlen Aug 05 '15 at 20:32
  • I want to use a rails server to serve json to be used to render text in a shoes gui. The goal is to be able to use shoes as a dumb front end. Ultimately the app is going to be used to passively access the users file system so a web app isn't feasible – boulder_ruby Aug 05 '15 at 20:39
  • somebody is abusing their SO privileges with that downvote – boulder_ruby Aug 05 '15 at 20:45
  • Okay, i get what you are trying to do. So what is your question? – jkeuhlen Aug 05 '15 at 20:47
  • I'm not sure if you've ever used Shoes, but it is not readily apparent how one might make http requests within the application such that the response can be actually used. To wit, how can I make HTTP requests within a Ruby Shoes GUI so I can utilize APIs? – boulder_ruby Aug 05 '15 at 20:49

1 Answers1

2

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

kdavh
  • 404
  • 6
  • 13