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
# File 'lib/ape/invoker.rb', line 9

def initialize(uriString, authent)
  @last_error = nil
  @crumbs = Crumbs.new
  @uri = AtomURI.check(uriString)
  if (@uri.class == String)
    @last_error = @uri
  end   
  @authent = authent
  @authent_checker = 0
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

#header(which) ⇒ Object



20
21
22
# File 'lib/ape/invoker.rb', line 20

def header(which)
  @response[which]
end

#need_authentication?(req) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ape/invoker.rb', line 34

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



24
25
26
27
28
29
30
31
32
# File 'lib/ape/invoker.rb', line 24

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



48
49
50
# File 'lib/ape/invoker.rb', line 48

def restart_authent_checker
  @authent_checker = 0
end