distant 0.1.6 → 0.1.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b013860fb76db4c918bb3dc7a2de9821d9ec8f47
4
- data.tar.gz: b8d368d98798f9e79f19af8202d56ed9ab2d6205
3
+ metadata.gz: 28f6eb35f0ed682487b4386e1f95b2367c9956c3
4
+ data.tar.gz: 222daaed9d2614c6cfeec30275d5327e0c57c844
5
5
  SHA512:
6
- metadata.gz: 73df7dd9644e1521668a611708a163f0163f330b9ad488488ab521eebaa4e7c951f6b10af1aff7066c6efeccc38c71b52804e84d64a31788842f54a1b51550cc
7
- data.tar.gz: 9c3254be7bff13ff9d0acc4fc6a5104de935c3730a1a8035d7f1f26e50032774544d7659c924b7a4aceb45e2ffc403517a676f37ea0ad5fa7733343806352a7e
6
+ metadata.gz: 518d5f087ee08af705f7ba9cdb0bd54dc3b482eb3e51614fa4902b069a9257190bb87a6c5219d1319c7bb51a997fa4a38dd389a37bff6aa7ca95e9c2455e34a8
7
+ data.tar.gz: 2be94392e8ab8d23acb09a578156af76d18cc034806cb83047a1638be0cc6f51f7af9435d098da94db35290821b361c4d99c5947a6602b51fdd306ccb7cf52e7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- distant (0.1.6)
4
+ distant (0.1.7)
5
5
  activesupport
6
6
  httparty
7
7
 
data/distant.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'distant'
3
- spec.version = '0.1.6'
3
+ spec.version = '0.1.7'
4
4
  spec.authors = ['John Drago']
5
5
  spec.email = 'jdrago.999@gmail.com'
6
6
  spec.homepage = 'https://github.com/distant/distant'
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
- path = path_closure_generator(route).call(*args)
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
- path = self.class.path_closure_generator(route).call(id: self.id)
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
- path = self.class.path_closure_generator(route).call(id: foreign_key_value)
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)
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: distant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Drago