Class: Typhoeus::Request

Inherits:
Object
  • Object
show all
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

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) ** :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



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

def initialize(url, options = {})
  @method           = options[:method] || :get
  @params           = options[:params]
  @body             = options[:body]
  @timeout          = options[:timeout]
  @connect_timeout  = options[:connect_timeout]
  @headers          = options[:headers] || {}
  @user_agent       = options[:user_agent] || Typhoeus::USER_AGENT
  @cache_timeout    = 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]
  @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]
  @verbose          = options[:verbose]
  @username         = options[:username]
  @password         = options[:password]
  @auth_method      = options[: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_methodObject

Returns the value of attribute auth_method.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def auth_method
  @auth_method
end

#bodyObject

Returns the value of attribute body.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def body
  @body
end

#cache_timeoutObject

Returns the value of attribute cache_timeout.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def cache_timeout
  @cache_timeout
end

#connect_timeoutObject

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_verificationObject

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_locationObject

Returns the value of attribute follow_location.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def follow_location
  @follow_location
end

#headersObject



107
108
109
110
# File 'lib/typhoeus/request.rb', line 107

def headers
  @headers["User-Agent"] = @user_agent
  @headers
end

#max_redirectsObject

Returns the value of attribute max_redirects.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def max_redirects
  @max_redirects
end

#methodObject

Returns the value of attribute method.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def method
  @method
end

#paramsObject

Returns the value of attribute params.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def params
  @params
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def password
  @password
end

#proxyObject

Returns the value of attribute proxy.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def proxy
  @proxy
end

#proxy_auth_methodObject

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_passwordObject

Returns the value of attribute proxy_password.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def proxy_password
  @proxy_password
end

#proxy_typeObject

Returns the value of attribute proxy_type.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def proxy_type
  @proxy_type
end

#proxy_usernameObject

Returns the value of attribute proxy_username.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def proxy_username
  @proxy_username
end

#responseObject

Returns the value of attribute response.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def response
  @response
end

#ssl_cacertObject

Returns the value of attribute ssl_cacert.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def ssl_cacert
  @ssl_cacert
end

#ssl_capathObject

Returns the value of attribute ssl_capath.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def ssl_capath
  @ssl_capath
end

#ssl_certObject

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_typeObject

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_keyObject

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_passwordObject

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_typeObject

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

#timeoutObject

Returns the value of attribute timeout.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def timeout
  @timeout
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/typhoeus/request.rb', line 5

def url
  @url
end

#user_agentObject

Returns the value of attribute user_agent.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def user_agent
  @user_agent
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/typhoeus/request.rb', line 7

def username
  @username
end

#verboseObject

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



202
203
204
# File 'lib/typhoeus/request.rb', line 202

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

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



190
191
192
# File 'lib/typhoeus/request.rb', line 190

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

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



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

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

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



194
195
196
# File 'lib/typhoeus/request.rb', line 194

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

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



198
199
200
# File 'lib/typhoeus/request.rb', line 198

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

.run(url, params) ⇒ Object



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

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



134
135
136
# File 'lib/typhoeus/request.rb', line 134

def after_complete(&block)
  @after_complete = block
end

#after_complete=(proc) ⇒ Object



138
139
140
# File 'lib/typhoeus/request.rb', line 138

def after_complete=(proc)
  @after_complete = proc
end

#cache_keyObject



179
180
181
# File 'lib/typhoeus/request.rb', line 179

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

#call_after_completeObject



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

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

#call_handlersObject



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

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

#handled_responseObject



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

def handled_response
  @handled_response || response
end

#handled_response=(val) ⇒ Object



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

def handled_response=(val)
  @handled_response = val
end

#hostObject



93
94
95
96
97
98
99
100
101
# File 'lib/typhoeus/request.rb', line 93

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



103
104
105
# File 'lib/typhoeus/request.rb', line 103

def host_domain
  @parsed_uri.host
end

#inspectObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/typhoeus/request.rb', line 161

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)


89
90
91
# File 'lib/typhoeus/request.rb', line 89

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

#on_complete(&block) ⇒ Object



126
127
128
# File 'lib/typhoeus/request.rb', line 126

def on_complete(&block)
  @on_complete = block
end

#on_complete=(proc) ⇒ Object



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

def on_complete=(proc)
  @on_complete = proc
end

#params_stringObject



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/typhoeus/request.rb', line 112

def params_string
  params.keys.sort { |a, b| a.to_s <=> b.to_s }.collect do |k|
    value = params[k]
    if value.is_a? Hash
      value.keys.collect {|sk| Typhoeus::Utils.escape("#{k}[#{sk}]") + "=" + Typhoeus::Utils.escape(value[sk].to_s)}
    elsif value.is_a? Array
      key = Typhoeus::Utils.escape(k.to_s)
      value.collect { |v| "#{key}[]=#{Typhoeus::Utils.escape(v.to_s)}" }.join('&')
    else
      "#{Typhoeus::Utils.escape(k.to_s)}=#{Typhoeus::Utils.escape(params[k].to_s)}"
    end
  end.flatten.join("&")
end