Class: Ape::Poster

Inherits:
Invoker show all
Defined in:
lib/ape/invokers/poster.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) ⇒ Poster

Returns a new instance of Poster.



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

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

Instance Attribute Details

#entryObject (readonly)

Returns the value of attribute entry.



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

def entry
  @entry
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

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



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
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ape/invokers/poster.rb', line 19

def post(contentType, body, req = nil)
  req = Net::HTTP::Post.new(AtomURI.on_the_wire(@uri)) if req.nil?
  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 post(contentType, body, req) if need_authentication?(req)
      restart_authent_checker
      
      if @response.code != '201'
        @last_error = @response.message
        return false
      end

      if (!((@response['Content-type'] =~ %r{^application/atom\+xml}) ||
            (@response['Content-type'] =~ %r{^application/atom\+xml;type=entry})))
        return true
      end

      begin
        @entry = Entry.new(@response.body)
        return true
      rescue ArgumentError
        @last_error = @entry.broken
        return false
      end
    end
  rescue Exception
    @last_error = "Can't connect to #{@uri.host} on port #{@uri.port}: #{$!}"
    return false
  end
end

#set_header(name, val) ⇒ Object



15
16
17
# File 'lib/ape/invokers/poster.rb', line 15

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