Dealing with HTTP.get response

148 views
Skip to first unread message

Johnny Holton

unread,
Jul 23, 2012, 9:37:25 AM7/23/12
to bubbl...@googlegroups.com
I'm trying to retrieve some "posts" from my Rails app.

Here's my request:
response = BubbleWrap::HTTP.get(App::Persistence['app_url'].to_s + "/posts", {credentials: {username: App::Persistence['auth_token'], password: 'X'}}).response

if response.ok?
  App.alert("It's OK!")
elsif response.status_code.to_s =~ /40\d/
  App.alert("Login failed")
else
  App.alert(response.ok?.to_s)
end

When I run the code, I get "false" from the last part of the if conditional

However, while the REPL is running, if I paste in the get request, then response.ok? is true and I can access the response like I expect to.

Why isn't it working in the app?

Thanks.

Christopher Brickley

unread,
Jul 23, 2012, 9:52:53 AM7/23/12
to bubbl...@googlegroups.com
Have you tried the block form?

BubbleWrap::HTTP.get(App::Persistence['app_url'].to_s + "/posts", {credentials: {username: App::Persistence['auth_token'], password: 'X'}}) do |response|
  if response.ok?
    App.alert("It's OK!")
  elsif response.status_code.to_s =~ /40\d/
    App.alert("Login failed")
  else
    App.alert(response.ok?.to_s)
  end
end


--
You received this message because you are subscribed to the Google Groups "BubbleWrap" group.
To post to this group, send email to bubbl...@googlegroups.com.
To unsubscribe from this group, send email to bubblewrap+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/bubblewrap/-/uNEPy-JYCo8J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Johnny Holton

unread,
Jul 23, 2012, 10:25:54 AM7/23/12
to bubbl...@googlegroups.com
That seems to work in regards to evaluating the response correctly, but I'm having trouble getting my data returned where it needs to go.  I'll explain.

What I want to happen is I call a method that executes this code and returns the body (JSON).

I've extracted this code to a method in a class I created:

class Post
def self.index
  temp_response = "hey hey"
    BubbleWrap::HTTP.get(App::Persistence['app_url'].to_s + "/posts", {credentials: {username: App::Persistence['auth_token'], password: 'X'}}) do |response|
      if response.ok?
          temp_response = BubbleWrap::JSON.parse(response.body.to_str)
        elsif response.status_code.to_s =~ /40\d/
          App.alert("Login failed")
        else
          App.alert(response.ok?.to_s)
        end
    end
  temp_response
  end
end

When Post.index is called, I get back "hey hey".  I never get back the parsed JSON.  Why?

Now if I instantiate an instance variable (@temp_response) in the view controller that calls Post.index, and then use that instance variable in place of the old 'temp_response', it works!
But that seems......messy.

Why is my local variable (temp_response) not taking the value I expect in the block?
What is the best way to do this?

Thanks!
-Johnny

On Monday, July 23, 2012 9:52:53 AM UTC-4, Christopher Brickley wrote:
Have you tried the block form?

BubbleWrap::HTTP.get(App::Persistence['app_url'].to_s + "/posts", {credentials: {username: App::Persistence['auth_token'], password: 'X'}}) do |response|
  if response.ok?
    App.alert("It's OK!")
  elsif response.status_code.to_s =~ /40\d/
    App.alert("Login failed")
  else
    App.alert(response.ok?.to_s)
  end
end

On Mon, Jul 23, 2012 at 9:37 AM, Johnny Holton wrote:
I'm trying to retrieve some "posts" from my Rails app.

Here's my request:
response = BubbleWrap::HTTP.get(App::Persistence['app_url'].to_s + "/posts", {credentials: {username: App::Persistence['auth_token'], password: 'X'}}).response

if response.ok?
  App.alert("It's OK!")
elsif response.status_code.to_s =~ /40\d/
  App.alert("Login failed")
else
  App.alert(response.ok?.to_s)
end

When I run the code, I get "false" from the last part of the if conditional

However, while the REPL is running, if I paste in the get request, then response.ok? is true and I can access the response like I expect to.

Why isn't it working in the app?

Thanks.

--
You received this message because you are subscribed to the Google Groups "BubbleWrap" group.
To post to this group, send email to bubbl...@googlegroups.com.
To unsubscribe from this group, send email to bubblewrap+unsubscribe@googlegroups.com.

Dylan Markow

unread,
Jul 23, 2012, 10:33:02 AM7/23/12
to bubbl...@googlegroups.com
There is a bug with the scope of local variables within blocks, Laurent said this should be fixed in the next release. An alternative to using an instance variable might be calling temp_response.retain.


Johnny Holton

unread,
Jul 23, 2012, 10:58:28 AM7/23/12
to bubbl...@googlegroups.com
Ah, I was not aware.
I'll use an instance variable for now.

Thanks for the info.
Reply all
Reply to author
Forward
0 new messages