Class: Ape::Invoker

Inherits:
Object
  • Object
show all
Defined in:
lib/ape/invoker.rb

Direct Known Subclasses

Deleter, Getter, Poster, Putter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uriString, authent) ⇒ Invoker

Returns a new instance of Invoker.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ape/invoker.rb', line 9

def initialize(uriString, authent)
  @last_error = nil
  
  # XXX really need a way to turn crumbs on/off, mostly off
  @crumbs = Crumbs.new
  @uri = AtomURI.check(uriString)
  if (@uri.class == String)
    @last_error = @uri
  end   
  @authent = authent
  @authent_checker = 0
  @headers = {}
end

Instance Attribute Details

#crumbsObject (readonly)

Returns the value of attribute crumbs.



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

def crumbs
  @crumbs
end

#last_errorObject (readonly)

Returns the value of attribute last_error.



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

def last_error
  @last_error
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#[](name) ⇒ Object

get a request header



29
30
31
# File 'lib/ape/invoker.rb', line 29

def [](name)
  @headers[name]
end

#[]=(name, val) ⇒ Object

Add a new request header



24
25
26
# File 'lib/ape/invoker.rb', line 24

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

#header(which) ⇒ Object



37
38
39
# File 'lib/ape/invoker.rb', line 37

def header(which)
  @response[which]
end

#need_authentication?(req) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ape/invoker.rb', line 51

def need_authentication?(req)
  if @response.instance_of?(Net::HTTPUnauthorized) && @authent
    #tries to authenticate just two times in order to avoid infinite loops
    raise AuthenticationError, 'Authentication is required' unless @authent_checker <= 1
    @authent_checker += 1
    
    @authent.add_to req, header('WWW-Authenticate')
    #clean the request body attribute, if we don't do it http.request(req, body) will raise an exception
    req.body = nil unless req.body.nil?
    return true
  end
  return false 
end

#prepare_httpObject



41
42
43
44
45
46
47
48
49
# File 'lib/ape/invoker.rb', line 41

def prepare_http
  http = Net::HTTP.new(@uri.host, @uri.port)
  if @uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  http.set_debug_output @crumbs if @crumbs
  http
end

#restart_authent_checkerObject



65
66
67
# File 'lib/ape/invoker.rb', line 65

def restart_authent_checker
  @authent_checker = 0
end

#set_header(name, val) ⇒ Object



33
34
35
# File 'lib/ape/invoker.rb', line 33

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