Class: ResourceSet::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/resource_set/action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, verb = nil, path = nil) ⇒ Action

Returns a new instance of Action.



5
6
7
8
9
10
11
# File 'lib/resource_set/action.rb', line 5

def initialize(name, verb = nil, path = nil)
  @name = name
  @verb = (verb && verb.downcase.to_sym) || :get
  @path = path
  @query_keys = []
  @body_handler = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/resource_set/action.rb', line 3

def name
  @name
end

Instance Method Details

#before_request(method_name = nil, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/resource_set/action.rb', line 54

def before_request(method_name = nil, &block)
  hooks[:before] ||= []

  if block_given?
    hooks[:before] << block
  else
    raise "Must include a method name" unless method_name
    hooks[:before] << method_name
  end

  nil
end

#body(&block) ⇒ Object



45
46
47
48
# File 'lib/resource_set/action.rb', line 45

def body(&block)
  @body_handler = block if block_given?
  @body_handler
end

#handler(*response_codes, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/resource_set/action.rb', line 34

def handler(*response_codes, &block)
  if response_codes.empty?
    handlers[:any] = block
  else
    response_codes.each do |code|
      code = StatusCodeMapper.code_for(code) unless code.is_a?(Integer)
      handlers[code] = block
    end
  end
end

#handlersObject



30
31
32
# File 'lib/resource_set/action.rb', line 30

def handlers
  @handlers ||= {}
end

#hooksObject



50
51
52
# File 'lib/resource_set/action.rb', line 50

def hooks
  @hooks ||= {}
end

#path(path = nil, &block) ⇒ Object



18
19
20
21
22
23
# File 'lib/resource_set/action.rb', line 18

def path(path = nil, &block)
  raise "You must pass either a block or a string for paths" if path and block_given?
  @path = path if path
  @path = block if block_given?
  @path
end

#query_keys(*keys) ⇒ Object



25
26
27
28
# File 'lib/resource_set/action.rb', line 25

def query_keys(*keys)
  return @query_keys if keys.empty?
  @query_keys += keys
end

#verb(v = nil) ⇒ Object



13
14
15
16
# File 'lib/resource_set/action.rb', line 13

def verb(v = nil)
  @verb = v if v
  @verb
end