async-rest 0.8.2 → 0.9.0

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
  SHA256:
3
- metadata.gz: 0d1256dc24f29fc1332acf1548cf62f8e2f4bf293e1ccf34f36f83683b518716
4
- data.tar.gz: 2a463fee417542e3b29f63f4bf1b77e35ffc11d8402f426e15e5860d297a4c10
3
+ metadata.gz: b63889027214f114310b9fe8f9a2996f11c56e0f42a6b796312cec368de8c460
4
+ data.tar.gz: a054ae31432301990edc8399321dc1c678567770917afacb16f3d7ddb2ed12ec
5
5
  SHA512:
6
- metadata.gz: eab724509c865977deec20a08f5ba15fadea3948a02d98f65795874dca94b7e966237b86e85780244b03e8d05cb63bfd18d025dc7092398830c48f8cfa7fee26
7
- data.tar.gz: 9a1131bb6b4062c8db19c43c9c2e334dfe0465744dbc41c822125288c7fcb7b20558191902ae8439cdfc0950183b4fbb10b4a9349c6311060fdcc8650f6163fb
6
+ metadata.gz: 8952cfb675125f2349d71e3138211f63109ba793ff0a5062a7ecb11cf03a1379a04a6c8362ffb0605019a88a350b8bf5a0ebeaf9f281ad495d5d3567c1350d90
7
+ data.tar.gz: 86b0ba32227b6154ad13a2d43fff29d37608b670adc61c0fcc5e442077e20c21c8326959a58cd22fe8c566fcce02bf82ddceaf25e762fbdc2fca0e71f007daae
@@ -22,5 +22,8 @@ module Async
22
22
  module REST
23
23
  class Error < StandardError
24
24
  end
25
+
26
+ class RequestFailure < Error
27
+ end
25
28
  end
26
29
  end
@@ -24,9 +24,6 @@ require_relative 'wrapper/json'
24
24
 
25
25
  module Async
26
26
  module REST
27
- class RequestFailure < Error
28
- end
29
-
30
27
  # REST components perform actions on a resource by using a representation to capture the current or intended state of that resource and transferring that representation between components. A representation is a sequence of bytes, plus representation metadata to describe those bytes. Other commonly used but less precise names for a representation include: document, file, and HTTP message entity, instance, or variant.
31
28
  #
32
29
  # A representation consists of data, metadata describing the data, and, on occasion, metadata to describe the metadata (usually for the purpose of verifying message integrity). Metadata is in the form of name-value pairs, where the name corresponds to a standard that defines the value's structure and semantics. Response messages may include both representation metadata and resource metadata: information about the resource that is not specific to the supplied representation.
@@ -47,8 +44,12 @@ module Async
47
44
  @value = value
48
45
  end
49
46
 
50
- def with(**parameters)
51
- self.class.new(@resource.with(parameters: parameters), wrapper: @wrapper)
47
+ def with(klass = self.class, **options)
48
+ klass.new(@resource.with(**options), wrapper: @wrapper)
49
+ end
50
+
51
+ def [] **parameters
52
+ self.with(parameters: parameters)
52
53
  end
53
54
 
54
55
  def close
@@ -87,22 +88,42 @@ module Async
87
88
  @metadata = response.headers
88
89
  @value = response.read
89
90
  else
90
- raise RequestFailure, "Could not fetch remote resource #{@resource}: #{response.status}!"
91
+
91
92
  end
92
93
  end
93
94
 
95
+ def value?
96
+ !@value.nil?
97
+ end
98
+
94
99
  def value
95
100
  @value ||= value!
96
101
  end
97
102
 
98
103
  def value= value
99
- if @value = value
104
+ @value = self.assign(value)
105
+ end
106
+
107
+ def call(value)
108
+ if value
100
109
  self.post(value)
101
110
  else
102
111
  self.delete
103
112
  end
104
113
  end
105
114
 
115
+ def assign(value)
116
+ response = self.call(value)
117
+
118
+ response.read
119
+
120
+ return @value
121
+ end
122
+
123
+ def update
124
+ @value = assign(@value)
125
+ end
126
+
106
127
  def inspect
107
128
  "\#<#{self.class} #{@resource.inspect}: value=#{@value.inspect}>"
108
129
  end
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Async
22
22
  module REST
23
- VERSION = "0.8.2"
23
+ VERSION = "0.9.0"
24
24
  end
25
25
  end
@@ -0,0 +1,40 @@
1
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Async
22
+ module REST
23
+ module Wrapper
24
+ class Generic
25
+ # @param payload [Object] a request payload to send.
26
+ # @param headers [Protocol::HTTP::Headers] the mutable HTTP headers for the request.
27
+ # @return [Body | nil] an optional request body based on the given payload.
28
+ def prepare_request(payload, headers)
29
+ end
30
+
31
+ # @param request [Protocol::HTTP::Request] the request that was made.
32
+ # @param response [Protocol::HTTP::Response] the response that was received.
33
+ # @return [Object] some application specific representation of the response.
34
+ def process_response(request, response)
35
+ return response
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -23,10 +23,12 @@ require 'json'
23
23
  require 'protocol/http/body/wrapper'
24
24
  require 'protocol/http/body/buffered'
25
25
 
26
+ require_relative 'generic'
27
+
26
28
  module Async
27
29
  module REST
28
30
  module Wrapper
29
- class JSON
31
+ class JSON < Generic
30
32
  APPLICATION_JSON = "application/json".freeze
31
33
  APPLICATION_JSON_STREAM = "application/json; boundary=NL".freeze
32
34
 
@@ -53,25 +55,29 @@ module Async
53
55
  end
54
56
  end
55
57
 
58
+ class Parser < HTTP::Body::Wrapper
59
+ def join
60
+ ::JSON.parse(super, symbolize_names: true)
61
+ end
62
+ end
63
+
64
+ def wrap_response(response)
65
+ if body = response.body
66
+ response.body = Parser.new(body)
67
+ end
68
+ end
69
+
56
70
  def process_response(request, response)
57
71
  if content_type = response.headers['content-type']
58
72
  if content_type.start_with? @content_type
59
- if body = response.body
60
- response.body = Parser.new(body)
61
- end
73
+ wrap_response(response)
62
74
  else
63
- warn "Unknown content type: #{content_type}!"
75
+ raise Error, "Unknown content type: #{content_type}!"
64
76
  end
65
77
  end
66
78
 
67
79
  return response
68
80
  end
69
-
70
- class Parser < HTTP::Body::Wrapper
71
- def join
72
- ::JSON.parse(super, symbolize_names: true)
73
- end
74
- end
75
81
  end
76
82
  end
77
83
  end
@@ -23,10 +23,12 @@ require 'json'
23
23
  require 'protocol/http/body/wrapper'
24
24
  require 'protocol/http/body/buffered'
25
25
 
26
+ require_relative 'generic'
27
+
26
28
  module Async
27
29
  module REST
28
30
  module Wrapper
29
- class URLEncoded
31
+ class URLEncoded < Generic
30
32
  APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded".freeze
31
33
 
32
34
  def initialize(content_type = APPLICATION_FORM_URLENCODED)
@@ -51,25 +53,29 @@ module Async
51
53
  end
52
54
  end
53
55
 
56
+ class Parser < ::Protocol::HTTP::Body::Wrapper
57
+ def join
58
+ ::Protocol::HTTP::URL.decode(super, symbolize_keys: true)
59
+ end
60
+ end
61
+
62
+ def wrap_response(response)
63
+ if body = response.body
64
+ response.body = Parser.new(body)
65
+ end
66
+ end
67
+
54
68
  def process_response(request, response)
55
69
  if content_type = response.headers['content-type']
56
70
  if content_type.start_with? @content_type
57
- if body = response.body
58
- response.body = Parser.new(body)
59
- end
71
+ wrap_response(response)
60
72
  else
61
- warn "Unknown content type: #{content_type}!"
73
+ raise Error, "Unknown content type: #{content_type}!"
62
74
  end
63
75
  end
64
76
 
65
77
  return response
66
78
  end
67
-
68
- class Parser < ::Protocol::HTTP::Body::Wrapper
69
- def join
70
- ::Protocol::HTTP::URL.decode(super, symbolize_keys: true)
71
- end
72
- end
73
79
  end
74
80
  end
75
81
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-31 00:00:00.000000000 Z
11
+ date: 2019-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async-http
@@ -129,6 +129,7 @@ files:
129
129
  - lib/async/rest/representation.rb
130
130
  - lib/async/rest/resource.rb
131
131
  - lib/async/rest/version.rb
132
+ - lib/async/rest/wrapper/generic.rb
132
133
  - lib/async/rest/wrapper/json.rb
133
134
  - lib/async/rest/wrapper/url_encoded.rb
134
135
  homepage: https://github.com/socketry/async-rest