Class: Ape::Getter

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

Instance Attribute Summary collapse

Attributes inherited from Invoker

#crumbs, #last_error, #response

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Invoker

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

Constructor Details

This class inherits a constructor from Ape::Invoker

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#charsetObject (readonly)

Returns the value of attribute charset.



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

def charset
  @charset
end

#contentTypeObject (readonly)

Returns the value of attribute contentType.



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

def contentType
  @contentType
end

#security_warningObject (readonly)

Returns the value of attribute security_warning.



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

def security_warning
  @security_warning
end

Class Method Details

.retrieve(uri, content_type = nil, authent = nil) ⇒ Object

Handy wrapper with optional media-type checking; return the Response object

Raises:

  • (Exception)


86
87
88
89
90
91
92
93
# File 'lib/ape/invokers/getter.rb', line 86

def Getter.retrieve(uri, content_type=nil, authent=nil)

  getter = Getter.new(uri, authent)
  raise(Exception, "Bad URI value #{uri}") if getter.last_error

  getter.get content_type
  return getter.response
end

Instance Method Details

#codeObject



61
62
63
# File 'lib/ape/invokers/getter.rb', line 61

def code
  @response.code
end

#document_failed?(depth, req) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ape/invokers/getter.rb', line 47

def document_failed?(depth, req)    
  if (depth > 10)      
    if need_authentication?(req)
  	#Authentication required
      @last_error = "Authentication is required"
    else
  	# too many redirects
      @last_error = "Too many redirects"
    end
    return true
  end
  return false
end

#get(contentType = nil, depth = 0, req = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
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
45
# File 'lib/ape/invokers/getter.rb', line 9

def get(contentType = nil, depth = 0, req = nil)
  req = Net::HTTP::Get.new(AtomURI.on_the_wire(@uri)) unless req
  @last_error = nil

  return false if document_failed?(depth, req)    
  
  begin
    http = prepare_http
   
    http.start do |connection|
      @response = connection.request(req)

      if need_authentication?(req)
        @security_warning = true unless http.use_ssl?
        return get(contentType, depth + 1, req) 
      end
      restart_authent_checker
      
      case @response
      when Net::HTTPSuccess
        return getBody(contentType)
        
      when Net::HTTPRedirection
        redirect_to = @uri.merge(@response['location'])
        @uri = AtomURI.check(redirect_to)
        return get(contentType, depth + 1)
        
      else
        @last_error = @response.message
        return false
      end      
    end
  rescue Exception
    @last_error = "Can't connect to #{@uri.host} on port #{@uri.port}: #{$!}"
    return false
  end
end

#getBody(contentType) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ape/invokers/getter.rb', line 65

def getBody contentType

  if contentType
    @contentType = @response['Content-Type']
    # XXX TODO - better regex
    if @contentType =~ /^([^;]*);/
      @contentType = $1
    end

    if contentType != @contentType
      @response = Net::HTTPGone.new('1.1', '409',
        "Content-type must be '#{contentType}', not '#{@contentType}'")
      @body = ""
    end
  end
    
  @body = @response.body
  return true
end