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
# 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
end

Instance Method Details

#download(url) ⇒ Object



53
54
55
# File 'lib/puppet_forge_server/http/http_client.rb', line 53

def download(url)
  open_uri(url)
end

#get(url) ⇒ Object



49
50
51
# File 'lib/puppet_forge_server/http/http_client.rb', line 49

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

#inspectObject



57
58
59
60
61
62
# File 'lib/puppet_forge_server/http/http_client.rb', line 57

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



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/puppet_forge_server/http/http_client.rb', line 36

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