preact-rpc-client 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/preact_rpc_client.rb +68 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd757cf82361c1895b8fbe9c7f755eec604f8048
4
+ data.tar.gz: 0daf25eda6ad8a23dbcefc0b352218d3bd1bb960
5
+ SHA512:
6
+ metadata.gz: 78872bc7a26c6c3212fad88d8efcb6eac8100868059b8b0716798a4277b2f92df0bea82329db28fe37866fcbb5f9dc6a6647bb6af51e611f555404f9b6eac74e
7
+ data.tar.gz: 38fb21bcfaf842dee1d92a499232318390664d2849e70741641d050b2cc2f199f50b8cc60b548818807c31abff20513860c7ea939835081d505a02313f030c27
@@ -0,0 +1,68 @@
1
+ require 'json'
2
+ require 'socket'
3
+
4
+ # A module that wraps socket server interaction with the preact-rpc server
5
+ module PreactRpcClient
6
+ extend self
7
+
8
+ # Default configuration
9
+ @max_data_buffer_size = 2048
10
+ @data_end_marker = "\r\n."
11
+
12
+ # Configure block
13
+ class << self
14
+ attr_accessor :data_end_marker, :socket_type, :socket_file, :server_host, :server_port
15
+ def configure
16
+ yield self
17
+ @sock = nil
18
+ end
19
+ end
20
+
21
+ # Render function
22
+ def render_component(component_name, props)
23
+ # Construct payload JSON
24
+ payload = {
25
+ id: next_request_id,
26
+ component: component_name,
27
+ props: props,
28
+ }.to_json + @data_end_marker
29
+
30
+ # Send request to server
31
+ conn.send(payload, 0)
32
+
33
+ # Receive response
34
+ resp = ""
35
+ while true
36
+ val = conn.recv(@max_data_buffer_size)
37
+ is_end = val.match(@data_end_marker)
38
+ val = val.split(@data_end_marker).first if is_end
39
+ resp += val
40
+ break if is_end
41
+ end
42
+
43
+ # Parse JSON response and return
44
+ begin
45
+ return JSON.parse(resp)
46
+ rescue
47
+ return {
48
+ error: 'Invalid server response'
49
+ }
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def conn
56
+ if @socket_type == 'unix'
57
+ @sock ||= UNIXSocket.new @socket_file
58
+ else
59
+ @sock ||= TCPSocket.new @server_host, @server_port
60
+ end
61
+ return @sock
62
+ end
63
+
64
+ def next_request_id
65
+ @req_id ||= 0
66
+ @req_id += 1
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: preact-rpc-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Musawir Shah
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This gem consists of helper functions to interact with the preact-rpc
14
+ server
15
+ email: musawir@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/preact_rpc_client.rb
21
+ homepage: http://rubygems.org/gems/preact-rpc-client
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.0.14.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Ruby client for the preact-rpc server
45
+ test_files: []