Class: F00px::Request::Callback

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#urlObject (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

#infoObject



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