Skip to content

Instantly share code, notes, and snippets.

@dangerouse
Last active October 14, 2015 05:37
Show Gist options
  • Save dangerouse/1623391 to your computer and use it in GitHub Desktop.
Save dangerouse/1623391 to your computer and use it in GitHub Desktop.
Cloudsponge Proxy Reference - Ruby
require 'cloudsponge'
def cloudsponge_proxy
url = Cloudsponge::URL_BASE + 'auth?' + request.query_string
response = Cloudsponge::Utility::get_url(url)
if response.is_a?(Net::HTTPRedirection)
redirect_to response["location"]
else
render :text => response.try(:body)
end
end
@sbull
Copy link

sbull commented Aug 26, 2013

It's somewhat unclear what framework this bit of code fits into, but in case you're using Rails 4 (or Rack?), some edits are needed. Particularly, request.request_method returns a String, so the symbol comparisons aren't very friendly.

In Rails, it's probably better to use request.post? and request.get? instead.

@sbull
Copy link

sbull commented Aug 26, 2013

Here's my revision for Rails, FWIW.

class CloudspongeController < ApplicationController

  def auth_proxy
    url = "#{Cloudsponge::URL_BASE}auth?#{request.query_string}"
    proxy_response =
      if request.get?
        Cloudsponge::Utility::get_url(url)
      else
        Cloudsponge::Utility::post_url(url, request.parameters)
      end
    if proxy_response.is_a?(Net::HTTPRedirection)
      redirect_to proxy_response['location']
    else
      render text: proxy_response.try(:body)
    end
  end

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment