Module: Rubyhorn::RestClient::Common

Included in:
MatterhornClient
Defined in:
lib/rubyhorn/rest_client/common.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

Returns the value of attribute cookie.



9
10
11
# File 'lib/rubyhorn/rest_client/common.rb', line 9

def cookie
  @cookie
end

#httpObject (readonly)

Returns the value of attribute http.



8
9
10
# File 'lib/rubyhorn/rest_client/common.rb', line 8

def http
  @http
end

Instance Method Details

#connectObject



11
12
13
14
15
16
# File 'lib/rubyhorn/rest_client/common.rb', line 11

def connect
	uri = URI.parse(config[:url])
	@http = Net::HTTP.new uri.host, uri.port
	@http.set_debug_output $stderr if !config[:debug].nil? and config[:debug].downcase == 'true'	
	
end

#execute_request(uri, req) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubyhorn/rest_client/common.rb', line 25

def execute_request uri, req
	uri.user = config[:user]
	uri.password = config[:password]
    head = Net::HTTP::Head.new uri.request_uri	
    head['X-REQUESTED-AUTH'] = 'Digest'
	res = @http.request head
	
#	if res.code.to_i != 200
      digest_auth = Net::HTTP::DigestAuth.new
      auth = digest_auth.auth_header uri, res['www-authenticate'], req.method 
      req.add_field 'Authorization', auth
      res = @http.request req
#        end
#	res
end

#get(url, args = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubyhorn/rest_client/common.rb', line 41

def get url, args = {}
  query = args.to_a.each { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join("&")
  url = config[:url] + url
  if !query.empty?
    url << "?" << query
  end
  uri = URI.parse(url)
  request = Net::HTTP::Get.new(uri.request_uri)
  request['Cookie'] = @cookie
  response = execute_request(uri, request)
  return response.body    
end

#login(url = "welcome.html") ⇒ Object



18
19
20
21
22
23
# File 'lib/rubyhorn/rest_client/common.rb', line 18

def  url = "welcome.html"
	uri = URI.parse(config[:url] + url)
    req = Net::HTTP::Head.new uri.request_uri
	res = execute_request(uri, req)
	@cookie = res['set-cookie']
end

#multipart_post(url, file, args = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubyhorn/rest_client/common.rb', line 58

def multipart_post url, file, args = {}
  if args.has_key? "filename"
    filename = args["filename"]
  else
    filename = File.basename file.path
  end
  reqparams = args.to_a
  mime_type = MIME::Types.of(filename)
  reqparams << ["BODY", UploadIO.new(file, mime_type, filename)]
  url = config[:url] + url
  uri = URI.parse(url)

  req = Net::HTTP::Post::Multipart.new uri.path, reqparams
  req['Cookie'] = @cookie
  res = execute_request(uri, req)
  return res.body
end

#post(uri, args = {}) ⇒ Object



54
55
56
# File 'lib/rubyhorn/rest_client/common.rb', line 54

def post uri, args = {}
  #TODO implement me!
end