Module: HttpClient

Defined in:
lib/httpclient/httpclient.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#obtained_bodyObject (readonly)

Returns the value of attribute obtained_body.



158
159
160
# File 'lib/httpclient/httpclient.rb', line 158

def obtained_body
  @obtained_body
end

#obtained_codeObject (readonly)

Returns the value of attribute obtained_code.



158
159
160
# File 'lib/httpclient/httpclient.rb', line 158

def obtained_code
  @obtained_code
end

#obtained_headersObject (readonly)

Returns the value of attribute obtained_headers.



158
159
160
# File 'lib/httpclient/httpclient.rb', line 158

def obtained_headers
  @obtained_headers
end

Instance Method Details

#admin(key, &block) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/httpclient/httpclient.rb', line 160

def admin( key, &block )
  if key == nil
    anonymous( &block )
  else
    @auth = { :cmuser => key }
    instance_eval( &block )
  end
end

#anonymous(&block) ⇒ Object



175
176
177
178
# File 'lib/httpclient/httpclient.rb', line 175

def anonymous(  &block )
  @auth = { :cmuser => 'anonymous' }
   instance_eval( &block )
end

#connect(&block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/httpclient/httpclient.rb', line 54

def connect( &block )
  uri = URI.parse( @@host )
  h = Net::HTTP.new( uri.host, uri.port )      
  if( uri.scheme == 'https' )
     h.use_ssl = uri.scheme
     h.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  headers = {}
  headers.merge!( @auth ) if @auth
  headers.desym!
  
  block.call( h, headers )
end

#create(path, form = nil, &block) ⇒ Object Also known as: update



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/httpclient/httpclient.rb', line 118

def create( path, form = nil, &block )
  connect do |http, headers|
    data = nil
    headers[ 'Content-Length' ] = '0'
  
    if form
      data, extra_headers = Multipart::Post.prepare_query( form )
      headers.merge!( extra_headers )
      headers[ 'Content-Length' ] = data.length.to_s
    end
  
    debug_request( :post, path, headers, form )
   
    res = http.post path, data, headers
    handle_response( res, block )
  end
end

#debug(&block) ⇒ Object



68
69
70
71
72
# File 'lib/httpclient/httpclient.rb', line 68

def debug( &block )
   @debug = true
   instance_eval( &block )
   @debug = false
end

#debug_request(http_method, path, headers, form = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/httpclient/httpclient.rb', line 76

def debug_request( http_method, path, headers, form = nil )
  if @debug
    puts ""
    puts "Request"
    puts "==========================="
    puts "method: #{http_method}"
     puts "path: #{path}"
    puts "headers: #{headers}"
    puts "form: #{form}" if form
    puts ""
  end
end

#debug_response(code, headers, body) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/httpclient/httpclient.rb', line 89

def debug_response( code, headers, body )
 if @debug
     puts ""
      puts "Response"
      puts "==========================="
      puts "status code: #{code}"
      puts "headers: #{headers}"
      puts "body: #{body}"
      puts ""
   end
end

#handle_response(res, block = nil) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/httpclient/httpclient.rb', line 139

def handle_response( res, block = nil )
 code = res.code.to_i
 headers = res.to_hash
 body = res.body
 
 debug_response( code, headers, body )
 
 if res[ 'Content-Type' ] == 'text/xml'
   body = REXML::Document.new( body )
 end
 if block
   block.call( code, body, headers ) 
 else
   @obtained_code = code
   @obtained_body = body
   @obtained_headers = headers
 end
end

#host(host) ⇒ Object



50
51
52
# File 'lib/httpclient/httpclient.rb', line 50

def host( host  )
  @@host = host
end

#remove(path, &block) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/httpclient/httpclient.rb', line 109

def remove( path, &block )
  connect do |http, headers|
    debug_request( :delete, path, headers )
     res = http.delete path, headers
    handle_response( res, block )
  end
end

#retrieve(path, &block) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/httpclient/httpclient.rb', line 101

def retrieve( path, &block )
  connect do |http, headers|
    debug_request( :get, path, headers )
    res = http.get path, headers
    handle_response( res, block )
  end
end

#user(username, password = nil, &block) ⇒ Object



169
170
171
172
173
# File 'lib/httpclient/httpclient.rb', line 169

def user( username, password = nil, &block )
   @auth = { :cmuser => username  }
   @auth[ :cmpasswd ] = password if password 
   instance_eval( &block )
end