Class: Respect::Rails::ResponseSchemaSet

Inherits:
Object
  • Object
show all
Defined in:
lib/respect/rails/response_schema_set.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ResponseSchemaSet) initialize(controller_name, action_name)

Initialize a new ResponseSchemaSet object for the given controller's action and collect response' schema from their respective file.



27
28
29
30
31
# File 'lib/respect/rails/response_schema_set.rb', line 27

def initialize(controller_name, action_name)
  @controller_name = controller_name
  @action_name = action_name
  @set = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method_name, *arguments, &block)



35
36
37
# File 'lib/respect/rails/response_schema_set.rb', line 35

def method_missing(method_name, *arguments, &block)
  define_response(method_name, *arguments, &block)
end

Instance Attribute Details

- (Object) action_name (readonly)

Returns the value of attribute action_name



33
34
35
# File 'lib/respect/rails/response_schema_set.rb', line 33

def action_name
  @action_name
end

- (Object) controller_name (readonly)

Returns the value of attribute controller_name



33
34
35
# File 'lib/respect/rails/response_schema_set.rb', line 33

def controller_name
  @controller_name
end

Class Method Details

+ (Object) symbolize_status(status)

FIXME(Nicolas Despres): Move me to another module/class.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/respect/rails/response_schema_set.rb', line 7

def symbolize_status(status)
  case status
  when Symbol
    status
  when String
    if status =~ /^\d+$/
      symbolize_status(status.to_i)
    else
      status.to_sym
    end
  when Numeric
    ResponseSchema.symbolize_http_status(status.to_i)
  else
    raise ArgumentError, "cannot normalize status '#{status}:#{status.class}'"
  end
end

Instance Method Details

- (Object) <<(response_schema)



45
46
47
# File 'lib/respect/rails/response_schema_set.rb', line 45

def <<(response_schema)
  @set[response_schema.http_status] = response_schema
end

- (Object) [](http_status)



39
40
41
# File 'lib/respect/rails/response_schema_set.rb', line 39

def [](http_status)
  @set[http_status]
end

- (Object) define_response(status, *arguments, &block)



53
54
55
56
# File 'lib/respect/rails/response_schema_set.rb', line 53

def define_response(status, *arguments, &block)
  status = self.class.symbolize_status(status)
  self << ResponseSchema.define(status, *arguments, &block)
end

- (Object) is(status, *arguments, &block)



49
50
51
# File 'lib/respect/rails/response_schema_set.rb', line 49

def is(status, *arguments, &block)
  define_response(status, *arguments, &block)
end