json_rpc_over_mqtt_example 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 89147978eb0cef9761d2e9ea890eb248ac998c92
4
+ data.tar.gz: 82e471a263fdfc13e93888b8b71e41c37c15ccf8
5
+ SHA512:
6
+ metadata.gz: b4c4cbc3755f910c80ad37f93da93b822b4b98405e81bc7493cea8c1c0a13e41af04a77f4e4882ed6919087ea6ae8f54e872ef37cc79fb2b92f20f3235857923
7
+ data.tar.gz: 25fb71508adc4297b620ed60acbe616b15b213bab1af297e0001af0022705d6ebcf340c821c64545604d250a5b5a0c6ded9aa8ed3ad5947d64d9645237cf0612
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ vendor/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 futoase
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,112 @@
1
+ # JSON-RPC over MQTT example
2
+
3
+ JSON-RPC over MQTT example
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ > gem 'json_rpc_over_mqtt_example'
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Setup of the MQTT Broker in your machine.
14
+ Mosquitto is better.
15
+
16
+ ### Setup of the MQTT Broker
17
+
18
+ - Mac OS X (homebrew)
19
+
20
+ Install
21
+
22
+ ```
23
+ > brew install mosquitto
24
+ ```
25
+
26
+ Starting MQTT Broker
27
+
28
+ ```
29
+ > launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mosquitto.plist
30
+ ```
31
+
32
+ ### Connect MQTT subscriber to MQTT Broker
33
+
34
+ ```
35
+ > mosquitto_sub -h localhost -t client
36
+ ```
37
+
38
+ ### Starting demo server
39
+
40
+ Starting JSON-RPC over MQTT example demo server.
41
+
42
+ ```
43
+ > json_rpc_over_mqtt_server
44
+ ```
45
+
46
+ ### Publish message to MQTT Broker
47
+
48
+ Publish message to a MQTT Broker.
49
+
50
+ - ping
51
+
52
+ Publisher
53
+
54
+ ```
55
+ > mosquitto_pub -h localhost -t server -m '{"jsonrpc": "2.0", "id": 2, "method": "ping", "params": {"url": "yahoo.co.jp"}}'
56
+ ```
57
+
58
+ Subscriber
59
+
60
+ ```
61
+ > {"jsonrpc":"2.0","id":2,"result":"OK: yahoo.co.jp"}
62
+ ```
63
+
64
+ - hello world
65
+
66
+ Publisher
67
+
68
+ ```
69
+ > mosquitto_pub -h localhost -t server -m '{"jsonrpc": "2.0", "id": 1000, "method": "hello_world", "params": {"name": "Bob"}}'
70
+ ```
71
+
72
+ Subscriber
73
+
74
+ ```
75
+ > {"jsonrpc":"2.0","id":1000,"result":"Hello World, Bob!"}
76
+ ```
77
+
78
+ - what time is it
79
+
80
+ Publisher
81
+
82
+ ```
83
+ > mosquitto_pub -h localhost -t server -m '{"jsonrpc": "2.0", "id": 1000, "method": "what_time_is_it?"}'
84
+ ```
85
+
86
+ Subscriber
87
+
88
+ ```
89
+ > {"jsonrpc":"2.0","id":1000,"result":"2014-12-31T09:36:47+09:00"}
90
+ ```
91
+
92
+ ## environments variable
93
+
94
+ - Broker address
95
+
96
+ **BROKER_ADDRESS**
97
+
98
+ - topic name of server
99
+
100
+ **SERVER_TOPIC_NAME**
101
+
102
+ - topic name of client
103
+
104
+ **CLIENT_TOPIC_NAME**
105
+
106
+ ## Contributing
107
+
108
+ 1. Fork it ( https://github.com/futoase/json_rpc_over_mqtt_example/fork )
109
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
110
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
111
+ 4. Push to the branch (`git push origin my-new-feature`)
112
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,5 @@
1
+ require 'json_rpc_over_mqtt_example'
2
+
3
+ logger.info("Starting the JSONRPC over MQTT server")
4
+
5
+ JSONRPCOverMQTT::MQTT::Connect::start
@@ -0,0 +1,5 @@
1
+ module JSONRPCOverMQTT
2
+ BROKER_ADDRESS = ENV['BROKER_ADDRESS'] || 'localhost'
3
+ SERVER_TOPIC_NAME = ENV['SERVER_TOPIC_NAME'] || 'server'
4
+ CLIENT_TOPIC_NAME = ENV['CLIENT_TOPIC_NAME'] || 'client'
5
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'json_rpc_over_mqtt_example/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json_rpc_over_mqtt_example"
8
+ spec.version = JSONRPCOverMQTT::VERSION
9
+ spec.authors = ["futoase"]
10
+ spec.email = ["futoase@gmail.com"]
11
+ spec.summary = %q{JSONRPC Over MQTT Example.}
12
+ spec.description = %q{JSONRPC Over MQTT Example.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_dependency "mqtt"
25
+ spec.add_dependency "net-ping"
26
+ end
@@ -0,0 +1,13 @@
1
+ require 'json'
2
+ require 'mqtt'
3
+
4
+ require_relative File.expand_path(__dir__ + '/../config/const')
5
+ require_relative 'json_rpc_over_mqtt_example/logger'
6
+ require_relative 'json_rpc_over_mqtt_example/json_rpc'
7
+ require_relative 'json_rpc_over_mqtt_example/mqtt'
8
+
9
+ __my_dir__ = File.expand_path(__dir__)
10
+ functions_dir = File.expand_path(__my_dir__ + '/json_rpc_over_mqtt_example/functions/**/*.rb')
11
+
12
+ # Include functions
13
+ Dir.glob(functions_dir).map { |f| require_relative f }
@@ -0,0 +1,4 @@
1
+ def hello_world(**kwargs)
2
+ name = kwargs[:name] || "Bob"
3
+ "Hello World, #{name}!"
4
+ end
@@ -0,0 +1,8 @@
1
+ require 'net/ping'
2
+
3
+ def ping(**kwargs)
4
+ url = kwargs[:url] || "google.com"
5
+ target = Net::Ping::External.new(url)
6
+ result = target.ping? ? "OK" : "NG"
7
+ "#{result}: #{url}"
8
+ end
@@ -0,0 +1,5 @@
1
+ require 'date'
2
+
3
+ def what_time_is_it?
4
+ DateTime.now.to_s
5
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'json_rpc/error_response'
2
+ require_relative 'json_rpc/request'
3
+ require_relative 'json_rpc/response'
4
+
5
+ module JSONRPCOverMQTT
6
+ module JSONRPC
7
+ VERSION = "2.0"
8
+ end
9
+ end
@@ -0,0 +1,28 @@
1
+ module JSONRPCOverMQTT
2
+ module JSONRPC
3
+
4
+ class ErrorResponse
5
+
6
+ METHOD_NOT_FOUND = {code: "-32601", message: "Method not found"}
7
+ PARSE_ERROR = {code: "-32602", message: "JSON parse error"}
8
+
9
+ def self.generate_response(json_rpc_id, error_response)
10
+ ::JSON.generate({
11
+ jsonrpc: VERSION,
12
+ id: json_rpc_id,
13
+ error: error_response
14
+ })
15
+ end
16
+
17
+ def self.method_not_found(json_rpc_id)
18
+ generate_response(json_rpc_id, METHOD_NOT_FOUND)
19
+ end
20
+
21
+ def self.parse_error
22
+ generate_response(nil, PARSE_ERROR)
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ module JSONRPCOverMQTT
2
+ module JSONRPC
3
+
4
+ class Request
5
+
6
+ attr_reader :id, :method, :params
7
+
8
+ def initialize(message)
9
+ json_rpc_obj = JSON.parse(message)
10
+ @id = json_rpc_obj["id"]
11
+ @method = json_rpc_obj["method"]
12
+
13
+ if json_rpc_obj["params"].nil?
14
+ @params = nil
15
+ else
16
+ @params = Hash[json_rpc_obj["params"].map{ |k,v| [k.to_sym, v] }]
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module JSONRPCOverMQTT
2
+ module JSONRPC
3
+
4
+ class Response
5
+ def self.generate_response(json_rpc_id, response)
6
+ ::JSON.generate({
7
+ jsonrpc: VERSION,
8
+ id: json_rpc_id,
9
+ result: response
10
+ })
11
+ end
12
+
13
+ def self.generate(json_rpc_id, response)
14
+ generate_response(json_rpc_id, response)
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ require 'logger'
2
+
3
+ def logger
4
+ @log ||= Logger.new(STDOUT)
5
+ @log.level = Logger::INFO
6
+ @log
7
+ end
@@ -0,0 +1 @@
1
+ require_relative 'mqtt/connect'
@@ -0,0 +1,41 @@
1
+ module JSONRPCOverMQTT
2
+ module MQTT
3
+
4
+ class Connect
5
+ def self.start
6
+
7
+ ::MQTT::Client.connect(BROKER_ADDRESS) do |c|
8
+ c.get(SERVER_TOPIC_NAME) do |topic, message|
9
+ logger.info("TOPIC: #{topic} MESSAGE:#{message}")
10
+
11
+ begin
12
+ request = JSONRPC::Request.new(message)
13
+
14
+ unless request.params.nil?
15
+ result = method(request.method).call(**request.params)
16
+ else
17
+ result = method(request.method).call
18
+ end
19
+
20
+ c.publish(
21
+ CLIENT_TOPIC_NAME,
22
+ JSONRPC::Response.generate(request.id, result)
23
+ )
24
+ rescue ::JSON::ParserError => e
25
+ c.publish(
26
+ CLIENT_TOPIC_NAME,
27
+ JSONRPC::ErrorResponse.parse_error
28
+ )
29
+ rescue NameError => e
30
+ c.publish(
31
+ CLIENT_TOPIC_NAME,
32
+ JSONRPC::ErrorResponse.method_not_found(request.id)
33
+ )
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module JSONRPCOverMQTT
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe JSONRPCOverMQTT::JSONRPC do
4
+
5
+ let(:error_response) { JSONRPCOverMQTT::JSONRPC::ErrorResponse }
6
+ let(:json_rpc_version) { '2.0' }
7
+ let(:json_rpc_id) { 100 }
8
+
9
+ context 'when generate method not found a not exists valid request' do
10
+ let(:method_not_found) { JSON.parse(error_response.method_not_found(json_rpc_id)) }
11
+ let(:error_code) { error_response::METHOD_NOT_FOUND[:code] }
12
+ let(:error_message) { error_response::METHOD_NOT_FOUND[:message] }
13
+ it "should be get response for method_not_found" do
14
+
15
+ expect(method_not_found).to be_instance_of(Hash)
16
+ expect(method_not_found).to include('jsonrpc')
17
+ expect(method_not_found).to include('id')
18
+ expect(method_not_found).to include('error')
19
+ expect(method_not_found["jsonrpc"]).to eq(json_rpc_version)
20
+ expect(method_not_found["id"]).to eq(json_rpc_id)
21
+ expect(method_not_found["error"]["code"]).to eq(error_code)
22
+ expect(method_not_found["error"]["message"]).to eq(error_message)
23
+ end
24
+ end
25
+
26
+ context 'when generate method not found a not exists valid request' do
27
+
28
+ let(:parse_error) { JSON.parse(error_response.parse_error) }
29
+ let(:error_code) { error_response::PARSE_ERROR[:code] }
30
+ let(:error_message) { error_response::PARSE_ERROR[:message] }
31
+
32
+ it "should be get response for parse_error" do
33
+
34
+ expect(parse_error).to be_instance_of(Hash)
35
+ expect(parse_error).to include('jsonrpc')
36
+ expect(parse_error).to include('id')
37
+ expect(parse_error).to include('error')
38
+ expect(parse_error["jsonrpc"]).to eq(json_rpc_version)
39
+ expect(parse_error["id"]).to eq(nil)
40
+ expect(parse_error["error"]["code"]).to eq(error_code)
41
+ expect(parse_error["error"]["message"]).to eq(error_message)
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,6 @@
1
+ require 'json'
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+ require 'json_rpc_over_mqtt_example/json_rpc'
5
+ require 'json_rpc_over_mqtt_example/mqtt_connect'
6
+ require 'json_rpc_over_mqtt_example/logger'
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_rpc_over_mqtt_example
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - futoase
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mqtt
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: net-ping
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: JSONRPC Over MQTT Example.
84
+ email:
85
+ - futoase@gmail.com
86
+ executables:
87
+ - json_rpc_over_mqtt_server
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/json_rpc_over_mqtt_server
99
+ - config/const.rb
100
+ - json_rpc_over_mqtt_example.gemspec
101
+ - lib/json_rpc_over_mqtt_example.rb
102
+ - lib/json_rpc_over_mqtt_example/functions/hello_world.rb
103
+ - lib/json_rpc_over_mqtt_example/functions/ping.rb
104
+ - lib/json_rpc_over_mqtt_example/functions/what_time_is_it.rb
105
+ - lib/json_rpc_over_mqtt_example/json_rpc.rb
106
+ - lib/json_rpc_over_mqtt_example/json_rpc/error_response.rb
107
+ - lib/json_rpc_over_mqtt_example/json_rpc/request.rb
108
+ - lib/json_rpc_over_mqtt_example/json_rpc/response.rb
109
+ - lib/json_rpc_over_mqtt_example/logger.rb
110
+ - lib/json_rpc_over_mqtt_example/mqtt.rb
111
+ - lib/json_rpc_over_mqtt_example/mqtt/connect.rb
112
+ - lib/json_rpc_over_mqtt_example/version.rb
113
+ - spec/json_rpc_spec.rb
114
+ - spec/spec_helper.rb
115
+ homepage: ''
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.4.5
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: JSONRPC Over MQTT Example.
139
+ test_files:
140
+ - spec/json_rpc_spec.rb
141
+ - spec/spec_helper.rb