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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eefda67e9d4b1b634f09e64d6744a43a6733dfd4
4
- data.tar.gz: 302093e3f36f08d49d1918759047255aa9c28867
3
+ metadata.gz: 42807a99a184545bb89ab8fe4d4ad206d129afe0
4
+ data.tar.gz: e409066f93c585ef5e809c03596f706f32e867d1
5
5
  SHA512:
6
- metadata.gz: 1ccf4876bf18424c64528b8a257d651b531ce92979f52ebbc9cf54e51a4372f881b5e7072accf557b56f5b0385b47c553f52d9df5ef6b07429a348639815f473
7
- data.tar.gz: 654b12d576e911c072d58426edb17b37cbd97ce3626b179cebb7a837536d32c93867387e93cdaca8a6e5acefe4a1d15ce9976968bfcbfb2ff2bea1e14d3a98be
6
+ metadata.gz: 6e75e6a17b9d4c389bb1c16b5e66484786c671dbafbfb9a5160bf606102421af95d2c56909336267adc8dfa1741e4fa3f3e93bc555b78edb55790de66997ccb1
7
+ data.tar.gz: 51086ae900df5c26bc8fc5d64c8627f04614bef94662c3771ee050b9864f9b53aa9677aaee17348374939d0d9079fa4616fa09f7f61c66e22ba4af777d7cab94
@@ -1,98 +1,80 @@
1
1
  require 'hashie'
2
2
 
3
- # Idee: If-Match wäre bei .delete_object und .put_object ein interessantes
4
- # Feature.
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
- class Resterl::BaseObject #< Hashie::Mash
10
+ def initialize data = {}, response = nil
11
+ @data = Hashie::Mash.new data
12
+ @response = response
13
+ end
7
14
 
8
- include ClassLevelInheritableAttributes
9
- cattr_inheritable :resterl_client, :parser, :composer, :complete_mime_type,
10
- :mapper
11
- attr_reader :response
15
+ def method_missing sym, *args, &block
16
+ @data.send sym, *args, &block
17
+ end
12
18
 
13
- #self.resterl_client = nil
14
- #self.complete_mime_type = 'text/plain'
19
+ def respond_to? sym
20
+ super || @data.respond_to?(sym)
21
+ end
15
22
 
16
- def initialize data = {}, response = nil
17
- @data = Hashie::Mash.new data
18
- @response = response
19
- end
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
- def method_missing sym, *args, &block
22
- # Vielleicht lassen sich auch feste Attribute delegieren, das wäre als
23
- # method_missing.
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
- protected
33
+ def delete_object url
34
+ resterl_client.delete(url, {}, accept_header, {})
35
+ end
28
36
 
29
- def self.get_object url, params = {}
30
- headers = { 'Accept' => complete_mime_type }
31
- response = resterl_client.get(url, params, headers)
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
- headers = {
42
- 'Accept' => complete_mime_type,
43
- 'Content-Type' => complete_mime_type
44
- }
45
- data = composer.call(data)
46
- response = resterl_client.post(url, params, data, headers)
47
-
48
- doc = response.body
49
- doc = parser.call(doc)
50
- doc = mapper.map(doc) if @mapper
51
- new(doc, response)
52
- end
53
-
54
- def self.delete_object url
55
- headers = {
56
- 'Accept' => complete_mime_type,
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
- def self.mime_type= t
82
- self.parser, self.composer, self.complete_mime_type = case t
83
- when :json
84
- # TODO prio 2: Only works when Rails is loaded?
85
- [ proc {|str| JSON.parse(str)},
86
- proc(&:to_json),
87
- 'application/json'
88
- ]
89
- when :xml
90
- # TODO prio 2: Only works when Rails is loaded?
91
- [ proc {|str| Hash.from_xml(str)},
92
- proc(&:to_xml),
93
- 'application/xml'
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
@@ -1,3 +1,3 @@
1
1
  module Resterl
2
- VERSION = '0.0.17'
2
+ VERSION = '0.0.18'
3
3
  end
@@ -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.17
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-08-06 00:00:00.000000000 Z
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.2
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
@@ -1,7 +0,0 @@
1
- * Edit README.rdoc
2
- * Tests!
3
- * Use technoweenie's faraday as base (http://github.com/technoweenie/faraday)
4
- * Secure caching with tests
5
- * Store parsed objects in cache (instead of the plain response)
6
- * Persistent connections
7
- * Documentation