lib/gem-search/cli.rb in gem-search-0.0.6 vs lib/gem-search/cli.rb in gem-search-0.0.7
- old
+ new
@@ -10,28 +10,40 @@
def search(query, opt_sort='name')
return unless query
opt_sort ||= 'name'
url = "#{SEARCH_URL}#{query}"
-
- begin
- open(url) do |f|
+
+ begin
+ open_uri(url) do |f|
gems = JSON.parse(f.read)
if gems.size.zero?
puts 'We did not find results.'
return
end
fmt_size = ruled_line_size(gems)
gems_sort!(gems, opt_sort)
render_header(fmt_size)
render_body(gems, fmt_size)
end
- rescue
+ rescue => e
puts 'An unexpected Network error has occurred.'
+ puts e
end
end
private
+ def open_uri(url, &block)
+ option = {}
+ proxy = URI.parse(url).find_proxy
+ if proxy
+ if proxy.user && proxy.password
+ option[:proxy_http_basic_authentication] = [proxy, proxy.usesr, proxy.password]
+ end
+ end
+ open(url, option, &block)
+ end
+
def ruled_line_size(gems)
line_size = DEFAULT_RULED_LINE_SIZE.dup
max_name_size = gems.map { |gem| "#{gem['name']} (#{gem['version']})".size }.max
line_size[0] = max_name_size if max_name_size > line_size[0]
line_size