Class: Restfolia::HTTP::Behaviour::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/restfolia/http/behaviour.rb

Overview

Public: Responsible to store behaviours. See #behaviours for details.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore

Public: Creates a Store.



57
58
59
60
# File 'lib/restfolia/http/behaviour.rb', line 57

def initialize
  self.clear
  @helpers = Helpers.new
end

Instance Attribute Details

#helpersObject (readonly)

Returns Restfolia::HTTP::Behaviour::Helpers instance.



54
55
56
# File 'lib/restfolia/http/behaviour.rb', line 54

def helpers
  @helpers
end

Instance Method Details

#behaviours(&block) ⇒ Object

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.



87
88
89
90
# File 'lib/restfolia/http/behaviour.rb', line 87

def behaviours(&block)
  self.instance_eval(&block)
  nil
end

#clearObject

Public: clear all defined behaviours. Returns nothing.



64
65
66
67
68
# File 'lib/restfolia/http/behaviour.rb', line 64

def clear
  @behaviours = {}
  @behaviours_range = {}
  nil
end

#default_behaviour(http_response) ⇒ Object

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



124
125
126
127
# 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

#execute(http_response) ⇒ Object

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.



134
135
136
137
138
139
140
141
# 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

#on(code, &block) ⇒ Object

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.



99
100
101
102
103
104
105
106
# 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