Module: CodeMutiny::HttpClient
- Defined in:
- lib/httpclient/httpclient.rb
Instance Attribute Summary collapse
-
#obtained_body ⇒ Object
readonly
Returns the value of attribute obtained_body.
-
#obtained_code ⇒ Object
readonly
Returns the value of attribute obtained_code.
-
#obtained_headers ⇒ Object
readonly
Returns the value of attribute obtained_headers.
Instance Method Summary collapse
- #admin(key, &block) ⇒ Object
- #anonymous(&block) ⇒ Object
- #connect(&block) ⇒ Object
- #create(path, form = nil, &block) ⇒ Object (also: #update)
- #debug(&block) ⇒ Object
- #debug_request(http_method, path, headers, form = nil) ⇒ Object
- #debug_response(code, headers, body) ⇒ Object
- #handle_response(res, block = nil) ⇒ Object
- #host(host) ⇒ Object
- #remove(path, &block) ⇒ Object
- #retrieve(path, &block) ⇒ Object
- #user(username, password = nil, &block) ⇒ Object
Instance Attribute Details
#obtained_body ⇒ Object (readonly)
Returns the value of attribute obtained_body.
164 165 166 |
# File 'lib/httpclient/httpclient.rb', line 164 def obtained_body @obtained_body end |
#obtained_code ⇒ Object (readonly)
Returns the value of attribute obtained_code.
164 165 166 |
# File 'lib/httpclient/httpclient.rb', line 164 def obtained_code @obtained_code end |
#obtained_headers ⇒ Object (readonly)
Returns the value of attribute obtained_headers.
164 165 166 |
# File 'lib/httpclient/httpclient.rb', line 164 def obtained_headers @obtained_headers end |
Instance Method Details
#admin(key, &block) ⇒ Object
166 167 168 169 170 171 172 173 |
# File 'lib/httpclient/httpclient.rb', line 166 def admin( key, &block ) if key == nil anonymous( &block ) else @auth = { :cmuser => key } instance_eval( &block ) end end |
#anonymous(&block) ⇒ Object
181 182 183 184 |
# File 'lib/httpclient/httpclient.rb', line 181 def anonymous( &block ) @auth = { :cmuser => 'anonymous' } instance_eval( &block ) end |
#connect(&block) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/httpclient/httpclient.rb', line 64 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
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/httpclient/httpclient.rb', line 125 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
77 78 79 80 81 |
# File 'lib/httpclient/httpclient.rb', line 77 def debug( &block ) @debug = true instance_eval( &block ) @debug = false end |
#debug_request(http_method, path, headers, form = nil) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/httpclient/httpclient.rb', line 83 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
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/httpclient/httpclient.rb', line 96 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
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/httpclient/httpclient.rb', line 146 def handle_response( res, block = nil ) code = res.code.to_i headers = res.to_hash body = res.body debug_response( code, headers, body ) case res[ 'Content-Type' ] when 'text/x-json' then body = JSON.parse body when 'text/xml' then 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
60 61 62 |
# File 'lib/httpclient/httpclient.rb', line 60 def host( host ) @@host = host end |
#remove(path, &block) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/httpclient/httpclient.rb', line 116 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
108 109 110 111 112 113 114 |
# File 'lib/httpclient/httpclient.rb', line 108 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
175 176 177 178 179 |
# File 'lib/httpclient/httpclient.rb', line 175 def user( username, password = nil, &block ) @auth = { :cmuser => username } @auth[ :cmpasswd ] = password if password instance_eval( &block ) end |