Class: Typhoeus::Request
- Inherits:
-
Object
- Object
- Typhoeus::Request
- Defined in:
- lib/typhoeus/request.rb
Constant Summary collapse
- ACCESSOR_OPTIONS =
[ :method, :params, :body, :headers, :connect_timeout, :timeout, :user_agent, :response, :cache_timeout, :follow_location, :max_redirects, :proxy, :proxy_username, :proxy_password, :disable_ssl_peer_verification, :disable_ssl_host_verification, :interface, :ssl_cert, :ssl_cert_type, :ssl_key, :ssl_key_type, :ssl_key_password, :ssl_cacert, :ssl_capath, :ssl_version, :verbose, :username, :password, :auth_method, :user_agent, :proxy_auth_method, :proxy_type ]
- LOCALHOST_ALIASES =
%w[ localhost 127.0.0.1 0.0.0.0 ]
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
- .delete(url, params = {}) ⇒ Object
- .get(url, params = {}) ⇒ Object
- .head(url, params = {}) ⇒ Object
- .post(url, params = {}) ⇒ Object
- .put(url, params = {}) ⇒ Object
- .run(url, params) ⇒ Object
Instance Method Summary collapse
- #after_complete(&block) ⇒ Object
- #after_complete=(proc) ⇒ Object
- #cache_key ⇒ Object
- #call_after_complete ⇒ Object
- #call_handlers ⇒ Object
- #handled_response ⇒ Object
- #handled_response=(val) ⇒ Object
- #host ⇒ Object
- #host_domain ⇒ Object
-
#initialize(url, options = {}) ⇒ Request
constructor
Initialize a new Request.
- #inspect ⇒ Object
- #localhost? ⇒ Boolean
- #on_complete(&block) ⇒ Object
- #on_complete=(proc) ⇒ Object
- #params_string ⇒ Object
- #user_agent ⇒ Object
- #user_agent=(value) ⇒ Object
Constructor Details
#initialize(url, options = {}) ⇒ Request
Initialize a new Request
Options:
-
url
: Endpoint (URL) of the request -
options
: A hash containing options among :
** :method
: :get (default) / :post / :put ** :params
: params as a Hash ** :body
** :timeout
: timeout (ms) ** :interface
: interface or ip address (string) ** :connect_timeout
: connect timeout (ms) ** :headers
: headers as Hash ** :cache_timeout
: cache timeout (ms) ** :follow_location ** :max_redirects ** :proxy ** :disable_ssl_peer_verification ** :disable_ssl_host_verification ** :ssl_cert ** :ssl_cert_type ** :ssl_key ** :ssl_key_type ** :ssl_key_password ** :ssl_cacert ** :ssl_capath ** :verbose ** :username ** :password ** :auth_method ** :user_agent
: user agent (string) - DEPRECATED
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 |
# File 'lib/typhoeus/request.rb', line 74 def initialize(url, = {}) @method = [:method] || :get @params = [:params] @body = [:body] @timeout = safe_to_i([:timeout]) @connect_timeout = safe_to_i([:connect_timeout]) @interface = [:interface] @headers = [:headers] || {} if .has_key?(:user_agent) self.user_agent = [:user_agent] end @cache_timeout = safe_to_i([:cache_timeout]) @follow_location = [:follow_location] @max_redirects = [:max_redirects] @proxy = [:proxy] @proxy_type = [:proxy_type] @proxy_username = [:proxy_username] @proxy_password = [:proxy_password] @proxy_auth_method = [:proxy_auth_method] @disable_ssl_peer_verification = [:disable_ssl_peer_verification] @disable_ssl_host_verification = [:disable_ssl_host_verification] @ssl_cert = [:ssl_cert] @ssl_cert_type = [:ssl_cert_type] @ssl_key = [:ssl_key] @ssl_key_type = [:ssl_key_type] @ssl_key_password = [:ssl_key_password] @ssl_cacert = [:ssl_cacert] @ssl_capath = [:ssl_capath] @ssl_version = [:ssl_version] @verbose = [:verbose] @username = [:username] @password = [:password] @auth_method = [:auth_method] if @method == :post @url = url else @url = @params ? "#{url}?#{params_string}" : url end @parsed_uri = URI.parse(@url) @on_complete = nil @after_complete = nil @handled_response = nil end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
40 41 42 |
# File 'lib/typhoeus/request.rb', line 40 def url @url end |
Class Method Details
.delete(url, params = {}) ⇒ Object
233 234 235 |
# File 'lib/typhoeus/request.rb', line 233 def self.delete(url, params = {}) run(url, params.merge(:method => :delete)) end |
.get(url, params = {}) ⇒ Object
221 222 223 |
# File 'lib/typhoeus/request.rb', line 221 def self.get(url, params = {}) run(url, params.merge(:method => :get)) end |
.head(url, params = {}) ⇒ Object
237 238 239 |
# File 'lib/typhoeus/request.rb', line 237 def self.head(url, params = {}) run(url, params.merge(:method => :head)) end |
.post(url, params = {}) ⇒ Object
225 226 227 |
# File 'lib/typhoeus/request.rb', line 225 def self.post(url, params = {}) run(url, params.merge(:method => :post)) end |
.put(url, params = {}) ⇒ Object
229 230 231 |
# File 'lib/typhoeus/request.rb', line 229 def self.put(url, params = {}) run(url, params.merge(:method => :put)) end |
Instance Method Details
#after_complete(&block) ⇒ Object
165 166 167 |
# File 'lib/typhoeus/request.rb', line 165 def after_complete(&block) @after_complete = block end |
#after_complete=(proc) ⇒ Object
169 170 171 |
# File 'lib/typhoeus/request.rb', line 169 def after_complete=(proc) @after_complete = proc end |
#cache_key ⇒ Object
210 211 212 |
# File 'lib/typhoeus/request.rb', line 210 def cache_key Digest::SHA1.hexdigest(url) end |
#call_after_complete ⇒ Object
180 181 182 |
# File 'lib/typhoeus/request.rb', line 180 def call_after_complete @after_complete.call(@handled_response) if @after_complete end |
#call_handlers ⇒ Object
173 174 175 176 177 178 |
# File 'lib/typhoeus/request.rb', line 173 def call_handlers if @on_complete @handled_response = @on_complete.call(response) call_after_complete end end |
#handled_response ⇒ Object
188 189 190 |
# File 'lib/typhoeus/request.rb', line 188 def handled_response @handled_response || response end |
#handled_response=(val) ⇒ Object
184 185 186 |
# File 'lib/typhoeus/request.rb', line 184 def handled_response=(val) @handled_response = val end |
#host ⇒ Object
138 139 140 141 142 143 144 145 146 |
# File 'lib/typhoeus/request.rb', line 138 def host slash_location = @url.index('/', 8) if slash_location @url.slice(0, slash_location) else query_string_location = @url.index('?') return query_string_location ? @url.slice(0, query_string_location) : @url end end |
#host_domain ⇒ Object
148 149 150 |
# File 'lib/typhoeus/request.rb', line 148 def host_domain @parsed_uri.host end |
#inspect ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/typhoeus/request.rb', line 192 def inspect result = ":method => #{self.method.inspect},\n" << "\t:url => #{URI.parse(self.url).to_s}" if self.body and !self.body.empty? result << ",\n\t:body => #{self.body.inspect}" end if self.params and !self.params.empty? result << ",\n\t:params => #{self.params.inspect}" end if self.headers and !self.headers.empty? result << ",\n\t:headers => #{self.headers.inspect}" end result end |
#localhost? ⇒ Boolean
125 126 127 |
# File 'lib/typhoeus/request.rb', line 125 def localhost? LOCALHOST_ALIASES.include?(@parsed_uri.host) end |
#on_complete(&block) ⇒ Object
157 158 159 |
# File 'lib/typhoeus/request.rb', line 157 def on_complete(&block) @on_complete = block end |
#on_complete=(proc) ⇒ Object
161 162 163 |
# File 'lib/typhoeus/request.rb', line 161 def on_complete=(proc) @on_complete = proc end |
#params_string ⇒ Object
152 153 154 155 |
# File 'lib/typhoeus/request.rb', line 152 def params_string traversal = Typhoeus::Utils.traverse_params_hash(params) Typhoeus::Utils.traversal_to_param_string(traversal) end |
#user_agent ⇒ Object
129 130 131 |
# File 'lib/typhoeus/request.rb', line 129 def user_agent headers['User-Agent'] end |
#user_agent=(value) ⇒ Object
133 134 135 136 |
# File 'lib/typhoeus/request.rb', line 133 def user_agent=(value) puts "DEPRECATED: Typhoeus::Request#user_agent=(value). This will be removed in a later version." headers['User-Agent'] = value end |