grape_hal_integration 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/grape_hal_integration.gemspec +1 -1
- data/lib/grape_hal_integration.rb +54 -24
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f1456724906907fcd3387a1f008a35d1583b2c9
|
4
|
+
data.tar.gz: d69dcb3eee1fcfc54fda87fa1db6c09fb5e912e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 383b13f0cf50d92a48bdab2f6a8dea0e6849d9e90e14a78dbe8f8fcc53a45b493b27509f2cbd381df47d0fa1a2e06fcbb998ab638673c2aa3838d29460bf2e73
|
7
|
+
data.tar.gz: 4c3b6b1886613c0fc85d67030433a0b7f583f1120d1f41da541928283d9f3117bcc1c91a77972d76b3794f107562dc428c3e1c5ddf919f1f697d051f2b26ad28
|
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'grape_hal_integration'
|
7
|
-
spec.version = '0.0
|
7
|
+
spec.version = '0.1.0'
|
8
8
|
spec.authors = ['Miroslav Csonka']
|
9
9
|
spec.email = ['miroslav.csonka@gmail.com']
|
10
10
|
spec.summary = %q{Provides convenient helpers for using hal with grape framework}
|
@@ -4,15 +4,36 @@ class GrapeHalIntegration < Grape::API
|
|
4
4
|
|
5
5
|
@@endpoints = {}
|
6
6
|
|
7
|
-
def self.
|
8
|
-
|
7
|
+
def self.links(class_name, action_name)
|
8
|
+
links = class_name.endpoint(action_name)[:links]
|
9
|
+
_links = Hash[links.map { |link|
|
10
|
+
|
11
|
+
if link.include? '#'
|
12
|
+
class_name2, action_name2 = link.split '#'
|
13
|
+
name = action_name2
|
14
|
+
endpoint = class_name2.constantize.endpoint(action_name2)
|
15
|
+
else
|
16
|
+
name = link
|
17
|
+
endpoint = class_name.endpoint(link)
|
18
|
+
end
|
19
|
+
|
20
|
+
raise "Unregistered link to '#{link}'" if endpoint.nil?
|
21
|
+
|
22
|
+
return [name, endpoint[:self]]
|
23
|
+
}]
|
24
|
+
_links[:self] = class_name.endpoint(action_name)[:self]
|
25
|
+
_links
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.build_current_action(name)
|
29
|
+
"#{self.name}##{name.to_s}"
|
9
30
|
end
|
10
31
|
|
11
|
-
def self.
|
32
|
+
def self.implement(name, url, links = [], &block)
|
12
33
|
name = name.to_s
|
13
34
|
http_verb, url = url.split ' '
|
14
|
-
|
15
|
-
@@endpoints[
|
35
|
+
|
36
|
+
@@endpoints[build_current_action(name)] = {
|
16
37
|
name: name,
|
17
38
|
links: links,
|
18
39
|
block: block,
|
@@ -21,28 +42,39 @@ class GrapeHalIntegration < Grape::API
|
|
21
42
|
method: http_verb.upcase
|
22
43
|
}
|
23
44
|
}
|
45
|
+
@@endpoints[build_current_action(name)][:self][:templated] = true if url.include? '{'
|
24
46
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
super_context = lambda do
|
47
|
+
class_name = self.name.constantize
|
48
|
+
get_params_name = url.scan(/\{([^}]*)\}/).flatten
|
49
|
+
wrapper_block = lambda do
|
30
50
|
get_params_values = get_params_name.map do |get_param_name|
|
31
|
-
|
51
|
+
result = get_param_name.match(/(.+)_id/)
|
52
|
+
value = params[get_param_name.intern]
|
53
|
+
unless result.blank?
|
54
|
+
begin
|
55
|
+
klass = result[1].singularize.classify.constantize
|
56
|
+
value = klass.find(value) if klass.class == Class
|
57
|
+
rescue NameError
|
58
|
+
end
|
59
|
+
end
|
60
|
+
value
|
32
61
|
end
|
33
|
-
|
62
|
+
|
63
|
+
hal = {_links: HalIntegrator.links(class_name, name)}
|
64
|
+
response = self.instance_exec *get_params_values, &block
|
65
|
+
hal.merge(response)
|
34
66
|
end
|
35
67
|
|
36
68
|
resource url.gsub('{', ':').gsub('}', '') do
|
37
69
|
case http_verb.upcase
|
38
70
|
when 'POST'
|
39
|
-
post &
|
71
|
+
post &wrapper_block
|
40
72
|
when 'PUT'
|
41
|
-
put &
|
73
|
+
put &wrapper_block
|
42
74
|
when 'DELETE'
|
43
|
-
delete &
|
75
|
+
delete &wrapper_block
|
44
76
|
when 'GET'
|
45
|
-
get &
|
77
|
+
get &wrapper_block
|
46
78
|
else
|
47
79
|
raise "Undefined http verb '#{http_verb}'"
|
48
80
|
end
|
@@ -50,19 +82,17 @@ class GrapeHalIntegration < Grape::API
|
|
50
82
|
end
|
51
83
|
|
52
84
|
def self.endpoint(name)
|
53
|
-
@@endpoints[
|
85
|
+
@@endpoints[build_current_action(name.to_s)]
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.endpoint_url(name)
|
89
|
+
endpoint(name)[:self]
|
54
90
|
end
|
55
91
|
|
56
92
|
def self.method_missing(method_name, *arguments, &block)
|
57
|
-
raise "Unknown name '#{
|
93
|
+
raise "Unknown name '#{build_current_action(method_name)}'" unless @@endpoints.has_key? build_current_action(method_name)
|
58
94
|
context = arguments.shift
|
59
95
|
context.instance_exec *arguments, &endpoint(method_name)[:block]
|
60
96
|
end
|
61
97
|
|
62
|
-
def self.endpoint_url(name)
|
63
|
-
e = endpoint(name)
|
64
|
-
raise "Could not find endpoint '#{name}'" unless e.is_a? Hash
|
65
|
-
e[:self]
|
66
|
-
end
|
67
|
-
|
68
98
|
end
|