Class: Fetch::Request
- Inherits:
-
Object
- Object
- Fetch::Request
- Defined in:
- lib/fetch/request.rb
Overview
A request to be completed with Typhoeus.
Instance Attribute Summary collapse
-
#body ⇒ Object
The post body to be sent with the request.
-
#follow_redirects ⇒ Object
Whether to follow redirects.
-
#headers ⇒ Object
The headers to be sent with the request.
-
#method ⇒ Object
The method to be used for the request.
-
#timeout ⇒ Object
The timeout for the request.
-
#url ⇒ Object
The URL to be requested.
Instance Method Summary collapse
-
#after_process(&block) ⇒ Object
Sets a callback to be run after each process.
-
#after_process! ⇒ Object
Runs the after process callback.
-
#before_process(&block) ⇒ Object
Sets a callback to be run before each process.
-
#before_process! ⇒ Object
Runs the before process callback.
-
#body_string ⇒ Object
The post body represented as a string.
-
#error(&block) ⇒ Object
Sets the callback to be run if the processing fails due to an exception.
-
#error!(exception) ⇒ Object
Runs the error callback.
-
#failed!(code, url) ⇒ Object
Runs the failure callback.
-
#failure(&block) ⇒ Object
Sets the callback to be run if a request fails.
-
#initialize(*args) ⇒ Request
constructor
Initializes the request and sets properties to the values defined in
options
. -
#parse(&block) ⇒ Object
Sets a parse callback to be run on the body returned from the request.
-
#parse!(body) ⇒ Object
Runs the before process callback.
-
#process(&block) ⇒ Object
Sets the callback to be run when the request completes.
-
#process!(body, url, effective_url) ⇒ Object
Runs the process callback.
-
#user_agent ⇒ Object
The user agent being sent with the request.
-
#user_agent=(value) ⇒ Object
Sets the user agent to be sent with the request.
Constructor Details
#initialize(*args) ⇒ Request
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/fetch/request.rb', line 16 def initialize(*args) = args.pop if args.last.is_a?(Hash) if args.any? self.url = args.first end if .each { |key, value| send("#{key}=", value) } end end |
Instance Attribute Details
#body ⇒ Object
The post body to be sent with the request.
49 50 51 |
# File 'lib/fetch/request.rb', line 49 def body @body ||= {} end |
#follow_redirects ⇒ Object
Whether to follow redirects. Default: true
32 33 34 35 |
# File 'lib/fetch/request.rb', line 32 def follow_redirects return @follow_redirects if defined?(@follow_redirects) @follow_redirects = true end |
#headers ⇒ Object
The headers to be sent with the request.
72 73 74 75 76 |
# File 'lib/fetch/request.rb', line 72 def headers @headers ||= { "User-Agent" => Fetch.config.user_agent } end |
#method ⇒ Object
The method to be used for the request.
41 42 43 |
# File 'lib/fetch/request.rb', line 41 def method @method || :get end |
#timeout ⇒ Object
The timeout for the request. Default: Taken from Fetch.config.timeout
63 64 65 66 |
# File 'lib/fetch/request.rb', line 63 def timeout return @timeout if defined?(@timeout) Fetch.config.timeout end |
#url ⇒ Object
The URL to be requested.
29 30 31 |
# File 'lib/fetch/request.rb', line 29 def url @url end |
Instance Method Details
#after_process(&block) ⇒ Object
Sets a callback to be run after each process.
133 134 135 136 |
# File 'lib/fetch/request.rb', line 133 def after_process(&block) raise "You must supply a block to #{self.class.name}#after_process" unless block @after_process_callback = block end |
#after_process! ⇒ Object
Runs the after process callback.
139 140 141 |
# File 'lib/fetch/request.rb', line 139 def after_process! @after_process_callback.call if @after_process_callback end |
#before_process(&block) ⇒ Object
Sets a callback to be run before each process.
92 93 94 95 |
# File 'lib/fetch/request.rb', line 92 def before_process(&block) raise "You must supply a block to #{self.class.name}#before_process" unless block @before_process_callback = block end |
#before_process! ⇒ Object
Runs the before process callback.
98 99 100 |
# File 'lib/fetch/request.rb', line 98 def before_process! @before_process_callback.call if @before_process_callback end |
#body_string ⇒ Object
The post body represented as a string.
57 58 59 |
# File 'lib/fetch/request.rb', line 57 def body_string body.map { |k, v| "#{CGI::escape(k.to_s)}=#{CGI::escape(v.to_s)}" }.join("&") end |
#error(&block) ⇒ Object
Sets the callback to be run if the processing fails due to an exception.
155 156 157 158 |
# File 'lib/fetch/request.rb', line 155 def error(&block) raise "You must supply a block to #{self.class.name}#error" unless block @error_callback = block end |
#error!(exception) ⇒ Object
Runs the error callback. Raises the exception given in exception
if an error callback isn’t defined.
162 163 164 165 166 167 168 |
# File 'lib/fetch/request.rb', line 162 def error!(exception) if @error_callback @error_callback.call(exception) else raise exception end end |
#failed!(code, url) ⇒ Object
Runs the failure callback.
150 151 152 |
# File 'lib/fetch/request.rb', line 150 def failed!(code, url) @failure_callback.call(code, url) if @failure_callback end |
#failure(&block) ⇒ Object
Sets the callback to be run if a request fails.
144 145 146 147 |
# File 'lib/fetch/request.rb', line 144 def failure(&block) raise "You must supply a block to #{self.class.name}#failure" unless block @failure_callback = block end |
#parse(&block) ⇒ Object
Sets a parse callback to be run on the body returned from the request. It is run before processing and its result send to process.
104 105 106 107 |
# File 'lib/fetch/request.rb', line 104 def parse(&block) raise "You must supply a block to #{self.class.name}#parse" unless block @parse_callback = block end |
#parse!(body) ⇒ Object
Runs the before process callback.
110 111 112 113 |
# File 'lib/fetch/request.rb', line 110 def parse!(body) return body unless @parse_callback @parse_callback.call(body) end |
#process(&block) ⇒ Object
Sets the callback to be run when the request completes.
116 117 118 119 |
# File 'lib/fetch/request.rb', line 116 def process(&block) raise "You must supply a block to #{self.class.name}#process" unless block @process_callback = block end |
#process!(body, url, effective_url) ⇒ Object
Runs the process callback. If it fails with an exception, it will send the exception to the error callback.
123 124 125 126 127 128 129 130 |
# File 'lib/fetch/request.rb', line 123 def process!(body, url, effective_url) before_process! body = parse!(body) @process_callback.call(body, url, effective_url) if @process_callback after_process! rescue => e error!(e) end |
#user_agent ⇒ Object
The user agent being sent with the request.
82 83 84 |
# File 'lib/fetch/request.rb', line 82 def user_agent headers["User-Agent"] end |
#user_agent=(value) ⇒ Object
Sets the user agent to be sent with the request.
87 88 89 |
# File 'lib/fetch/request.rb', line 87 def user_agent=(value) headers.merge! "User-Agent" => value end |