Class: RestClient::Request
- Inherits:
-
Object
- Object
- RestClient::Request
- Defined in:
- lib/rest_client.rb,
lib/request_errors.rb
Overview
backwards compatibility
Constant Summary collapse
- Redirect =
RestClient::Redirect
RestClient::Unauthorized
- RequestFailed =
RestClient::RequestFailed
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #decode(content_encoding, body) ⇒ Object
- #default_headers ⇒ Object
- #display_log(msg) ⇒ Object
- #execute ⇒ Object
- #execute_inner ⇒ Object
-
#initialize(args) ⇒ Request
constructor
A new instance of Request.
- #make_headers(user_headers) ⇒ Object
- #net_http_class(method) ⇒ Object
- #parse_url(url) ⇒ Object
- #parse_url_with_auth(url) ⇒ Object
- #process_payload(p = nil, parent_key = nil) ⇒ Object
- #process_result(res) ⇒ Object
- #request_log ⇒ Object
- #response_log(res) ⇒ Object
- #setup_credentials(req) ⇒ Object
- #transmit(uri, req, payload) ⇒ Object
Constructor Details
#initialize(args) ⇒ Request
Returns a new instance of Request.
84 85 86 87 88 89 90 91 |
# File 'lib/rest_client.rb', line 84 def initialize(args) @method = args[:method] or raise ArgumentError, "must pass :method" @url = args[:url] or raise ArgumentError, "must pass :url" @headers = args[:headers] || {} @payload = process_payload(args[:payload]) @user = args[:user] @password = args[:password] end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
78 79 80 |
# File 'lib/rest_client.rb', line 78 def headers @headers end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
78 79 80 |
# File 'lib/rest_client.rb', line 78 def method @method end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
78 79 80 |
# File 'lib/rest_client.rb', line 78 def password @password end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
78 79 80 |
# File 'lib/rest_client.rb', line 78 def payload @payload end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
78 79 80 |
# File 'lib/rest_client.rb', line 78 def url @url end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
78 79 80 |
# File 'lib/rest_client.rb', line 78 def user @user end |
Class Method Details
.execute(args) ⇒ Object
80 81 82 |
# File 'lib/rest_client.rb', line 80 def self.execute(args) new(args).execute end |
Instance Method Details
#decode(content_encoding, body) ⇒ Object
192 193 194 195 196 197 198 199 200 |
# File 'lib/rest_client.rb', line 192 def decode(content_encoding, body) if content_encoding == 'gzip' Zlib::GzipReader.new(StringIO.new(body)).read elsif content_encoding == 'deflate' Zlib::Inflate.new.inflate(body) else body end end |
#default_headers ⇒ Object
226 227 228 |
# File 'lib/rest_client.rb', line 226 def default_headers { :accept => 'application/xml', :accept_encoding => 'gzip, deflate' } end |
#display_log(msg) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/rest_client.rb', line 214 def display_log(msg) return unless log_to = RestClient.log if log_to == 'stdout' STDOUT.puts msg elsif log_to == 'stderr' STDERR.puts msg else File.open(log_to, 'a') { |f| f.puts msg } end end |
#execute ⇒ Object
93 94 95 96 97 98 |
# File 'lib/rest_client.rb', line 93 def execute execute_inner rescue Redirect => e @url = e.url execute end |
#execute_inner ⇒ Object
100 101 102 103 |
# File 'lib/rest_client.rb', line 100 def execute_inner uri = parse_url_with_auth(url) transmit uri, net_http_class(method).new(uri.request_uri, make_headers(headers)), payload end |
#make_headers(user_headers) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/rest_client.rb', line 105 def make_headers(user_headers) final = {} merged = default_headers.merge(user_headers) merged.keys.each do |key| final[key.to_s.gsub(/_/, '-').capitalize] = merged[key] end final end |
#net_http_class(method) ⇒ Object
114 115 116 |
# File 'lib/rest_client.rb', line 114 def net_http_class(method) Net::HTTP.const_get(method.to_s.capitalize) end |
#parse_url(url) ⇒ Object
118 119 120 121 |
# File 'lib/rest_client.rb', line 118 def parse_url(url) url = "http://#{url}" unless url.match(/^http/) URI.parse(url) end |
#parse_url_with_auth(url) ⇒ Object
123 124 125 126 127 128 |
# File 'lib/rest_client.rb', line 123 def parse_url_with_auth(url) uri = parse_url(url) @user = uri.user if uri.user @password = uri.password if uri.password uri end |
#process_payload(p = nil, parent_key = nil) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rest_client.rb', line 130 def process_payload(p=nil, parent_key=nil) unless p.is_a?(Hash) p else @headers[:content_type] ||= 'application/x-www-form-urlencoded' p.keys.map do |k| key = parent_key ? "#{parent_key}[#{k}]" : k if p[k].is_a? Hash process_payload(p[k], key) else value = URI.escape(p[k].to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) "#{key}=#{value}" end end.join("&") end end |
#process_result(res) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/rest_client.rb', line 170 def process_result(res) if %w(200 201 202).include? res.code decode res['content-encoding'], res.body elsif %w(301 302 303).include? res.code url = res.header['Location'] if url !~ /^http/ uri = URI.parse(@url) uri.path = "/#{url}".squeeze('/') url = uri.to_s end raise Redirect, url elsif res.code == "401" raise Unauthorized elsif res.code == "404" raise ResourceNotFound else raise RequestFailed, res end end |
#request_log ⇒ Object
202 203 204 205 206 207 208 |
# File 'lib/rest_client.rb', line 202 def request_log out = [] out << "RestClient.#{method} #{url.inspect}" out << (payload.size > 100 ? "(#{payload.size} byte payload)".inspect : payload.inspect) if payload out << headers.inspect.gsub(/^\{/, '').gsub(/\}$/, '') unless headers.empty? out.join(', ') end |
#response_log(res) ⇒ Object
210 211 212 |
# File 'lib/rest_client.rb', line 210 def response_log(res) "# => #{res.code} #{res.class.to_s.gsub(/^Net::HTTP/, '')} | #{(res['Content-type'] || '').gsub(/;.*$/, '')} #{res.body.size} bytes" end |
#setup_credentials(req) ⇒ Object
166 167 168 |
# File 'lib/rest_client.rb', line 166 def setup_credentials(req) req.basic_auth(user, password) if user end |
#transmit(uri, req, payload) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/rest_client.rb', line 147 def transmit(uri, req, payload) setup_credentials(req) net = Net::HTTP.new(uri.host, uri.port) net.use_ssl = uri.is_a?(URI::HTTPS) display_log request_log net.start do |http| res = http.request(req, payload || "") display_log response_log(res) process_result res end rescue EOFError raise RestClient::ServerBrokeConnection rescue Timeout::Error raise RestClient::RequestTimeout end |