Class: Ape::Putter

Inherits:
Invoker show all
Defined in:
lib/ape/invokers/putter.rb

Instance Attribute Summary collapse

Attributes inherited from Invoker

#crumbs, #last_error, #response

Instance Method Summary collapse

Methods inherited from Invoker

#[], #[]=, #header, #need_authentication?, #prepare_http, #restart_authent_checker

Constructor Details

#initialize(uriString, authent) ⇒ Putter

Returns a new instance of Putter.



9
10
11
12
# File 'lib/ape/invokers/putter.rb', line 9

def initialize(uriString, authent)
  super uriString, authent
  @headers = {}
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/ape/invokers/putter.rb', line 7

def headers
  @headers
end

Instance Method Details

#put(contentType, body, req = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ape/invokers/putter.rb', line 18

def put(contentType, body, req = nil)
  req = Net::HTTP::Put.new(AtomURI.on_the_wire(@uri)) unless req
  
  req.set_content_type contentType
  @headers.each { |k, v| req[k]= v }

  begin
    http = prepare_http
    
    http.start do |connection|
      @response = connection.request(req, body)
      
      return put(contentType, body, req) if need_authentication?(req)
      restart_authent_checker
      
      unless @response.kind_of? Net::HTTPSuccess
        @last_error = @response.message
        return false
      end
      
      return true
    end
  rescue Exception
    @last_error = "Can't connect to #{@uri.host} on port #{@uri.port}: #{$!}"
    return nil
  end
end

#set_header(name, val) ⇒ Object



14
15
16
# File 'lib/ape/invokers/putter.rb', line 14

def set_header(name, val)
  @headers[name] = val
end