/
paths.rb
65 lines (62 loc) · 1.98 KB
/
paths.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module Her
module Model
module Paths
# Return a path based on the collection path and a resource data
#
# @example
# class User
# include Her::Model
# collection_path "/utilisateurs"
# end
#
# User.find(1) # Fetched via GET /utilisateurs/1
def request_path # {{{
self.class.build_request_path(@data)
end # }}}
module ClassMethods
# Defines a custom collection path for the resource
#
# @example
# class User
# include Her::Model
# collection_path "/users"
# end
def collection_path(path=nil) # {{{
return @her_collection_path unless path
@her_resource_path = "#{path}/:id"
@her_collection_path = path
end # }}}
# Defines a custom resource path for the resource
#
# @example
# class User
# include Her::Model
# resource_path "/users/:id"
# end
def resource_path(path=nil) # {{{
return @her_resource_path unless path
@her_resource_path = path
end # }}}
# Return a custom path based on the collection path and variable parameters
#
# @example
# class User
# include Her::Model
# collection_path "/utilisateurs"
# end
#
# User.all # Fetched via GET /utilisateurs
def build_request_path(path=nil, parameters={}) # {{{
unless path.is_a?(String)
parameters = path || {}
path = parameters.include?(:id) ? @her_resource_path : @her_collection_path
end
path.gsub(/:([\w_]+)/) do
# Look for :key or :_key, otherwise raise an exception
parameters[$1.to_sym] || parameters["_#{$1}".to_sym] || raise(Her::Errors::PathError.new("Missing :_#{$1} parameter to build the request path (#{path})."))
end
end # }}}
end
end
end
end