Module: HalApi::Representer::UriMethods
- Extended by:
- ActiveSupport::Concern
- Included in:
- HalApi::Representer
- Defined in:
- lib/hal_api/representer/uri_methods.rb
Overview
expects underlying model to have filename, class, and id attributes
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
117
118
119
120
121
122
123
124
|
# File 'lib/hal_api/representer/uri_methods.rb', line 117
def method_missing(method_name, *args, &block)
if method_name.to_s.ends_with?('_path_template')
original_method_name = method_name[0..-10]
template_named_path(original_method_name, *args)
else
super(method_name, *args, &block)
end
end
|
Instance Method Details
#alternate_url(*path) ⇒ Object
77
78
79
|
# File 'lib/hal_api/representer/uri_methods.rb', line 77
def alternate_url(*path)
"https://#{self.class.alternate_host}/#{path.map(&:to_s).join('/')}"
end
|
#becomes_represented_class(rep) ⇒ Object
70
71
72
73
74
75
|
# File 'lib/hal_api/representer/uri_methods.rb', line 70
def becomes_represented_class(rep)
return rep unless rep.respond_to?(:becomes, true)
klass = rep.try(:item_class) || rep.class.try(:base_class)
klass && (klass != rep.class) ? rep.becomes(klass) : rep
end
|
#find_model_name(klass) ⇒ Object
98
99
100
|
# File 'lib/hal_api/representer/uri_methods.rb', line 98
def find_model_name(klass)
klass.try(:name) || klass.ancestors.detect{|c| c.try(:name) }.name
end
|
#joined_names(args) ⇒ Object
87
88
89
90
|
# File 'lib/hal_api/representer/uri_methods.rb', line 87
def joined_names(args)
(Array(args.map { |arg| model_uri_part_to_string(arg) }) +
model_uri_suffix(args)).flatten.compact.join('/')
end
|
#model_path(represented) ⇒ Object
47
48
49
50
51
|
# File 'lib/hal_api/representer/uri_methods.rb', line 47
def model_path(represented)
rep = becomes_represented_class(represented)
class_path = rep.class.name.underscore.pluralize
"#{class_path}/#{represented.id}"
end
|
#model_uri(*args) ⇒ Object
Also known as:
profile_url
81
82
83
|
# File 'lib/hal_api/representer/uri_methods.rb', line 81
def model_uri(*args)
"http://#{self.class.profile_host}/model/#{joined_names(args)}"
end
|
#model_uri_part_to_string(part) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/hal_api/representer/uri_methods.rb', line 102
def model_uri_part_to_string(part)
if part.is_a?(String) || part.is_a?(Symbol)
part.to_s.dasherize
else
klass = part.is_a?(Class) ? part : (part.try(:item_class) || part.class)
if klass.respond_to?(:base_class, true) && !klass.superclass.name.demodulize.starts_with?('Base')
parent = klass.superclass.name.underscore.dasherize
child = klass.name.underscore.gsub(/_#{parent}$/, '').dasherize
[parent, child]
else
klass.name.underscore.dasherize
end
end
end
|
#model_uri_suffix(args) ⇒ Object
92
93
94
95
96
|
# File 'lib/hal_api/representer/uri_methods.rb', line 92
def model_uri_suffix(args)
represented = args.last
klass = represented.try(:item_decorator) || self.class
find_model_name(klass).deconstantize.underscore.dasherize.split('/')[1..-1] || []
end
|
#self_url(represented) ⇒ Object
53
54
55
56
|
# File 'lib/hal_api/representer/uri_methods.rb', line 53
def self_url(represented)
rep = becomes_represented_class(represented)
polymorphic_path([:api, rep])
end
|
#template_named_path(named_path, options) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/hal_api/representer/uri_methods.rb', line 126
def template_named_path(named_path, options)
replace_options = options.keys.inject({}) do |s, k|
s[k] = "_#{k.upcase}_REPLACE_"
s
end
path = send(named_path, replace_options)
replace_options.keys.each do |k|
path.gsub!(replace_options[k], (options[k] || ''))
end
path
end
|
#vary_params ⇒ Object
62
63
64
|
# File 'lib/hal_api/representer/uri_methods.rb', line 62
def vary_params
[]
end
|
#vary_query_params ⇒ Object
66
67
68
|
# File 'lib/hal_api/representer/uri_methods.rb', line 66
def vary_query_params
"{?#{vary_params.join(',')}}"
end
|
#vary_url(represented) ⇒ Object
58
59
60
|
# File 'lib/hal_api/representer/uri_methods.rb', line 58
def vary_url(represented)
self_url(represented)
end
|