distant 0.1.0 → 0.1.1
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/README.md +34 -0
- data/distant.gemspec +1 -1
- data/lib/distant/base.rb +9 -14
- data/lib/distant/translator.rb +42 -0
- data/lib/distant.rb +1 -0
- data/spec/client/base_spec.rb +14 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 730b8a2b639f0f0cac7f5174658e69f64eafb634
|
4
|
+
data.tar.gz: 39258025d76ebfab214f398130f36872f2fa8971
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c66735d6409692b5c80e6c74a00a07048b18bf6f76ff9e54990b8cfc67ed016d11122e34622741c9864a4e72521d558e792be1c77ee3208f9b43d962cf78dc52
|
7
|
+
data.tar.gz: f61b848ee6324101a6514aeebfbf1eaca88e45127d3798de1e8b67f87d9735234275c782473b38244d2b3f07dc5fa43f5d95ba9e2528386e6d01e7e9db6709be
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,26 @@
|
|
1
1
|
|
2
2
|
# Distant Client
|
3
3
|
|
4
|
+
## Why?
|
5
|
+
|
6
|
+
Active-Rest-Client is dead.
|
7
|
+
|
8
|
+
FlexiRest doesn't work for me, and I'm too impatient to step painstakingly slow
|
9
|
+
through Faraday code to figure out why.
|
10
|
+
|
11
|
+
* Distant uses HTTParty under the hood.
|
12
|
+
* If you need to set special headers, it's easy.
|
13
|
+
* If you need to disable ssl certificate authentication, it's easy.
|
14
|
+
* If you need to enable debugging information, it's easy.
|
15
|
+
* If you need to change the data coming to or from your API, it's easy.
|
16
|
+
|
17
|
+
## What Not To Do:
|
18
|
+
|
19
|
+
**DON'T**
|
20
|
+
|
21
|
+
* Think you can expose database functionality as a REST API. You will be disappointed by performance every time.
|
22
|
+
* Use this when another client for the API you want to consume already exists.
|
23
|
+
|
4
24
|
## Installation
|
5
25
|
|
6
26
|
### In your Gemfile
|
@@ -20,18 +40,32 @@ Distant.configure do |config|
|
|
20
40
|
end
|
21
41
|
|
22
42
|
class Organization < Distant::Base
|
43
|
+
attr_accessor :id, :name
|
23
44
|
get :all, '/organizations'
|
24
45
|
get :find, '/organizations/:id'
|
25
46
|
has_many :networks, '/organizations/:id/networks'
|
47
|
+
translate do
|
48
|
+
from_hash do |hash|
|
49
|
+
recursive_underscore(hash)
|
50
|
+
end
|
51
|
+
to_hash do |obj|
|
52
|
+
{
|
53
|
+
id: obj.id,
|
54
|
+
fooId: obj.foo_id,
|
55
|
+
}
|
56
|
+
end
|
57
|
+
end
|
26
58
|
end
|
27
59
|
|
28
60
|
class Network < Distant::Base
|
61
|
+
attr_accessor :id, :organization_id, :name
|
29
62
|
belongs_to :organization
|
30
63
|
get :find, '/networks/:id'
|
31
64
|
has_many :clients, '/networks/:id/clients'
|
32
65
|
end
|
33
66
|
|
34
67
|
class Client < Distant::Base
|
68
|
+
attr_accessor :id, :network_id, :name
|
35
69
|
belongs_to :network
|
36
70
|
get :find, '/clients/:id'
|
37
71
|
end
|
data/distant.gemspec
CHANGED
data/lib/distant/base.rb
CHANGED
@@ -85,26 +85,21 @@ module Distant
|
|
85
85
|
case response.code
|
86
86
|
when 200..299
|
87
87
|
parsed = JSON.parse(response.body, symbolize_names: true)
|
88
|
-
|
88
|
+
parsed.is_a?(Array) ?
|
89
|
+
parsed.map{ |item| translator.translate_from_hash(item) }
|
90
|
+
: translator.translate_from_hash(item)
|
89
91
|
else
|
90
92
|
raise Distant::ApiError.new response
|
91
93
|
end
|
92
94
|
end
|
93
95
|
|
94
|
-
|
96
|
+
def translator
|
97
|
+
@translator
|
98
|
+
end
|
95
99
|
|
96
|
-
def
|
97
|
-
|
98
|
-
|
99
|
-
thing.each do |key, val|
|
100
|
-
out[key.to_s.underscore.to_sym] = recursive_underscore(val)
|
101
|
-
end
|
102
|
-
out
|
103
|
-
elsif thing.is_a? Array
|
104
|
-
thing.map{ |item| recursive_underscore(item) }
|
105
|
-
else
|
106
|
-
thing
|
107
|
-
end
|
100
|
+
def translate(&block)
|
101
|
+
@translator = Distant::Translator.new
|
102
|
+
translator.instance_eval(&block)
|
108
103
|
end
|
109
104
|
end
|
110
105
|
|
@@ -0,0 +1,42 @@
|
|
1
|
+
|
2
|
+
module Distant
|
3
|
+
class Translator
|
4
|
+
attr_accessor :from_hash_translator, :to_hash_translator
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
# Sensible defaults:
|
8
|
+
self.from_hash_translator = Proc.new{ |hash| hash }
|
9
|
+
self.to_hash_translator = Proc.new{ |obj| obj.to_h }
|
10
|
+
end
|
11
|
+
|
12
|
+
def translate_from_hash(hash)
|
13
|
+
self.from_hash_translator.call(hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
def translate_to_hash(obj)
|
17
|
+
self.to_hash_translator.call(obj)
|
18
|
+
end
|
19
|
+
|
20
|
+
def from_hash(&block)
|
21
|
+
self.from_hash_translator = block
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_hash(&block)
|
25
|
+
self.to_hash_translator = block
|
26
|
+
end
|
27
|
+
|
28
|
+
def recursive_underscore(thing)
|
29
|
+
if thing.is_a? Hash
|
30
|
+
out = {}
|
31
|
+
thing.each do |key, val|
|
32
|
+
out[key.to_s.underscore.to_sym] = recursive_underscore(val)
|
33
|
+
end
|
34
|
+
out
|
35
|
+
elsif thing.is_a? Array
|
36
|
+
thing.map{ |item| recursive_underscore(item) }
|
37
|
+
else
|
38
|
+
thing
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/distant.rb
CHANGED
data/spec/client/base_spec.rb
CHANGED
@@ -7,9 +7,22 @@ describe Distant::Base do
|
|
7
7
|
end
|
8
8
|
class Distant::BaseTest < Distant::Base
|
9
9
|
attr_accessor :id, :name
|
10
|
-
|
11
10
|
has_many :sub_tests, '/base/tests/:id/sub_tests'
|
11
|
+
|
12
|
+
translate do
|
13
|
+
from_hash do |hash|
|
14
|
+
recursive_underscore(hash)
|
15
|
+
end
|
16
|
+
to_hash do |obj|
|
17
|
+
{
|
18
|
+
id: obj.id,
|
19
|
+
fooId: obj.foo_id,
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
12
24
|
end
|
25
|
+
|
13
26
|
class Distant::SubTest < Distant::Base
|
14
27
|
attr_accessor :id, :base_test_id
|
15
28
|
belongs_to :base_test, ''
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Drago
|
@@ -154,6 +154,7 @@ files:
|
|
154
154
|
- lib/distant/base.rb
|
155
155
|
- lib/distant/config.rb
|
156
156
|
- lib/distant/connection.rb
|
157
|
+
- lib/distant/translator.rb
|
157
158
|
- spec/client/base_spec.rb
|
158
159
|
- spec/client/config_spec.rb
|
159
160
|
- spec/client/connection_spec.rb
|