Class: Typhoeus::Request
- Inherits:
-
Object
- Object
- Typhoeus::Request
- Defined in:
- lib/typhoeus/request.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#cache_timeout ⇒ Object
Returns the value of attribute cache_timeout.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#response ⇒ Object
Returns the value of attribute response.
-
#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.
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
-
#initialize(url, options = {}) ⇒ Request
constructor
A new instance of Request.
- #on_complete(&block) ⇒ Object
- #on_complete=(proc) ⇒ Object
- #params_string ⇒ Object
Constructor Details
#initialize(url, options = {}) ⇒ Request
Returns a new instance of Request.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/typhoeus/request.rb', line 6 def initialize(url, = {}) @method = [:method] || :get @params = [:params] @body = [:body] @timeout = [:timeout] @headers = [:headers] || {} @user_agent = [:user_agent] || Typhoeus::USER_AGENT @cache_timeout = [:cache_timeout] @url = @params ? "#{url}?#{params_string}" : url @on_complete = nil @after_complete = nil @handled_response = nil end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
3 4 5 |
# File 'lib/typhoeus/request.rb', line 3 def body @body end |
#cache_timeout ⇒ Object
Returns the value of attribute cache_timeout.
3 4 5 |
# File 'lib/typhoeus/request.rb', line 3 def cache_timeout @cache_timeout end |
#headers ⇒ Object
Returns the value of attribute headers.
3 4 5 |
# File 'lib/typhoeus/request.rb', line 3 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
3 4 5 |
# File 'lib/typhoeus/request.rb', line 3 def method @method end |
#params ⇒ Object
Returns the value of attribute params.
3 4 5 |
# File 'lib/typhoeus/request.rb', line 3 def params @params end |
#response ⇒ Object
Returns the value of attribute response.
3 4 5 |
# File 'lib/typhoeus/request.rb', line 3 def response @response end |
#timeout ⇒ Object
Returns the value of attribute timeout.
3 4 5 |
# File 'lib/typhoeus/request.rb', line 3 def timeout @timeout end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
4 5 6 |
# File 'lib/typhoeus/request.rb', line 4 def url @url end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
3 4 5 |
# File 'lib/typhoeus/request.rb', line 3 def user_agent @user_agent end |
Instance Method Details
#after_complete(&block) ⇒ Object
57 58 59 |
# File 'lib/typhoeus/request.rb', line 57 def after_complete(&block) @after_complete = block end |
#after_complete=(proc) ⇒ Object
61 62 63 |
# File 'lib/typhoeus/request.rb', line 61 def after_complete=(proc) @after_complete = proc end |
#cache_key ⇒ Object
84 85 86 |
# File 'lib/typhoeus/request.rb', line 84 def cache_key Digest::SHA1.hexdigest(url) end |
#call_after_complete ⇒ Object
72 73 74 |
# File 'lib/typhoeus/request.rb', line 72 def call_after_complete @after_complete.call(@handled_response) if @after_complete end |
#call_handlers ⇒ Object
65 66 67 68 69 70 |
# File 'lib/typhoeus/request.rb', line 65 def call_handlers if @on_complete @handled_response = @on_complete.call(response) call_after_complete end end |
#handled_response ⇒ Object
80 81 82 |
# File 'lib/typhoeus/request.rb', line 80 def handled_response @handled_response || response end |
#handled_response=(val) ⇒ Object
76 77 78 |
# File 'lib/typhoeus/request.rb', line 76 def handled_response=(val) @handled_response = val end |
#host ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/typhoeus/request.rb', line 20 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
49 50 51 |
# File 'lib/typhoeus/request.rb', line 49 def on_complete(&block) @on_complete = block end |
#on_complete=(proc) ⇒ Object
53 54 55 |
# File 'lib/typhoeus/request.rb', line 53 def on_complete=(proc) @on_complete = proc end |
#params_string ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/typhoeus/request.rb', line 35 def params_string params.keys.sort.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 |