distant 0.1.3 → 0.1.4
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/Gemfile.lock +1 -1
- data/distant.gemspec +1 -1
- data/lib/distant/base.rb +2 -2
- data/lib/distant/config.rb +2 -1
- data/lib/distant/connection.rb +6 -4
- data/spec/client/base_spec.rb +33 -19
- data/spec/client/connection_spec.rb +6 -4
- data/spec/client/translator_spec.rb +97 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11000ca2e279a5ab60f8848ad83ff73a9e412752
|
4
|
+
data.tar.gz: 74f397589688443544dc0dc61c5a073bc8035f4b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de50b26f9fe809c513c168a9877b6ef097a01b37b0e96ca1f164e353c84d673e41fef56e9658f6e5e5daddea45bbf0e2ac4ccaaa7cc4f017b623679349e26aba
|
7
|
+
data.tar.gz: 7ce0dc782fc5d42b1b09dc12840cb272a1573365230c346e6b7f74082a01867fca12f2a62568179b97ad04de9e5fa368a20d2d33b8834dfc954ab39763597f19
|
data/Gemfile.lock
CHANGED
data/distant.gemspec
CHANGED
data/lib/distant/base.rb
CHANGED
@@ -33,7 +33,7 @@ module Distant
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def connection
|
36
|
-
@@connection ||= Distant::Connection.
|
36
|
+
@@connection ||= Distant::Connection.configure( Distant.config )
|
37
37
|
end
|
38
38
|
|
39
39
|
def has_many_rels
|
@@ -111,7 +111,7 @@ module Distant
|
|
111
111
|
parsed = JSON.parse(response.body, symbolize_names: true)
|
112
112
|
parsed.is_a?(Array) ?
|
113
113
|
parsed.map{ |item| translator.translate_from_hash(item) }
|
114
|
-
: translator.translate_from_hash(
|
114
|
+
: translator.translate_from_hash(parsed)
|
115
115
|
else
|
116
116
|
raise Distant::ApiError.new response
|
117
117
|
end
|
data/lib/distant/config.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
|
2
2
|
module Distant
|
3
3
|
class Config
|
4
|
-
attr_accessor :base_uri, :auth_header_generator, :default_header_generator
|
4
|
+
attr_accessor :base_uri, :debug, :auth_header_generator, :default_header_generator
|
5
5
|
|
6
6
|
def initialize
|
7
7
|
# Default just returns an empty hash:
|
8
8
|
self.auth_header_generator = Proc.new{ {} }
|
9
9
|
self.default_header_generator = Proc.new{ {} }
|
10
|
+
self.debug = false
|
10
11
|
end
|
11
12
|
|
12
13
|
def set_authentication_headers_with(&block)
|
data/lib/distant/connection.rb
CHANGED
@@ -3,11 +3,13 @@ module Distant
|
|
3
3
|
class Connection
|
4
4
|
include HTTParty
|
5
5
|
|
6
|
-
|
7
|
-
def initialize(config)
|
6
|
+
def self.configure(config)
|
8
7
|
raise ArgumentError.new 'invalid config' unless config.is_a? Distant::Config
|
9
|
-
self.
|
10
|
-
|
8
|
+
self.base_uri config.base_uri
|
9
|
+
if config.debug
|
10
|
+
self.debug_output
|
11
|
+
end
|
12
|
+
self
|
11
13
|
end
|
12
14
|
end
|
13
15
|
end
|
data/spec/client/base_spec.rb
CHANGED
@@ -88,7 +88,7 @@ describe Distant::Base do
|
|
88
88
|
context 'when called' do
|
89
89
|
context 'the first time' do
|
90
90
|
before do
|
91
|
-
expect(Distant::Connection).to receive(:
|
91
|
+
expect(Distant::Connection).to receive(:configure).exactly(1).times
|
92
92
|
end
|
93
93
|
it 'returns a new Distant::Connection' do
|
94
94
|
Distant::BaseTest.connection
|
@@ -96,7 +96,7 @@ describe Distant::Base do
|
|
96
96
|
end
|
97
97
|
context 'subsequent times' do
|
98
98
|
before do
|
99
|
-
expect(Distant::Connection).to receive(:
|
99
|
+
expect(Distant::Connection).to receive(:configure).exactly(1).times.and_call_original
|
100
100
|
end
|
101
101
|
it 'returns the same Distant::Connection' do
|
102
102
|
first_connection = Distant::BaseTest.connection
|
@@ -121,7 +121,7 @@ describe Distant::Base do
|
|
121
121
|
expect(Distant::BaseTest).to receive(:preprocess_response){ [{id: 123}] }
|
122
122
|
end
|
123
123
|
it 'makes a GET request with the correct route' do
|
124
|
-
|
124
|
+
expect(Distant::Connection).to receive(:get).with(@route, {})
|
125
125
|
# Finally:
|
126
126
|
Distant::BaseTest.all
|
127
127
|
end
|
@@ -131,7 +131,7 @@ describe Distant::Base do
|
|
131
131
|
expect(Distant::BaseTest).to receive(:preprocess_response){ {id: 123} }
|
132
132
|
end
|
133
133
|
it 'makes a GET request with the correct route' do
|
134
|
-
|
134
|
+
expect(Distant::Connection).to receive(:get).with(@single_route.gsub(':id', '123'), {})
|
135
135
|
# Finally:
|
136
136
|
Distant::BaseTest.find(id: 123)
|
137
137
|
end
|
@@ -151,7 +151,7 @@ describe Distant::Base do
|
|
151
151
|
expect(Distant::BaseTest).to receive(:preprocess_response){ [{base_test_id: 123, id: 456}]}
|
152
152
|
end
|
153
153
|
it 'makes a GET request with the correct route' do
|
154
|
-
|
154
|
+
expect(Distant::Connection).to receive(:get).with('/base/123/tests', {})
|
155
155
|
result = Distant::BaseTest.new(id: 123).sub_tests
|
156
156
|
expect(result.first).to be_a Distant::SubTest
|
157
157
|
end
|
@@ -171,7 +171,7 @@ describe Distant::Base do
|
|
171
171
|
expect(Distant::SubTest).to receive(:preprocess_response){ {id: 123, name: 'foo'}}
|
172
172
|
end
|
173
173
|
it 'makes a GET request with the correct route' do
|
174
|
-
|
174
|
+
expect(Distant::Connection).to receive(:get).with('/base/123', {})
|
175
175
|
result = Distant::SubTest.new(id: 456, base_test_id: 123).base_test
|
176
176
|
expect(result).to be_a Distant::BaseTest
|
177
177
|
end
|
@@ -196,19 +196,33 @@ describe Distant::Base do
|
|
196
196
|
end
|
197
197
|
context 'and the response' do
|
198
198
|
context 'is valid JSON' do
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
199
|
+
context 'in the form of' do
|
200
|
+
context 'an Array of Objects' do
|
201
|
+
before do
|
202
|
+
@response_data = [
|
203
|
+
{id: 123, name: 'Test'},
|
204
|
+
{fooId: 456, nickName: 'Testy McTesterson'}
|
205
|
+
]
|
206
|
+
@expected_data = [
|
207
|
+
{id: 123, name: 'Test'},
|
208
|
+
{foo_id: 456, nick_name: 'Testy McTesterson'}
|
209
|
+
]
|
210
|
+
expect(@response).to receive(:body){ @response_data.to_json }
|
211
|
+
end
|
212
|
+
it 'returns the parsed JSON response' do
|
213
|
+
expect(Distant::BaseTest.preprocess_response(@response)).to eq @expected_data
|
214
|
+
end
|
215
|
+
end
|
216
|
+
context 'an Object' do
|
217
|
+
before do
|
218
|
+
@response_data = {fooId: 456, nickName: 'Testy McTesterson'}
|
219
|
+
@expected_data = {foo_id: 456, nick_name: 'Testy McTesterson'}
|
220
|
+
expect(@response).to receive(:body){ @response_data.to_json }
|
221
|
+
end
|
222
|
+
it 'returns the parsed JSON response' do
|
223
|
+
expect(Distant::BaseTest.preprocess_response(@response)).to eq @expected_data
|
224
|
+
end
|
225
|
+
end
|
212
226
|
end
|
213
227
|
end
|
214
228
|
context 'is not valid JSON' do
|
@@ -1,16 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Distant::Connection do
|
4
|
-
describe '
|
4
|
+
describe '.configure' do
|
5
5
|
context 'when a Distant::Config object' do
|
6
6
|
context 'is provided' do
|
7
|
-
it '
|
8
|
-
|
7
|
+
it 'does not raise an exception' do
|
8
|
+
config = Distant::Config.new
|
9
|
+
config.debug = true
|
10
|
+
expect{described_class.configure(config)}.not_to raise_error
|
9
11
|
end
|
10
12
|
end
|
11
13
|
context 'is not provided' do
|
12
14
|
it 'raises an exception' do
|
13
|
-
expect{described_class.
|
15
|
+
expect{described_class.configure('a string')}.to raise_error ArgumentError
|
14
16
|
end
|
15
17
|
end
|
16
18
|
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Distant::Translator do
|
4
|
+
describe 'initialize' do
|
5
|
+
before do
|
6
|
+
@translator = described_class.new
|
7
|
+
end
|
8
|
+
it 'sets default from/to translators' do
|
9
|
+
expect(@translator.from_hash_translator).to be_a Proc
|
10
|
+
expect(@translator.to_hash_translator).to be_a Proc
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#translate_from_hash(hash)' do
|
15
|
+
before do
|
16
|
+
@translator = described_class.new
|
17
|
+
@translator.from_hash do |hash|
|
18
|
+
secret = hash[:secret]
|
19
|
+
{foo: secret}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
it 'uses the supplied translation block' do
|
23
|
+
my_secret = SecureRandom.uuid
|
24
|
+
result = @translator.translate_from_hash(secret: my_secret)
|
25
|
+
expect(result).to eq(foo: my_secret)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
describe '#translate_to_hash(obj)' do
|
29
|
+
before do
|
30
|
+
@translator = described_class.new
|
31
|
+
@translator.to_hash do |hash|
|
32
|
+
secret = hash[:secret]
|
33
|
+
{foo: secret}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
it 'uses the supplied translation block' do
|
37
|
+
my_secret = SecureRandom.uuid
|
38
|
+
result = @translator.translate_to_hash(secret: my_secret)
|
39
|
+
expect(result).to eq(foo: my_secret)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
describe '#from_hash(&block)' do
|
43
|
+
before do
|
44
|
+
@translator = described_class.new
|
45
|
+
@secret = SecureRandom.uuid
|
46
|
+
@translator.from_hash do
|
47
|
+
@secret
|
48
|
+
end
|
49
|
+
end
|
50
|
+
it 'sets the block as the from_hash_translator' do
|
51
|
+
expect(@translator.from_hash_translator.call).to eq @secret
|
52
|
+
end
|
53
|
+
end
|
54
|
+
describe '#to_hash(&block)' do
|
55
|
+
before do
|
56
|
+
@translator = described_class.new
|
57
|
+
@secret = SecureRandom.uuid
|
58
|
+
@translator.to_hash do
|
59
|
+
@secret
|
60
|
+
end
|
61
|
+
end
|
62
|
+
it 'sets the block as the to_hash_translator' do
|
63
|
+
expect(@translator.to_hash_translator.call).to eq @secret
|
64
|
+
end
|
65
|
+
end
|
66
|
+
describe '#recursive_underscore(thing)' do
|
67
|
+
context 'when given' do
|
68
|
+
before do
|
69
|
+
@translator = described_class.new
|
70
|
+
end
|
71
|
+
context 'an Array of hashes' do
|
72
|
+
before do
|
73
|
+
@data = [{'fooBar' => 'baz'}]
|
74
|
+
end
|
75
|
+
it 'calls recursive_underscore on all elements in the array' do
|
76
|
+
expect(@translator.recursive_underscore(@data)).to eq([{foo_bar: 'baz'}])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
context 'a Hash' do
|
80
|
+
before do
|
81
|
+
@data = {'fooBar' => 'baz'}
|
82
|
+
end
|
83
|
+
it 'underscores the keys in the hash' do
|
84
|
+
expect(@translator.recursive_underscore(@data)).to eq(foo_bar: 'baz')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
context 'anything else' do
|
88
|
+
before do
|
89
|
+
@data = 'Hello, World'
|
90
|
+
end
|
91
|
+
it 'returns the thing as-is' do
|
92
|
+
expect(@translator.recursive_underscore(@data)).to eq(@data)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
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.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Drago
|
@@ -159,6 +159,7 @@ files:
|
|
159
159
|
- spec/client/config_spec.rb
|
160
160
|
- spec/client/connection_spec.rb
|
161
161
|
- spec/client/module_spec.rb
|
162
|
+
- spec/client/translator_spec.rb
|
162
163
|
- spec/spec_helper.rb
|
163
164
|
homepage: https://github.com/distant/distant
|
164
165
|
licenses: []
|
@@ -188,4 +189,5 @@ test_files:
|
|
188
189
|
- spec/client/config_spec.rb
|
189
190
|
- spec/client/connection_spec.rb
|
190
191
|
- spec/client/module_spec.rb
|
192
|
+
- spec/client/translator_spec.rb
|
191
193
|
- spec/spec_helper.rb
|