Class: PuppetForgeServer::Http::HttpClient

Inherits:
Object
  • Object
show all
Includes:
Utils::CacheProvider, Utils::FilteringInspecter
Defined in:
lib/puppet_forge_server/http/http_client.rb

Instance Method Summary collapse

Methods included from Utils::FilteringInspecter

inspect_without, #inspect_without

Methods included from Utils::CacheProvider

#cache_instance, #configure_cache

Constructor Details

#initialize(cache = nil) ⇒ HttpClient

Returns a new instance of HttpClient.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puppet_forge_server/http/http_client.rb', line 29

def initialize(cache = nil)
  cache = cache_instance if cache.nil?
  cache.extend(PuppetForgeServer::Utils::FilteringInspecter)
  @log = PuppetForgeServer::Logger.get
  @cache = cache
  @uri_options= {
    'User-Agent' => "Puppet-Forge-Server/#{PuppetForgeServer::VERSION}",
    :allow_redirections => :safe,
  }
  # OpenURI does not work with  http_proxy=http://username:password@proxyserver:port/
  # so split the proxy_url and feed it basic authentication.
  if ENV.has_key?('http_proxy')
    proxy = URI.parse(ENV['http_proxy'])
    if proxy.userinfo != nil
      @uri_options[:proxy_http_basic_authentication] = [
        "#{proxy.scheme}://#{proxy.host}:#{proxy.port}",
        proxy.userinfo.split(':')[0],
        proxy.userinfo.split(':')[1]
      ]
    end
  end

end

Instance Method Details

#download(url) ⇒ Object



70
71
72
# File 'lib/puppet_forge_server/http/http_client.rb', line 70

def download(url)
  open_uri(url)
end

#get(url) ⇒ Object



66
67
68
# File 'lib/puppet_forge_server/http/http_client.rb', line 66

def get(url)
  open_uri(url).read
end

#inspectObject



74
75
76
77
78
79
# File 'lib/puppet_forge_server/http/http_client.rb', line 74

def inspect
  cache_inspected = @cache.inspect_without [ :@data ]
  cache_inspected.gsub!(/>$/, ", @size=#{@cache.size}>")
  inspected = inspect_without [ :@cache ]
  inspected.gsub(/>$/, ", @cache=#{cache_inspected}>")
end

#post_file(url, file_hash, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/puppet_forge_server/http/http_client.rb', line 53

def post_file(url, file_hash, options = {})
  options = { :http => {}, :headers => {}}.merge(options)

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  options[:http].each {|k,v| http.call(k, v) }

  req = Net::HTTP::Post::Multipart.new uri.path, "file" => UploadIO.new(File.open(file_hash[:tempfile]), file_hash[:type], file_hash[:filename])
  options[:headers].each {|k,v| req[k] = v }

  http.request(req)
end