Class: Rubapi::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/rubapi/router.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.routesObject

Returns the value of attribute routes.



4
5
6
# File 'lib/rubapi/router.rb', line 4

def routes
  @routes
end

Class Method Details

.blank_resource(resource) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rubapi/router.rb', line 71

def blank_resource(resource)
  { 
    name: resource, 
    actions: {
      get: [],
      post: [],
      put: [],
      delete:[]
    }, 
    nested: [],
    controller: nil,
    pagination: nil
  }
end

.controller(klass) ⇒ Object



108
109
110
# File 'lib/rubapi/router.rb', line 108

def controller(klass)
  @current_resource[:controller] = klass
end

.extract_params(uri_params, params) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubapi/router.rb', line 40

def extract_params(uri_params, params)
  result = {}
  if params.any?
    for i in 1..(params.size)
      key = params[i-1]
      key[0]=''
      result[key] = uri_params[i*2]
    end
  end

  result
end

.fetch(path, verb) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubapi/router.rb', line 20

def fetch(path, verb)
  paths = path.split("/")
  resources = paths.select{|pa| /([a-z]+)/.match pa }.map(&:to_sym)
  resources = find_resource(resources)

  return nil if resources.nil?

  resources[:actions][verb].each do |re|
    resource_path = re[:path].split("/")
    if paths.count == resource_path.count
      request = re.clone
      request[:controller] = resources[:controller]
      request[:pagination] = resources[:pagination]
      return request
    end
  end

  false
end

.find_resource(resources) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rubapi/router.rb', line 54

def find_resource(resources)
  self.routes ||= []

  resource = resources.shift

  root = self.routes.select{|route| route[:name] == resource}.first
  while(resources.any?)
    resource = resources.shift
    if root[:nested].any?
      root = root[:nested].select {|route| route[:name] == resource}.first
    end
  end

  root
end

.pagination(options) ⇒ Object



112
113
114
# File 'lib/rubapi/router.rb', line 112

def pagination(options)
  @current_resource[:pagination] = options
end

.resource(resource) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rubapi/router.rb', line 6

def resource(resource)
  self.routes ||= []
  if @nested_resource
    @current_resource[:nested] << rest_resource(resource)
  else
    self.routes << rest_resource(resource)
  end
  @current_resource = find_resource([resource])

  @nested_resource = true
  yield if block_given?
  @nested_resource = false
end

.rest_resource(resource) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rubapi/router.rb', line 86

def rest_resource(resource)
  path = "/#{resource}"
  if @nested_resource
    path = "/#{@current_resource[:name]}/:#{@current_resource[:name]}_id/#{resource}"
  end
  blank_resource(resource).tap do |rest|
    rest[:actions][:get]    << { method: :index  , path: "#{path}"     }
    rest[:actions][:get]    << { method: :show   , path: "#{path}/:#{resource}_id" }
    rest[:actions][:post]   << { method: :create , path: "#{path}"     }
    rest[:actions][:put]    << { method: :update , path: "#{path}/:#{resource}_id" }
    rest[:actions][:delete] << { method: :delete , path: "#{path}/:#{resource}_id" }
  end
end

.without(*actions) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/rubapi/router.rb', line 100

def without(*actions)
  actions.first.each do |verb, actions|
    @current_resource[:actions]
      .select{|key,actions| verb == key }[verb]
      .delete_if{|action| actions.include?(action[:method]) }
  end
end