Class: Typhoeus::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/typhoeus/request.rb

Constant Summary collapse

ACCESSOR_OPTIONS =
[
  :method,
  :params,
  :body,
  :headers,
  :cache_key_basis,
  :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,
  :proxy_auth_method,
  :proxy_type
]
LOCALHOST_ALIASES =
%w[ localhost 127.0.0.1 0.0.0.0 ]

Class Method Summary collapse

Instance Method Summary collapse

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



72
73
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
# File 'lib/typhoeus/request.rb', line 72

def initialize(url, options = {})
  @url = url
  @method           = options[:method] || :get
  @params           = options[:params]
  @body             = options[:body]
  @timeout          = safe_to_i(options[:timeout])
  @connect_timeout  = safe_to_i(options[:connect_timeout])
  @interface        = options[:interface]
  @headers          = options[:headers] || {}

  @cache_timeout    = safe_to_i(options[:cache_timeout])
  @follow_location  = options[:follow_location]
  @max_redirects    = options[:max_redirects]
  @proxy            = options[:proxy]
  @proxy_type       = options[:proxy_type]
  @proxy_username   = options[:proxy_username]
  @proxy_password   = options[:proxy_password]
  @proxy_auth_method = options[:proxy_auth_method]
  @disable_ssl_peer_verification = options[:disable_ssl_peer_verification]
  @disable_ssl_host_verification = options[:disable_ssl_host_verification]
  @ssl_cert         = options[:ssl_cert]
  @ssl_cert_type    = options[:ssl_cert_type]
  @ssl_key          = options[:ssl_key]
  @ssl_key_type     = options[:ssl_key_type]
  @ssl_key_password = options[:ssl_key_password]
  @ssl_cacert       = options[:ssl_cacert]
  @ssl_capath       = options[:ssl_capath]
  @ssl_version      = options[:ssl_version]
  @verbose          = options[:verbose]
  @username         = options[:username]
  @password         = options[:password]
  @auth_method      = options[:auth_method]

  @on_complete      = nil
  @after_complete   = nil
  @handled_response = nil
end

Class Method Details

.delete(url, params = {}) ⇒ Object



230
231
232
# File 'lib/typhoeus/request.rb', line 230

def self.delete(url, params = {})
  run(url, params.merge(:method => :delete))
end

.get(url, params = {}) ⇒ Object



218
219
220
# File 'lib/typhoeus/request.rb', line 218

def self.get(url, params = {})
  run(url, params.merge(:method => :get))
end

.head(url, params = {}) ⇒ Object



234
235
236
# File 'lib/typhoeus/request.rb', line 234

def self.head(url, params = {})
  run(url, params.merge(:method => :head))
end

.post(url, params = {}) ⇒ Object



222
223
224
# File 'lib/typhoeus/request.rb', line 222

def self.post(url, params = {})
  run(url, params.merge(:method => :post))
end

.put(url, params = {}) ⇒ Object



226
227
228
# File 'lib/typhoeus/request.rb', line 226

def self.put(url, params = {})
  run(url, params.merge(:method => :put))
end

.run(url, params) ⇒ Object



211
212
213
214
215
216
# File 'lib/typhoeus/request.rb', line 211

def self.run(url, params)
  r = new(url, params)
  Typhoeus::Hydra.hydra.queue r
  Typhoeus::Hydra.hydra.run
  r.response
end

Instance Method Details

#after_complete(&block) ⇒ Object



162
163
164
# File 'lib/typhoeus/request.rb', line 162

def after_complete(&block)
  @after_complete = block
end

#after_complete=(proc) ⇒ Object



166
167
168
# File 'lib/typhoeus/request.rb', line 166

def after_complete=(proc)
  @after_complete = proc
end

#cache_keyObject



207
208
209
# File 'lib/typhoeus/request.rb', line 207

def cache_key
  Digest::SHA1.hexdigest(cache_key_basis || url)
end

#call_after_completeObject



177
178
179
# File 'lib/typhoeus/request.rb', line 177

def call_after_complete
  @after_complete.call(@handled_response) if @after_complete
end

#call_handlersObject



170
171
172
173
174
175
# File 'lib/typhoeus/request.rb', line 170

def call_handlers
  if @on_complete
    @handled_response = @on_complete.call(response)
    call_after_complete
  end
end

#handled_responseObject



185
186
187
# File 'lib/typhoeus/request.rb', line 185

def handled_response
  @handled_response || response
end

#handled_response=(val) ⇒ Object



181
182
183
# File 'lib/typhoeus/request.rb', line 181

def handled_response=(val)
  @handled_response = val
end

#hostObject



134
135
136
137
138
139
140
141
142
# File 'lib/typhoeus/request.rb', line 134

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_domainObject



144
145
146
# File 'lib/typhoeus/request.rb', line 144

def host_domain
  parsed_uri.host
end

#inspectObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/typhoeus/request.rb', line 189

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

Returns:

  • (Boolean)


112
113
114
# File 'lib/typhoeus/request.rb', line 112

def localhost?
  LOCALHOST_ALIASES.include?(parsed_uri.host)
end

#on_complete(&block) ⇒ Object



154
155
156
# File 'lib/typhoeus/request.rb', line 154

def on_complete(&block)
  @on_complete = block
end

#on_complete=(proc) ⇒ Object



158
159
160
# File 'lib/typhoeus/request.rb', line 158

def on_complete=(proc)
  @on_complete = proc
end

#params_stringObject



148
149
150
151
152
# File 'lib/typhoeus/request.rb', line 148

def params_string
  return nil unless params
  traversal = Typhoeus::Utils.traverse_params_hash(params)
  Typhoeus::Utils.traversal_to_param_string(traversal)
end

#parsed_uriObject



130
131
132
# File 'lib/typhoeus/request.rb', line 130

def parsed_uri
  @parsed_uri ||= URI.parse(@url)
end

#urlObject



120
121
122
123
124
125
126
127
128
# File 'lib/typhoeus/request.rb', line 120

def url
  if [:post, :put].include?(@method)
    @url
  else
    url = "#{@url}?#{params_string}"
    url += "&#{URI.escape(@body)}" if @body
    url.gsub("?&", "?").gsub(/\?$/, '')
  end
end

#user_agentObject



116
117
118
# File 'lib/typhoeus/request.rb', line 116

def user_agent
  headers['User-Agent']
end