Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveResource should support URI::Generic

#352

ActiveResource detects whether to use SSL with is_a?(URI::HTTPS), which causes problems if you have a URI::Generic object, which is useful if your ActiveResource configuration could be HTTP or HTTPS. It is, of course, easy to workaround by forcing the URI::Generic instance through URI.parse which will determine the correct class. A more elegant solution is to look at the URI::HTTP#scheme attribute to determine whether to use SSL.

quick patch, sorry no test:


diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/l
index 0c4ea43..4f653f0 100644
--- a/activeresource/lib/active_resource/connection.rb
+++ b/activeresource/lib/active_resource/connection.rb
@@ -180,7 +180,7 @@ module ActiveResource
       # remote service and resources.
       def http
         http             = Net::HTTP.new(@site.host, @site.port)
-        http.use_ssl     = @site.is_a?(URI::HTTPS)
+        http.use_ssl     = @site.scheme == 'https'
         http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl
         http.read_timeout = @timeout if @timeout # If timeout is not set, the d
         http

Reported by Ian Smith-Heisters · June 13th, 2008 @ 04:43 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Ian Smith-Heisters
    Ian Smith-Heisters

    I'll try that as an attachment

    June 6th, 2008 @ 02:12 AM

  2. Gaius Centus Novus
    Gaius Centus Novus

    Seems fine; in fact, seems like the "truer" test of what's intended, but I can't see setting the connection URL to anything other than HTTP or HTTPS.

    Maybe the use case is using ActiveResource to find things on the same server? In that case, you might pass a root-relative instead of absolute string to URI::parse (like '/my-other-app/'), which would return a URI::Generic. In that case, though, SSL (a) would be unnecessary and (b) still couldn't be determined from the URI.

    June 13th, 2008 @ 11:01 AM

  3. Ian Smith-Heisters
    Ian Smith-Heisters

    My use case was allowing configuration with a YAML file like so:

    
    # See URI::Generic#build for possible configuration options
    scheme: http
    host: 127.0.0.1
    userinfo: 'username:password'
    port: 3000
    path: /
    

    Then you read it into ActiveResource like so:

    
    CONFIG = YAML.load(ERB.new(File.read(File.join(RAILS_ROOT, 'config', 'intranet.yml'))).result).with_indifferent_access
    self.site = URI::Generic.build(CONFIG)
    

    Generic is necessary because developers will be using http on localhost, while production servers will use https.

    HTH

    June 13th, 2008 @ 04:41 PM

  4. Gaius Centus Novus
    Gaius Centus Novus

    Why not do this:

    in conf/intranet.yml:

    active_resource_url: http://user:password@127.0.0.1:3000/path

    in loader.rb:

    CONFIG = YAML.load(ERB.new(File.read(File.join(RAILS_ROOT, 'config',

    'intranet.yml'))).result).with_indifferent_access

    self.site = URI::parse(CONFIG[:active_resource_url)

    That way, the developers can specify whatever they want, and you

    always get an HTTP or HTTPS URI.

    -James

    June 13th, 2008 @ 04:55 PM

  5. Ian Smith-Heisters
    Ian Smith-Heisters

    yeah, that's essentially what I'm doing now, except that I approached it by modifying

    
    self.site = URI::Generic.build(CONFIG)
    

    to

    
    self.site = URI::Generic.build(CONFIG).to_s
    

    ...exploiting the fact that self.site= will call URI.parse for me. IMO it's just a little nicer in that it still allows specifying the url in its various parts with labels (clearer), and that it keeps CONFIG namespaced to the ActiveResource descendant. Six of one, half a dozen of the other.

    The point isn't that there isn't a workaround, but that the duck typing approach is just slightly more flexible and less surprising.

    June 13th, 2008 @ 05:40 PM

  6. Gaius Centus Novus
    Gaius Centus Novus

    No, I like your fix. My point was that the fix is really for the sake of "doing the right thing" rather than providing some unavailable functionality.

    June 13th, 2008 @ 05:43 PM

  7. Ian Smith-Heisters
  8. Pratik
    Pratik
    • State changed from new to invalid
    • Tag set to activeresource, patch

    Missing tests.

    August 21st, 2008 @ 12:48 PM

  9. Pratik
    Pratik
    • State changed from invalid to incomplete

    August 21st, 2008 @ 12:48 PM

  10. Rohit Arondekar
    Rohit Arondekar
    • State changed from incomplete to stale
    • Importance changed from to

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

    October 6th, 2010 @ 06:44 AM