[DRE-commits] [librarian-puppet] 24/97: Use open-uri to simplify code and support proxies with http_proxy

Stig Sandbeck Mathisen ssm at debian.org
Tue Mar 11 12:12:48 UTC 2014


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository librarian-puppet.

commit 6590f912b7449d1cc865ca2c3f0bb3fef7c6514d
Author: Carlos Sanchez <csanchez at maestrodev.com>
Date:   Tue Jul 16 12:25:04 2013 +0200

    Use open-uri to simplify code and support proxies with http_proxy
---
 lib/librarian/puppet/source/forge.rb | 46 +++++++++++++-----------------------
 1 file changed, 17 insertions(+), 29 deletions(-)

diff --git a/lib/librarian/puppet/source/forge.rb b/lib/librarian/puppet/source/forge.rb
index dccdea9..ac93b6c 100644
--- a/lib/librarian/puppet/source/forge.rb
+++ b/lib/librarian/puppet/source/forge.rb
@@ -1,6 +1,5 @@
-require 'uri'
-require 'net/http'
 require 'json'
+require 'open-uri'
 
 module Librarian
   module Puppet
@@ -132,26 +131,14 @@ module Librarian
           end
 
           def vendor_cache(name, version)
-            File.open(vendored_path(name, version).to_s, 'w') do |f|
-              download(name, version) do |data|
-                f << data
-              end
-            end
-          end
-
-          def download(name, version, &block)
             data = api_call("api/v1/releases.json?module=#{name}&version=#{version}")
-
             info = data[name].detect {|h| h['version'] == version.to_s }
-
-            stream(info['file'], &block)
-          end
-
-          def stream(file, &block)
-            Net::HTTP.get_response(URI.parse("#{source}#{file}")) do |res|
-              res.code
-
-              res.read_body(&block)
+            File.open(vendored_path(name, version).to_s, 'w') do |f|
+              open("#{source}#{info['file']}") do |input|
+                while (buffer = input.read)
+                  f.write(buffer)
+                end
+              end
             end
           end
 
@@ -162,16 +149,17 @@ module Librarian
         private
 
           def api_call(path)
-            base_url = source.to_s
-            resp = Net::HTTP.get_response(URI.parse("#{base_url}/#{path}"))
-            case resp.code.to_i
-            when 404,410
-              nil
-            when 200
-              data = resp.body
+            base_url = source.uri
+            begin
+              data = open("#{base_url}/#{path}") {|f| f.read}
               JSON.parse(data)
-            else
-              raise Error, "Error requesting #{base_url}/#{path}: [#{resp.code}] #{resp.body}"
+            rescue OpenURI::HTTPError => e
+              case e.io.status[0].to_i
+              when 404,410
+                nil
+              else
+                raise e, "Error requesting #{base_url}/#{path}: #{e.to_s}"
+              end
             end
           end
         end

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/librarian-puppet.git



More information about the Pkg-ruby-extras-commits mailing list