Class: F00px::Request::Callback
- Inherits:
-
Object
- Object
- F00px::Request::Callback
- Defined in:
- lib/f00px/request/callback.rb
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#complete(&block) ⇒ Object
(also: #on_complete)
Register a complete callback.
-
#error(&block) ⇒ Object
(also: #on_error)
Registers an error callback.
- #info ⇒ Object
-
#initialize(method, url, params = {}) ⇒ Callback
constructor
A new instance of Callback.
- #perform(response) ⇒ Object
-
#success(&block) ⇒ Object
(also: #on_success)
Registers a success callback.
Constructor Details
#initialize(method, url, params = {}) ⇒ Callback
Returns a new instance of Callback.
7 8 9 |
# File 'lib/f00px/request/callback.rb', line 7 def initialize(method, url, params={}) @method, @url, @params = [method, url, params] end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
5 6 7 |
# File 'lib/f00px/request/callback.rb', line 5 def method @method end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
5 6 7 |
# File 'lib/f00px/request/callback.rb', line 5 def params @params end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
5 6 7 |
# File 'lib/f00px/request/callback.rb', line 5 def url @url end |
Instance Method Details
#complete(&block) ⇒ Object Also known as: on_complete
Register a complete callback. This will be triggered after success or error on all requests.
43 44 45 46 |
# File 'lib/f00px/request/callback.rb', line 43 def complete(&block) @on_complete = block self end |
#error(&block) ⇒ Object Also known as: on_error
Registers an error callback. The callback will be executed if any 400 or 500 HTTP status is returned.
PxApi.get('/photos').
error{|res| puts "HTTP Status: #{res.status}" }
29 30 31 32 |
# File 'lib/f00px/request/callback.rb', line 29 def error(&block) @on_error = block self end |
#info ⇒ Object
11 12 13 |
# File 'lib/f00px/request/callback.rb', line 11 def info [method, url, params] end |
#perform(response) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/f00px/request/callback.rb', line 15 def perform(response) if response.status != 200 on_error!(response) else on_success!(response) end on_complete!(response) end |
#success(&block) ⇒ Object Also known as: on_success
Registers a success callback. This will be triggered if the status code is not 4xx or 5xx.
36 37 38 39 |
# File 'lib/f00px/request/callback.rb', line 36 def success(&block) @on_success = block self end |