Class: Bellows::HTTP
- Inherits:
-
Object
- Object
- Bellows::HTTP
- Defined in:
- lib/bellows/http.rb
Constant Summary collapse
- MULTI_PART_BOUNDARY =
"jtZ!pZ1973um"
- @@http =
nil
- @@auth_user =
nil
- @@auth_password =
nil
Class Method Summary collapse
- .delete(url_path) ⇒ Object
- .file_upload(url_path, file_data = {}, post_data = {}) ⇒ Object
- .get(url_path) ⇒ Object
- .init_connection ⇒ Object
- .post(url_path, post_data) ⇒ Object
- .put(url_path, put_data) ⇒ Object
Class Method Details
.delete(url_path) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/bellows/http.rb', line 189 def self.delete(url_path) init_connection if @@http.nil? req = Net::HTTP::Delete.new(url_path) req.basic_auth @@auth_user, @@auth_password if @@auth_user and @@auth_password response = @@http.request(req) case response when Net::HTTPSuccess return response.body when Net::HTTPRedirection return response['location'] else response.error! end end |
.file_upload(url_path, file_data = {}, post_data = {}) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/bellows/http.rb', line 92 def self.file_upload(url_path, file_data={}, post_data={}) init_connection if @@http.nil? req = Net::HTTP::Post.new(url_path) post_arr=[] post_data.each_pair do |key, value| post_arr << "--#{MULTI_PART_BOUNDARY}\r\n" post_arr << "Content-Disposition: form-data; name=\"#{key}\"\r\n" post_arr << "\r\n" post_arr << value post_arr << "\r\n" end file_data.each_pair do |name, file| post_arr << "--#{MULTI_PART_BOUNDARY}\r\n" post_arr << "Content-Disposition: form-data; name=\"#{name}\"; filename=\"#{File.basename(file)}\"\r\n" post_arr << "Content-Type: text/plain\r\n" post_arr << "\r\n" post_arr << File.read(file) post_arr << "\r\n--#{MULTI_PART_BOUNDARY}--\r\n" end post_arr << "--#{MULTI_PART_BOUNDARY}--\r\n\r\n" req.body=post_arr.join req.basic_auth @@auth_user, @@auth_password if @@auth_user and @@auth_password req["Content-Type"] = "multipart/form-data, boundary=#{MULTI_PART_BOUNDARY}" response = @@http.request(req) case response when Net::HTTPSuccess return response.body else puts response.body response.error! end end |
.get(url_path) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/bellows/http.rb', line 176 def self.get(url_path) init_connection if @@http.nil? req = Net::HTTP::Get.new(url_path) req.basic_auth @@auth_user, @@auth_password if @@auth_user and @@auth_password response = @@http.request(req) case response when Net::HTTPSuccess return response.body else response.error! end end |
.init_connection ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/bellows/http.rb', line 55 def self.init_connection configs=Util.load_configs base_url = configs["smokestack_url"] @@auth_user = configs["smokestack_username"] @@auth_password = configs["smokestack_password"] ssl_key = configs["ssl_key"] ssl_cert = configs["ssl_cert"] ssl_ca_cert = configs["ssl_ca_cert"] url=URI.parse(base_url) @@http = Net::HTTP.new(url.host,url.port) if base_url =~ /^https/ @@http.use_ssl = true if ssl_ca_cert then @@http.verify_mode = OpenSSL::SSL::VERIFY_PEER else @@http.verify_mode = OpenSSL::SSL::VERIFY_NONE end if ssl_key then pkey_data=IO.read(ssl_key) if pkey_data =~ /^-----BEGIN RSA PRIVATE KEY-----/ @@http.key=OpenSSL::PKey::RSA.new(pkey_data) else @@http.key=OpenSSL::PKey::DSA.new(pkey_data) end end @@http.cert=OpenSSL::X509::Certificate.new(IO.read(ssl_cert)) if ssl_cert @@http.ca_file=ssl_ca_cert if ssl_ca_cert end end |
.post(url_path, post_data) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/bellows/http.rb', line 130 def self.post(url_path, post_data) init_connection if @@http.nil? req = Net::HTTP::Post.new(url_path) if post_data.kind_of?(String) then req.body=post_data elsif post_data.kind_of?(Hash) then req.form_data=post_data else raise "Invalid post data type." end req.basic_auth @@auth_user, @@auth_password if @@auth_user and @@auth_password response = @@http.request(req) case response when Net::HTTPSuccess return response.body when Net::HTTPRedirection return response['location'] else puts response.body response.error! end end |
.put(url_path, put_data) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/bellows/http.rb', line 153 def self.put(url_path, put_data) init_connection if @@http.nil? req = Net::HTTP::Put.new(url_path) if put_data.kind_of?(String) then req.body=put_data elsif put_data.kind_of?(Hash) then req.form_data=put_data else raise "Invalid put data type." end req.basic_auth @@auth_user, @@auth_password if @@auth_user and @@auth_password response = @@http.request(req) case response when Net::HTTPSuccess return response.body when Net::HTTPRedirection return response['location'] else puts response.body response.error! end end |