class Restfolia::HTTP::Behaviour::Store
Public: Responsible to store behaviours. See behaviours
for details.
Attributes
Returns Restfolia::HTTP::Behaviour::Helpers
instance.
Public Class Methods
Public: Creates a Store
.
# File lib/restfolia/http/behaviour.rb, line 57 def initialize self.clear! @helpers = Helpers.new end
Public Instance Methods
Public: It’s a nice way to define configurations for your behaves using a block.
block - Required block to customize your behaves. Below are the methods available inside block:
#on - See #on
Examples
store = Restfolia::HTTP::Behaviour::Store.new store.behaviours do on(200) { '200 behaviour' } on([201, 204]) { 'behaviour for 201 and 204 codes' } on(300...400) { '3xx range' } end
Returns nothing.
# File lib/restfolia/http/behaviour.rb, line 87 def behaviours(&block) self.instance_eval(&block) nil end
Public: clear all defined behaviours. Returns nothing.
# File lib/restfolia/http/behaviour.rb, line 64 def clear! @behaviours = {} @behaviours_range = {} nil end
Public: Method called by execute
in case of ‘not found’ http code.
http_response - Net::HTTPResponse object.
Examples
store = Restfolia::HTTP::Behaviour::Store.new store.behaviours do on(200) { '200 ok' } end http_resp = OpenStruct.new(:code => 201) #simulate response 201 store.execute(http_resp) # => #<Restfolia::ResponseError "Undefined behaviour for 201" ...>
Returns nothing. Raises Restfolia::ResponseError
# File lib/restfolia/http/behaviour.rb, line 124 def default_behaviour(http_response) msg = "Undefined behaviour for #{http_response.code}" raise Restfolia::ResponseError.new(msg, caller, http_response) end
Public: Look for defined behaviour, based on HTTP
code. If behaviour not found, call default_behaviour
.
http_response - Net::HTTPResponse. Returns Result from Proc behaviour or default_behaviour
method.
# File lib/restfolia/http/behaviour.rb, line 134 def execute(http_response) code = http_response.code.to_i if (behaviour = find(code)) behaviour.call(http_response) else default_behaviour(http_response) end end
Public: Add behaviour on this store. See behaviours
for examples.
code - Integer or any object that respond to include? block - Required block with behaviour for this code.
Returns nothing.
# File lib/restfolia/http/behaviour.rb, line 99 def on(code, &block) if code.is_a?(Integer) @behaviours[code] = block elsif code.respond_to?(:include?) @behaviours_range[code] = block end nil end
Protected Instance Methods
Internal: Search for defined behaviour.
code - Integer or object that respond to include?
Returns nil or Proc
# File lib/restfolia/http/behaviour.rb, line 150 def find(code) behaviour = @behaviours[code] if behaviour.nil? _, behaviour = @behaviours_range.detect do |range, proc| range.include?(code) end end behaviour end