Class: Typhoeus::Request
- Inherits:
-
Object
- Object
- Typhoeus::Request
- Defined in:
- lib/typhoeus/request.rb
Constant Summary collapse
- LOCALHOST_ALIASES =
%w[ localhost 127.0.0.1 0.0.0.0 ]
Instance Attribute Summary collapse
-
#auth_method ⇒ Object
Returns the value of attribute auth_method.
-
#body ⇒ Object
Returns the value of attribute body.
-
#cache_timeout ⇒ Object
Returns the value of attribute cache_timeout.
-
#connect_timeout ⇒ Object
Returns the value of attribute connect_timeout.
-
#disable_ssl_peer_verification ⇒ Object
Returns the value of attribute disable_ssl_peer_verification.
-
#follow_location ⇒ Object
Returns the value of attribute follow_location.
- #headers ⇒ Object
-
#interface ⇒ Object
Returns the value of attribute interface.
-
#max_redirects ⇒ Object
Returns the value of attribute max_redirects.
-
#method ⇒ Object
Returns the value of attribute method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#password ⇒ Object
Returns the value of attribute password.
-
#proxy ⇒ Object
Returns the value of attribute proxy.
-
#proxy_auth_method ⇒ Object
Returns the value of attribute proxy_auth_method.
-
#proxy_password ⇒ Object
Returns the value of attribute proxy_password.
-
#proxy_type ⇒ Object
Returns the value of attribute proxy_type.
-
#proxy_username ⇒ Object
Returns the value of attribute proxy_username.
-
#response ⇒ Object
Returns the value of attribute response.
-
#ssl_cacert ⇒ Object
Returns the value of attribute ssl_cacert.
-
#ssl_capath ⇒ Object
Returns the value of attribute ssl_capath.
-
#ssl_cert ⇒ Object
Returns the value of attribute ssl_cert.
-
#ssl_cert_type ⇒ Object
Returns the value of attribute ssl_cert_type.
-
#ssl_key ⇒ Object
Returns the value of attribute ssl_key.
-
#ssl_key_password ⇒ Object
Returns the value of attribute ssl_key_password.
-
#ssl_key_type ⇒ Object
Returns the value of attribute ssl_key_type.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
-
#username ⇒ Object
Returns the value of attribute username.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
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
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 ** :user_agent
: user agent (string) ** :cache_timeout
: cache timeout (ms) ** :follow_location ** :max_redirects ** :proxy ** :disable_ssl_peer_verification ** :ssl_cert ** :ssl_cert_type ** :ssl_key ** :ssl_key_type ** :ssl_key_password ** :ssl_cacert ** :ssl_capath ** :verbose ** :username ** :password ** +:auth_method
46 47 48 49 50 51 52 53 54 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 |
# File 'lib/typhoeus/request.rb', line 46 def initialize(url, = {}) @method = [:method] || :get @params = [:params] @body = [:body] @timeout = [:timeout] @connect_timeout = [:connect_timeout] @interface = [:interface] @headers = [:headers] || {} @user_agent = [:user_agent] || Typhoeus::USER_AGENT @cache_timeout = [: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] @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] @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
#auth_method ⇒ Object
Returns the value of attribute auth_method.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def auth_method @auth_method end |
#body ⇒ Object
Returns the value of attribute body.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def body @body end |
#cache_timeout ⇒ Object
Returns the value of attribute cache_timeout.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def cache_timeout @cache_timeout end |
#connect_timeout ⇒ Object
Returns the value of attribute connect_timeout.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def connect_timeout @connect_timeout end |
#disable_ssl_peer_verification ⇒ Object
Returns the value of attribute disable_ssl_peer_verification.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def disable_ssl_peer_verification @disable_ssl_peer_verification end |
#follow_location ⇒ Object
Returns the value of attribute follow_location.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def follow_location @follow_location end |
#headers ⇒ Object
109 110 111 112 |
# File 'lib/typhoeus/request.rb', line 109 def headers @headers["User-Agent"] = @user_agent @headers end |
#interface ⇒ Object
Returns the value of attribute interface.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def interface @interface end |
#max_redirects ⇒ Object
Returns the value of attribute max_redirects.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def max_redirects @max_redirects end |
#method ⇒ Object
Returns the value of attribute method.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def method @method end |
#params ⇒ Object
Returns the value of attribute params.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def params @params end |
#password ⇒ Object
Returns the value of attribute password.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def password @password end |
#proxy ⇒ Object
Returns the value of attribute proxy.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def proxy @proxy end |
#proxy_auth_method ⇒ Object
Returns the value of attribute proxy_auth_method.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def proxy_auth_method @proxy_auth_method end |
#proxy_password ⇒ Object
Returns the value of attribute proxy_password.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def proxy_password @proxy_password end |
#proxy_type ⇒ Object
Returns the value of attribute proxy_type.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def proxy_type @proxy_type end |
#proxy_username ⇒ Object
Returns the value of attribute proxy_username.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def proxy_username @proxy_username end |
#response ⇒ Object
Returns the value of attribute response.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def response @response end |
#ssl_cacert ⇒ Object
Returns the value of attribute ssl_cacert.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def ssl_cacert @ssl_cacert end |
#ssl_capath ⇒ Object
Returns the value of attribute ssl_capath.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def ssl_capath @ssl_capath end |
#ssl_cert ⇒ Object
Returns the value of attribute ssl_cert.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def ssl_cert @ssl_cert end |
#ssl_cert_type ⇒ Object
Returns the value of attribute ssl_cert_type.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def ssl_cert_type @ssl_cert_type end |
#ssl_key ⇒ Object
Returns the value of attribute ssl_key.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def ssl_key @ssl_key end |
#ssl_key_password ⇒ Object
Returns the value of attribute ssl_key_password.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def ssl_key_password @ssl_key_password end |
#ssl_key_type ⇒ Object
Returns the value of attribute ssl_key_type.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def ssl_key_type @ssl_key_type end |
#timeout ⇒ Object
Returns the value of attribute timeout.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def timeout @timeout end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
5 6 7 |
# File 'lib/typhoeus/request.rb', line 5 def url @url end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def user_agent @user_agent end |
#username ⇒ Object
Returns the value of attribute username.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def username @username end |
#verbose ⇒ Object
Returns the value of attribute verbose.
7 8 9 |
# File 'lib/typhoeus/request.rb', line 7 def verbose @verbose end |
Class Method Details
.delete(url, params = {}) ⇒ Object
195 196 197 |
# File 'lib/typhoeus/request.rb', line 195 def self.delete(url, params = {}) run(url, params.merge(:method => :delete)) end |
.get(url, params = {}) ⇒ Object
183 184 185 |
# File 'lib/typhoeus/request.rb', line 183 def self.get(url, params = {}) run(url, params.merge(:method => :get)) end |
.head(url, params = {}) ⇒ Object
199 200 201 |
# File 'lib/typhoeus/request.rb', line 199 def self.head(url, params = {}) run(url, params.merge(:method => :head)) end |
.post(url, params = {}) ⇒ Object
187 188 189 |
# File 'lib/typhoeus/request.rb', line 187 def self.post(url, params = {}) run(url, params.merge(:method => :post)) end |
.put(url, params = {}) ⇒ Object
191 192 193 |
# File 'lib/typhoeus/request.rb', line 191 def self.put(url, params = {}) run(url, params.merge(:method => :put)) end |
Instance Method Details
#after_complete(&block) ⇒ Object
127 128 129 |
# File 'lib/typhoeus/request.rb', line 127 def after_complete(&block) @after_complete = block end |
#after_complete=(proc) ⇒ Object
131 132 133 |
# File 'lib/typhoeus/request.rb', line 131 def after_complete=(proc) @after_complete = proc end |
#cache_key ⇒ Object
172 173 174 |
# File 'lib/typhoeus/request.rb', line 172 def cache_key Digest::SHA1.hexdigest(url) end |
#call_after_complete ⇒ Object
142 143 144 |
# File 'lib/typhoeus/request.rb', line 142 def call_after_complete @after_complete.call(@handled_response) if @after_complete end |
#call_handlers ⇒ Object
135 136 137 138 139 140 |
# File 'lib/typhoeus/request.rb', line 135 def call_handlers if @on_complete @handled_response = @on_complete.call(response) call_after_complete end end |
#handled_response ⇒ Object
150 151 152 |
# File 'lib/typhoeus/request.rb', line 150 def handled_response @handled_response || response end |
#handled_response=(val) ⇒ Object
146 147 148 |
# File 'lib/typhoeus/request.rb', line 146 def handled_response=(val) @handled_response = val end |
#host ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/typhoeus/request.rb', line 95 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
105 106 107 |
# File 'lib/typhoeus/request.rb', line 105 def host_domain @parsed_uri.host end |
#inspect ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/typhoeus/request.rb', line 154 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
91 92 93 |
# File 'lib/typhoeus/request.rb', line 91 def localhost? LOCALHOST_ALIASES.include?(@parsed_uri.host) end |
#on_complete(&block) ⇒ Object
119 120 121 |
# File 'lib/typhoeus/request.rb', line 119 def on_complete(&block) @on_complete = block end |
#on_complete=(proc) ⇒ Object
123 124 125 |
# File 'lib/typhoeus/request.rb', line 123 def on_complete=(proc) @on_complete = proc end |
#params_string ⇒ Object
114 115 116 117 |
# File 'lib/typhoeus/request.rb', line 114 def params_string traversal = Typhoeus::Utils.traverse_params_hash(params) Typhoeus::Utils.traversal_to_param_string(traversal) end |