Class: Typhoeus::Request

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

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/typhoeus/request.rb', line 33

def initialize(url, options = {})
  @method           = options[:method] || :get
  @params           = options[:params]
  @body             = options[:body]
  @timeout          = options[: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]
  @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]

  if @method == :post
    @url = url
  else
    @url = @params ? "#{url}?#{params_string}" : url
  end
  @on_complete      = nil
  @after_complete   = nil
  @handled_response = nil
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def body
  @body
end

#cache_timeoutObject

Returns the value of attribute cache_timeout.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def cache_timeout
  @cache_timeout
end

#disable_ssl_peer_verificationObject

Returns the value of attribute disable_ssl_peer_verification.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def disable_ssl_peer_verification
  @disable_ssl_peer_verification
end

#follow_locationObject

Returns the value of attribute follow_location.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def follow_location
  @follow_location
end

#headersObject

Returns the value of attribute headers.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def headers
  @headers
end

#max_redirectsObject

Returns the value of attribute max_redirects.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def max_redirects
  @max_redirects
end

#methodObject

Returns the value of attribute method.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def method
  @method
end

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def params
  @params
end

#proxyObject

Returns the value of attribute proxy.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def proxy
  @proxy
end

#responseObject

Returns the value of attribute response.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def response
  @response
end

#ssl_cacertObject

Returns the value of attribute ssl_cacert.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def ssl_cacert
  @ssl_cacert
end

#ssl_capathObject

Returns the value of attribute ssl_capath.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def ssl_capath
  @ssl_capath
end

#ssl_certObject

Returns the value of attribute ssl_cert.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def ssl_cert
  @ssl_cert
end

#ssl_cert_typeObject

Returns the value of attribute ssl_cert_type.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def ssl_cert_type
  @ssl_cert_type
end

#ssl_keyObject

Returns the value of attribute ssl_key.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def ssl_key
  @ssl_key
end

#ssl_key_passwordObject

Returns the value of attribute ssl_key_password.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def ssl_key_password
  @ssl_key_password
end

#ssl_key_typeObject

Returns the value of attribute ssl_key_type.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def ssl_key_type
  @ssl_key_type
end

#timeoutObject

Returns the value of attribute timeout.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def timeout
  @timeout
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

#user_agentObject

Returns the value of attribute user_agent.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def user_agent
  @user_agent
end

#verboseObject

Returns the value of attribute verbose.



3
4
5
# File 'lib/typhoeus/request.rb', line 3

def verbose
  @verbose
end

Class Method Details

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



151
152
153
# File 'lib/typhoeus/request.rb', line 151

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

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



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

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

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



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

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

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



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

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

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



147
148
149
# File 'lib/typhoeus/request.rb', line 147

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

.run(url, params) ⇒ Object



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

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



101
102
103
# File 'lib/typhoeus/request.rb', line 101

def after_complete(&block)
  @after_complete = block
end

#after_complete=(proc) ⇒ Object



105
106
107
# File 'lib/typhoeus/request.rb', line 105

def after_complete=(proc)
  @after_complete = proc
end

#cache_keyObject



128
129
130
# File 'lib/typhoeus/request.rb', line 128

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

#call_after_completeObject



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

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

#call_handlersObject



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

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

#handled_responseObject



124
125
126
# File 'lib/typhoeus/request.rb', line 124

def handled_response
  @handled_response || response
end

#handled_response=(val) ⇒ Object



120
121
122
# File 'lib/typhoeus/request.rb', line 120

def handled_response=(val)
  @handled_response = val
end

#hostObject



64
65
66
67
68
69
70
71
72
# File 'lib/typhoeus/request.rb', line 64

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

#on_complete(&block) ⇒ Object



93
94
95
# File 'lib/typhoeus/request.rb', line 93

def on_complete(&block)
  @on_complete = block
end

#on_complete=(proc) ⇒ Object



97
98
99
# File 'lib/typhoeus/request.rb', line 97

def on_complete=(proc)
  @on_complete = proc
end

#params_stringObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/typhoeus/request.rb', line 79

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| Rack::Utils.escape("#{k}[#{sk}]") + "=" + Rack::Utils.escape(value[sk].to_s)}
    elsif value.is_a? Array
      key = Rack::Utils.escape(k.to_s)
      value.collect { |v| "#{key}=#{Rack::Utils.escape(v.to_s)}" }.join('&')
    else
      "#{Rack::Utils.escape(k.to_s)}=#{Rack::Utils.escape(params[k].to_s)}"
    end
  end.flatten.join("&")
end