Class: Oos4ruby::HTTPInvoker

Inherits:
Object
  • Object
show all
Defined in:
lib/oos4ruby/http_invoker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, authent) ⇒ HTTPInvoker



9
10
11
12
13
14
15
# File 'lib/oos4ruby/http_invoker.rb', line 9

def initialize(uri, authent)
  @uri = (uri if uri.kind_of?URI) || URI.parse(uri)
  @authent = authent
  
  @headers = {}
  @entry = nil
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/oos4ruby/http_invoker.rb', line 7

def body
  @body
end

#entryObject (readonly)

Returns the value of attribute entry.



7
8
9
# File 'lib/oos4ruby/http_invoker.rb', line 7

def entry
  @entry
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/oos4ruby/http_invoker.rb', line 7

def headers
  @headers
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/oos4ruby/http_invoker.rb', line 7

def response
  @response
end

Instance Method Details

#delete(req = nil, depth = 0) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/oos4ruby/http_invoker.rb', line 92

def delete( req = nil, depth = 0 )
  return false unless is_authenticate?depth, req

  http, req = prepare_http req, :delete
  
  http.start do |connection|
    @response = connection.request(req)

    return delete(req, depth + 1) if need_authentication?(req)

    return true if @response.kind_of? Net::HTTPSuccess

    return false
  end

end

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/oos4ruby/http_invoker.rb', line 21

def get(depth = 0, req = nil)           
  return false unless is_authenticate?depth, req
  
  http, req = prepare_http req, :get
  
  http.start do |connection|
    @response = connection.request(req)

    return get(depth + 1, req) if need_authentication?(req)

    if @response.kind_of?Net::HTTPSuccess
      @body = @response.body
      return true
    end 

    
    return false
  end      
  
end

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/oos4ruby/http_invoker.rb', line 42

def post(contentType, body, depth = 0, req = nil)    
  return false unless is_authenticate?depth, req
  
  http, req = prepare_http req, :post
  
  req.set_content_type contentType
  @headers.each { |k, v| req[k]= v }

  http.start do |connection|
    if body.instance_of?String
      @response = connection.request(req, body)
    else
      req.body_stream = body
      @response = connection.request(req)
    end

    return post(contentType, body, depth + 1, req) if need_authentication?(req)

    return false if @response.code != '201'

    begin
      @entry = Entry.new @response.body
      return true
    rescue ArgumentError          
      return false
    end
  end
  
end

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/oos4ruby/http_invoker.rb', line 72

def put(contentType, body, depth = 0, req = nil)    
  return false unless is_authenticate?depth, req
  
  http, req = prepare_http req, :put    
  
  req.set_content_type contentType
  @headers.each { |k, v| req[k]= v }

  http.start do |connection|
    @response = connection.request(req, body)

    return put(contentType, body, depth +1, req) if need_authentication?(req)

    return true if @response.kind_of? Net::HTTPSuccess        

    return false
  end
  
end

#set_header(name, val) ⇒ Object



17
18
19
# File 'lib/oos4ruby/http_invoker.rb', line 17

def set_header(name, val)
  @headers[name.to_s.gsub(/_/, '-')] = val
end