resterl 0.0.17 → 0.0.18
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/resterl/base_object.rb +66 -84
- data/lib/resterl/version.rb +1 -1
- data/spec/lib/resterl/base_object_spec.rb +18 -0
- metadata +5 -4
- data/TODO.rdoc +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42807a99a184545bb89ab8fe4d4ad206d129afe0
|
4
|
+
data.tar.gz: e409066f93c585ef5e809c03596f706f32e867d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e75e6a17b9d4c389bb1c16b5e66484786c671dbafbfb9a5160bf606102421af95d2c56909336267adc8dfa1741e4fa3f3e93bc555b78edb55790de66997ccb1
|
7
|
+
data.tar.gz: 51086ae900df5c26bc8fc5d64c8627f04614bef94662c3771ee050b9864f9b53aa9677aaee17348374939d0d9079fa4616fa09f7f61c66e22ba4af777d7cab94
|
data/lib/resterl/base_object.rb
CHANGED
@@ -1,98 +1,80 @@
|
|
1
1
|
require 'hashie'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module Resterl
|
4
|
+
class BaseObject
|
5
|
+
include ClassLevelInheritableAttributes
|
6
|
+
cattr_inheritable :resterl_client, :parser, :composer, :complete_mime_type,
|
7
|
+
:mapper
|
8
|
+
attr_reader :response
|
5
9
|
|
6
|
-
|
10
|
+
def initialize data = {}, response = nil
|
11
|
+
@data = Hashie::Mash.new data
|
12
|
+
@response = response
|
13
|
+
end
|
7
14
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
attr_reader :response
|
15
|
+
def method_missing sym, *args, &block
|
16
|
+
@data.send sym, *args, &block
|
17
|
+
end
|
12
18
|
|
13
|
-
|
14
|
-
|
19
|
+
def respond_to? sym
|
20
|
+
super || @data.respond_to?(sym)
|
21
|
+
end
|
15
22
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
23
|
+
class << self
|
24
|
+
def get_object url, params = {}
|
25
|
+
response = resterl_client.get(url, params, accept_header)
|
26
|
+
new_from_response(response)
|
27
|
+
end
|
20
28
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
@data.send sym, *args, &block
|
25
|
-
end
|
29
|
+
def post_to_object url, params = {}, data = {}
|
30
|
+
post_put_object(:post, url, params, data)
|
31
|
+
end
|
26
32
|
|
27
|
-
|
33
|
+
def delete_object url
|
34
|
+
resterl_client.delete(url, {}, accept_header, {})
|
35
|
+
end
|
28
36
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
doc = response.body
|
33
|
-
doc = parser.call(doc)
|
34
|
-
doc = mapper.map(doc) if @mapper
|
35
|
-
new(doc, response)
|
36
|
-
end
|
37
|
-
|
38
|
-
def self.post_to_object url, params = {}, data = {}
|
39
|
-
# TODO: Refactoring
|
37
|
+
def put_object url, params = {}, data = {}
|
38
|
+
post_put_object(:put, url, params, data)
|
39
|
+
end
|
40
40
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
resterl_client.delete(url, {}, headers, {})
|
59
|
-
# TODO: Antwort parsen?
|
60
|
-
end
|
61
|
-
|
62
|
-
def self.put_object url, params = {}, data
|
63
|
-
# TODO: Refactoring
|
64
|
-
|
65
|
-
headers = {
|
66
|
-
'Accept' => complete_mime_type,
|
67
|
-
'Content-Type' => complete_mime_type
|
68
|
-
}
|
69
|
-
data = composer.call(data)
|
70
|
-
response = resterl_client.put(url, params, data, headers)
|
71
|
-
|
72
|
-
doc = response.body
|
73
|
-
doc = parser.call(doc)
|
74
|
-
doc = mapper.map(doc) if @mapper
|
75
|
-
|
76
|
-
# TODO: In Ordnung?
|
77
|
-
new(doc, response)
|
78
|
-
end
|
79
|
-
|
41
|
+
def mime_type= t
|
42
|
+
self.parser, self.composer, self.complete_mime_type =
|
43
|
+
case t
|
44
|
+
when :json
|
45
|
+
[proc { |str| JSON.parse(str) },
|
46
|
+
proc(&:to_json),
|
47
|
+
'application/json'
|
48
|
+
]
|
49
|
+
when :xml
|
50
|
+
[proc { |str| Hash.from_xml(str) },
|
51
|
+
proc(&:to_xml),
|
52
|
+
'application/xml'
|
53
|
+
]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
80
58
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
59
|
+
def post_put_object method, url, params, data
|
60
|
+
headers = accept_header.merge(
|
61
|
+
'Content-Type' => complete_mime_type
|
62
|
+
)
|
63
|
+
data = composer.call(data)
|
64
|
+
response = resterl_client.send(method, url, params, data, headers)
|
65
|
+
new_from_response response
|
66
|
+
end
|
67
|
+
|
68
|
+
def new_from_response response
|
69
|
+
doc = response.body
|
70
|
+
doc = parser.call(doc)
|
71
|
+
doc = mapper.map(doc) if @mapper
|
72
|
+
new(doc, response)
|
73
|
+
end
|
74
|
+
|
75
|
+
def accept_header
|
76
|
+
{ 'Accept' => complete_mime_type }
|
77
|
+
end
|
95
78
|
end
|
96
79
|
end
|
97
|
-
|
98
80
|
end
|
data/lib/resterl/version.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
require 'active_support/core_ext/object/try'
|
4
|
+
|
5
|
+
describe Resterl::BaseObject do
|
6
|
+
subject { described_class.new(foo: :bar) }
|
7
|
+
context '#try from ActiveSupport' do
|
8
|
+
it 'returns a value if the key exists' do
|
9
|
+
expect(subject.try(:foo)).to eq :bar
|
10
|
+
end
|
11
|
+
it 'returns nil if the key does not exist' do
|
12
|
+
expect(subject.try(:fox)).not_to be
|
13
|
+
end
|
14
|
+
end
|
15
|
+
it { is_expected.to respond_to(:response) }
|
16
|
+
it { is_expected.to respond_to(:foo) }
|
17
|
+
it { is_expected.not_to respond_to(:fox) }
|
18
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resterl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.18
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Dütsch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -120,7 +120,6 @@ files:
|
|
120
120
|
- LICENSE
|
121
121
|
- README.rdoc
|
122
122
|
- Rakefile
|
123
|
-
- TODO.rdoc
|
124
123
|
- lib/resterl.rb
|
125
124
|
- lib/resterl/base_object.rb
|
126
125
|
- lib/resterl/caches/cache_interface.rb
|
@@ -139,6 +138,7 @@ files:
|
|
139
138
|
- lib/resterl/response.rb
|
140
139
|
- lib/resterl/version.rb
|
141
140
|
- resterl.gemspec
|
141
|
+
- spec/lib/resterl/base_object_spec.rb
|
142
142
|
- spec/lib/resterl/client_spec.rb
|
143
143
|
- spec/spec_helper.rb
|
144
144
|
homepage: http://github.com/Nix-wie-weg/resterl
|
@@ -160,11 +160,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
160
160
|
version: '0'
|
161
161
|
requirements: []
|
162
162
|
rubyforge_project:
|
163
|
-
rubygems_version: 2.4.
|
163
|
+
rubygems_version: 2.4.6
|
164
164
|
signing_key:
|
165
165
|
specification_version: 4
|
166
166
|
summary: Rudimentary HTTP client with focus on caching
|
167
167
|
test_files:
|
168
|
+
- spec/lib/resterl/base_object_spec.rb
|
168
169
|
- spec/lib/resterl/client_spec.rb
|
169
170
|
- spec/spec_helper.rb
|
170
171
|
has_rdoc:
|
data/TODO.rdoc
DELETED