Class: ResourceKit::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/resource_kit/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
# File 'lib/resource_kit/action.rb', line 5

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

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



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

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



44
45
46
47
# File 'lib/resource_kit/action.rb', line 44

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

#handler(*response_codes, &block) ⇒ Object



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

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?(Fixnum)
      handlers[code] = block
    end
  end
end

#handlersObject



29
30
31
# File 'lib/resource_kit/action.rb', line 29

def handlers
  @handlers ||= {}
end

#hooksObject



49
50
51
# File 'lib/resource_kit/action.rb', line 49

def hooks
  @hooks ||= {}
end

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



17
18
19
20
21
22
# File 'lib/resource_kit/action.rb', line 17

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



24
25
26
27
# File 'lib/resource_kit/action.rb', line 24

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

#verb(v = nil) ⇒ Object



12
13
14
15
# File 'lib/resource_kit/action.rb', line 12

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