io_request 1.0.0 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6ba3ef4cb967a230cfb350a7413cee8868a2817f5451134105c7ba075470427
4
- data.tar.gz: 1438ae77d9f2db624bbf8596d2b4acbf793aa1826bcc9a6e0f83dfe8fde3e541
3
+ metadata.gz: da825a2b5c4621543a1c66625eebf871852a07a5ed51db4fa3df51a26ed23874
4
+ data.tar.gz: a47015fa8c95cad7baadf55d0ef43921272c9fa4898d4a78f238b6f48ca15842
5
5
  SHA512:
6
- metadata.gz: e1cc15e03f5489653ec07ce7d7424992ee259b508124972f84fe8c2f10bf08465a59ebebd25dc30cba5be8012c583c2028b4c22332882e13e575903c0898e7af
7
- data.tar.gz: a648b296f3b12c90205253bd40ce906b12a5638b892113bfdcf54deedaed8a41d5262c5f4937edc28509de862ce52c91429f450c40a8b97c60acdcf8afdec651
6
+ metadata.gz: 9a4ccf24e708cfe1bf1d428f780be958a54516dd57dbfc25b450eba57b4ea111dfcc09f88a1fdfefe12eb55bf450541665f3e0a3eb3458406c9958d843f8af3b
7
+ data.tar.gz: 044ff5beb060aa71aa95971f3d4ad6eb2b0dab452a3c2df16460adafc8adae690a8d13f2a9993dcf32d43020abd03cbc041ac23d7aec97674247b12aead5bed7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- io_request (1.0.0)
4
+ io_request (1.1.0)
5
5
  json (~> 2.0)
6
6
  logger (~> 1.4)
7
7
  timeout-extensions (~> 0.1.1)
data/README.md CHANGED
@@ -20,7 +20,9 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ Documentation is available at https://www.rubydoc.info/gems/io_request
24
+
25
+ Some examples could be find in *examples* folder.
24
26
 
25
27
  ## Development
26
28
 
@@ -0,0 +1,52 @@
1
+ require "io_request"
2
+
3
+ r1, w1 = IO.pipe
4
+ r2, w2 = IO.pipe
5
+
6
+ client_1 = IORequest::Client.new read: r1, write: w2
7
+ client_2 = IORequest::Client.new read: r2, write: w1
8
+
9
+ # Use
10
+ # Set up responders
11
+ # Authorization
12
+ client_2.respond type: "auth" do |request|
13
+ puts "Client 2: Authorization attempt as #{request.data[:username].inspect}"
14
+ sleep 2 # Some processing
15
+ { type: "auth_success" }
16
+ end
17
+
18
+ # Default
19
+ client_2.respond do |request|
20
+ puts "Client 2: #{request.data.inspect}"
21
+ { type: "success" }
22
+ end
23
+
24
+ # Send requests
25
+ auth = false
26
+ auth_request = client_1.request(
27
+ data: { type: "auth", username: "mymail@example.com", password: "let's pretend password hash is here" },
28
+ sync: true
29
+ ) do |response|
30
+ unless response.data[:type] == "auth_success"
31
+ puts "Client 1: Authorization failed. Response: #{response.data.inspect}"
32
+ next
33
+ end
34
+
35
+ auth = true
36
+ # Do something
37
+ end
38
+ exit unless auth
39
+ puts "Client 1: Authorized!"
40
+
41
+ message = client_1.request(
42
+ data: { type: "message", message: "Hello!" },
43
+ sync: true
44
+ ) do |response|
45
+ puts "Client 1: Message responded"
46
+ end
47
+
48
+ # Close
49
+ r1.close
50
+ w1.close
51
+ r2.close
52
+ w2.close
@@ -8,9 +8,6 @@ module IORequest
8
8
  include Utility::WithProgName
9
9
  include Utility::MultiThread
10
10
 
11
- # IO-like object provided at initialization.
12
- attr_reader :io
13
-
14
11
  # Initialize new client over IO.
15
12
  #
16
13
  # @option options [:gets] read IO to read from.
@@ -31,8 +28,7 @@ module IORequest
31
28
  #
32
29
  # Optional block can be provided. It will be called when response received.
33
30
  #
34
- # @param data [Hash] data to send with request.
35
- #
31
+ # @option options [Hash] data data to send.
36
32
  # @option options [Boolean] sync whether to join request after sending.
37
33
  # @option options [Integer, Float] timeout timeout for {Request#join}.
38
34
  #
@@ -42,6 +38,7 @@ module IORequest
42
38
  def request(data: {}, sync: false, timeout: nil, &block)
43
39
  req = Request.new(data)
44
40
  @out_requests[req] = block
41
+ IORequest.debug("Sending request ##{req.id}", prog_name)
45
42
  send(req.to_hash)
46
43
  req.join(timeout) if sync
47
44
  req
@@ -85,12 +82,18 @@ module IORequest
85
82
  in_thread do
86
83
  responder = find_responder(req)
87
84
  data = nil
88
- begin
89
- data = responder.call(req) if responder
85
+ data = begin
86
+ if responder
87
+ responder.call(req)
88
+ else
89
+ IORequest.warn "Responder not found!"
90
+ nil
91
+ end
90
92
  rescue Exception => e
91
- IORequest.warn("Provided block raised exception:\n#{e.full_message}", prog_name)
93
+ IORequest.warn "Provided block raised exception:\n#{e.full_message}", prog_name
94
+ nil
92
95
  end
93
- data = {} unless data.is_a?(Hash)
96
+ data ||= {}
94
97
  res = Response.new(data, req)
95
98
  send(res.to_hash)
96
99
  end
@@ -121,15 +124,22 @@ module IORequest
121
124
 
122
125
  # find responder for provided request.
123
126
  def find_responder(req)
127
+ result = nil
124
128
  @responders.each do |subdata, block|
125
- break block if req.data.contains? subdata
129
+ if req.data.contains? subdata
130
+ result = block
131
+ break
132
+ end
126
133
  end
134
+
135
+ result
127
136
  end
128
137
 
129
138
  # Send data.
130
139
  #
131
140
  # @param [Hash]
132
141
  def send(data)
142
+ IORequest.debug("Sending hash: #{data.inspect}", prog_name)
133
143
  send_raw(encode(data_to_string data))
134
144
  end
135
145
 
@@ -1,4 +1,4 @@
1
1
  module IORequest
2
2
  # Gem version.
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: io_request
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fizvlad
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-13 00:00:00.000000000 Z
11
+ date: 2019-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -109,6 +109,7 @@ files:
109
109
  - README.md
110
110
  - Rakefile
111
111
  - bin/console
112
+ - examples/simple_example.rb
112
113
  - io_request.gemspec
113
114
  - lib/io_request.rb
114
115
  - lib/io_request/client.rb