distant 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/distant.gemspec +1 -1
- data/lib/distant/base.rb +12 -4
- data/spec/client/base_spec.rb +15 -11
- 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: 28f6eb35f0ed682487b4386e1f95b2367c9956c3
|
4
|
+
data.tar.gz: 222daaed9d2614c6cfeec30275d5327e0c57c844
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 518d5f087ee08af705f7ba9cdb0bd54dc3b482eb3e51614fa4902b069a9257190bb87a6c5219d1319c7bb51a997fa4a38dd389a37bff6aa7ca95e9c2455e34a8
|
7
|
+
data.tar.gz: 2be94392e8ab8d23acb09a578156af76d18cc034806cb83047a1638be0cc6f51f7af9435d098da94db35290821b361c4d99c5947a6602b51fdd306ccb7cf52e7
|
data/Gemfile.lock
CHANGED
data/distant.gemspec
CHANGED
data/lib/distant/base.rb
CHANGED
@@ -30,6 +30,7 @@ module Distant
|
|
30
30
|
def init_class_vars
|
31
31
|
@has_many = [ ]
|
32
32
|
@belongs_to = [ ]
|
33
|
+
@translator = Distant::Translator.new
|
33
34
|
end
|
34
35
|
|
35
36
|
def connection
|
@@ -50,7 +51,8 @@ module Distant
|
|
50
51
|
|
51
52
|
def get(name, route)
|
52
53
|
define_singleton_method(name) do |*args|
|
53
|
-
|
54
|
+
path_generator, captures = self.path_closure_generator(route)
|
55
|
+
path = path_generator.call(*args)
|
54
56
|
headers = Distant.config.default_headers('')
|
55
57
|
.merge(Distant.config.auth_headers(''))
|
56
58
|
response_data = preprocess_response connection.get(path, headers: headers)
|
@@ -65,7 +67,9 @@ module Distant
|
|
65
67
|
def has_many(plural, route)
|
66
68
|
@has_many << plural.to_sym
|
67
69
|
define_method(plural) do
|
68
|
-
|
70
|
+
path_generator, captures = self.class.path_closure_generator(route)
|
71
|
+
path_args = captures.map{|x| {x.to_sym => send(x)}}.reduce({}, :merge)
|
72
|
+
path = path_generator.call(path_args)
|
69
73
|
headers = Distant.config.default_headers('')
|
70
74
|
.merge(Distant.config.auth_headers(''))
|
71
75
|
class_ref = Kernel.const_get self.class.to_s.deconstantize + '::' + plural.to_s.singularize.classify
|
@@ -79,7 +83,9 @@ module Distant
|
|
79
83
|
define_method(singular) do
|
80
84
|
foreign_key_attr = singular.to_s + '_id'
|
81
85
|
foreign_key_value = self.send(foreign_key_attr)
|
82
|
-
|
86
|
+
path_generator, captures = self.class.path_closure_generator(route)
|
87
|
+
path_args = captures.map{|x| {x.to_sym => send(x)}}.reduce({}, :merge)
|
88
|
+
path = path_generator.call(id: foreign_key_value)
|
83
89
|
headers = Distant.config.default_headers('')
|
84
90
|
.merge(Distant.config.auth_headers(''))
|
85
91
|
class_ref = Kernel.const_get self.class.to_s.deconstantize + '::' + singular.to_s.classify
|
@@ -96,13 +102,15 @@ module Distant
|
|
96
102
|
template = route.gsub(%r{:([^/\{\}\:\-]+)}, "%{#{$1}}")
|
97
103
|
|
98
104
|
# Convert '/foo/%{bar}/%{bux}' to /foo/123/456 with {bar: 123, bux: 456}
|
99
|
-
class_eval <<-EOF
|
105
|
+
proc = class_eval <<-EOF
|
100
106
|
Proc.new do |#{captures.map{|cap| "#{cap}:"}.join(', ')}|
|
101
107
|
template % {
|
102
108
|
#{captures.map{ |cap| cap + ": " + cap }.join(', ')}
|
103
109
|
}
|
104
110
|
end
|
105
111
|
EOF
|
112
|
+
|
113
|
+
return proc, captures
|
106
114
|
end
|
107
115
|
|
108
116
|
def preprocess_response(response)
|
data/spec/client/base_spec.rb
CHANGED
@@ -36,7 +36,7 @@ describe Distant::Base do
|
|
36
36
|
end
|
37
37
|
context 'the closure returned' do
|
38
38
|
before do
|
39
|
-
@closure = Distant::BaseTest.path_closure_generator(@route)
|
39
|
+
@closure, @captures = Distant::BaseTest.path_closure_generator(@route)
|
40
40
|
end
|
41
41
|
it 'accepts no arguments' do
|
42
42
|
expect{@closure.call()}.not_to raise_error
|
@@ -49,7 +49,7 @@ describe Distant::Base do
|
|
49
49
|
end
|
50
50
|
context 'the closure returned' do
|
51
51
|
before do
|
52
|
-
@closure = Distant::BaseTest.path_closure_generator(@route)
|
52
|
+
@closure, @captures = Distant::BaseTest.path_closure_generator(@route)
|
53
53
|
end
|
54
54
|
it 'accepts one corresponding argument' do
|
55
55
|
expect{@closure.call(id: 123)}.not_to raise_error
|
@@ -63,7 +63,7 @@ describe Distant::Base do
|
|
63
63
|
end
|
64
64
|
context 'the closure returned' do
|
65
65
|
before do
|
66
|
-
@closure = Distant::BaseTest.path_closure_generator(@route)
|
66
|
+
@closure, @captures = Distant::BaseTest.path_closure_generator(@route)
|
67
67
|
end
|
68
68
|
it 'accepts all corresponding arguments' do
|
69
69
|
expect{@closure.call(id: 123, bar_id: 456)}.not_to raise_error
|
@@ -147,11 +147,13 @@ describe Distant::Base do
|
|
147
147
|
expect(Distant::BaseTest.new).to respond_to :sub_tests
|
148
148
|
end
|
149
149
|
context 'when the plural instance method is called' do
|
150
|
-
before do
|
151
|
-
expect(Distant::BaseTest).to receive(:preprocess_response){ [{base_test_id: 123, id: 456}]}
|
152
|
-
end
|
153
150
|
it 'makes a GET request with the correct route' do
|
154
|
-
expect(Distant::Connection).to receive(:get).with('/base/123/tests', headers: {})
|
151
|
+
expect(Distant::Connection).to receive(:get).with('/base/123/tests', headers: {}) do
|
152
|
+
response = double('response')
|
153
|
+
expect(response).to receive(:code){ 200 }
|
154
|
+
expect(response).to receive(:body){ [{base_test_id: 123, id: 456}].to_json }
|
155
|
+
response
|
156
|
+
end
|
155
157
|
result = Distant::BaseTest.new(id: 123).sub_tests
|
156
158
|
expect(result.first).to be_a Distant::SubTest
|
157
159
|
end
|
@@ -167,11 +169,13 @@ describe Distant::Base do
|
|
167
169
|
expect(Distant::SubTest.new).to respond_to :base_test
|
168
170
|
end
|
169
171
|
context 'when the instance method is called' do
|
170
|
-
before do
|
171
|
-
expect(Distant::SubTest).to receive(:preprocess_response){ {id: 123, name: 'foo'}}
|
172
|
-
end
|
173
172
|
it 'makes a GET request with the correct route' do
|
174
|
-
expect(Distant::Connection).to receive(:get).with('/base/123', headers: {})
|
173
|
+
expect(Distant::Connection).to receive(:get).with('/base/123', headers: {}) do
|
174
|
+
response = double('response')
|
175
|
+
expect(response).to receive(:code){ 200 }
|
176
|
+
expect(response).to receive(:body){ {id: 123, name: 'test'}.to_json }
|
177
|
+
response
|
178
|
+
end
|
175
179
|
result = Distant::SubTest.new(id: 456, base_test_id: 123).base_test
|
176
180
|
expect(result).to be_a Distant::BaseTest
|
177
181
|
end
|