crossbar-http 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9530ff099336cf9bdd53fef13a14a4bbd300d4f7
4
+ data.tar.gz: 1f388882acb44ab8a45cf04d2f4a2c1ff3023545
5
+ SHA512:
6
+ metadata.gz: 549fbec78e190dee8cc82aa7817865979c609e6e759a2b12d9cf9f2fb77cb7292ce9d9fcb8e4e2772bf256aa44ea9900fcb94fdaca2f6862bc95155d704fe586
7
+ data.tar.gz: b8c76b295948e7c8f814b04f94ad5c12085338cf43109f53ecd7dc98f9912188e8c602857e0130897671c7a2c8e4bef72f0217c9abd723a2718e2939603addab
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.13.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in crossbar_http.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Eric Chapman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # Crossbar HTTP
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/crossbar-http.svg)](https://badge.fury.io/rb/crossbar-http)
4
+
5
+ Module that provides methods for accessing Crossbar.io HTTP Bridge Services
6
+
7
+ ## Revision History
8
+
9
+ - v0.1:
10
+ - Initial version
11
+
12
+ ## Installation
13
+
14
+ ``` ruby
15
+ gem 'crossbar-http'
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ### Call
21
+ To call a Crossbar HTTP bridge, do the following
22
+
23
+ ``` ruby
24
+ client = Crossbar::HTTP::Client("http://127.0.0.1/call")
25
+ result = client.call("com.example.add", 2, 3, offset=10)
26
+ ```
27
+
28
+ This will call the following method
29
+
30
+ ``` python
31
+ def onJoin(self, details):
32
+
33
+ def add_something(x, y, offset=0):
34
+ print "Add was called"
35
+ return x+y+offset
36
+
37
+ self.register(add_something, "com.example.add")
38
+ ```
39
+
40
+ ### Publish
41
+ To publish to a Crossbar HTTP bridge, do the following
42
+
43
+ ``` ruby
44
+ client = Crossbar::HTTP::Client("http://127.0.0.1/publish")
45
+ result = client.publish("com.example.event", event="new event")
46
+ ```
47
+
48
+ The receiving subscription would look like
49
+
50
+ ``` python
51
+ def onJoin(self, details):
52
+
53
+ def subscribe_something(event=None, **kwargs):
54
+ print "Publish was called with event %s" % event
55
+
56
+ self.subscribe(subscribe_something, "com.example.event")
57
+ ```
58
+
59
+ ### Key/Secret
60
+ For bridge services that have a key and secret defined, simply include the key and secret in the instantiation of the
61
+ client.
62
+
63
+ ``` ruby
64
+ client = Crossbar::HTTP::Client("http://127.0.0.1/publish", key: "key", secret: "secret")
65
+ ```
66
+
67
+ ## Contributing
68
+ To contribute, fork the repo and submit a pull request.
69
+
70
+ ## Testing
71
+ TODO
72
+
73
+ ##License
74
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "crossbar_http"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'crossbar-http/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'crossbar-http'
8
+ spec.version = Crossbar::HTTP::VERSION
9
+ spec.authors = ['Eric Chapman']
10
+ spec.email = ['eric.chappy@gmail.com']
11
+
12
+ spec.summary = %q{Crossbar.io HTTP Bridge Ruby Client}
13
+ spec.description = %q{}
14
+ spec.homepage = 'https://github.com/ericchapman/ruby-crossbar-http'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.13'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ end
@@ -0,0 +1,155 @@
1
+ =begin
2
+
3
+ Copyright (c) 2017 Eric Chapman
4
+
5
+ MIT License
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ "Software"), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ =end
27
+
28
+ require 'openssl'
29
+ require 'base64'
30
+ require 'net/http'
31
+ require 'json'
32
+
33
+ module Crossbar
34
+ module HTTP
35
+ class Client
36
+ attr_accessor :url, :key, :secret, :verbose, :sequence
37
+
38
+ # Initializes the client
39
+ # @param url [String] - The url of the router's HTTP bridge
40
+ # @param key [String] - The key (optional)
41
+ # @param secret [String] - The secret (optional)
42
+ # @param verbose [Bool] - 'True' if you want debug messages printed
43
+ def initialize(url, key: nil, secret: nil, verbose: false)
44
+
45
+ raise 'The url can not be nil' unless url != nil
46
+
47
+ self.url = url
48
+ self.key = key
49
+ self.secret = secret
50
+ self.verbose = verbose
51
+ self.sequence = 1
52
+ end
53
+
54
+ # Publishes to the topic
55
+ # @param topic [String] - The name of the topic
56
+ # @param *args [] - The arguments and key word arguments
57
+ # @return [Integer] - Returns the id of the publish
58
+ def publish(topic, *args, **kwargs)
59
+ raise 'The topic can not be nil' unless url != nil
60
+
61
+ params = {
62
+ topic: topic,
63
+ args: args,
64
+ kwargs: kwargs
65
+ }
66
+
67
+ self._make_api_call(params)
68
+ end
69
+
70
+ # Calls the procedure
71
+ # @param procedure [String] - The name of the topic
72
+ # @param *args [] - The arguments and key word arguments
73
+ # @return [Integer] - Returns the id of the publish
74
+ def call(procedure, *args, **kwargs)
75
+ raise 'The topic can not be nil' unless url != nil
76
+
77
+ params = {
78
+ procedure: procedure,
79
+ args: args,
80
+ kwargs: kwargs
81
+ }
82
+
83
+ self._make_api_call(params)
84
+ end
85
+
86
+ # Computes the signature.
87
+ # Described at: http://crossbar.io/docs/HTTP-Bridge-Services-Caller/
88
+ # Reference code is at: https://github.com/crossbario/crossbar/blob/master/crossbar/adapter/rest/common.py
89
+ # @param body [Hash]
90
+ # @return (signature, nonce, timestamp)
91
+ def _compute_signature(body, nonce: nil, timestamp: nil)
92
+
93
+ timestamp ||= Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
94
+ nonce ||= rand(0..2**53)
95
+
96
+ # Compute signature: HMAC[SHA256]_{secret} (key | timestamp | seq | nonce | body) => signature
97
+ hm = OpenSSL::HMAC.new(self.secret, OpenSSL::Digest::SHA256.new)
98
+ hm << self.key
99
+ hm << timestamp
100
+ hm << self.sequence.to_s
101
+ hm << nonce.to_s
102
+ hm << body
103
+ signature = Base64.urlsafe_encode64(hm.digest)
104
+
105
+ return signature, nonce, timestamp
106
+ end
107
+
108
+ def _make_api_call(json_params=nil)
109
+
110
+ puts "Crossbar::HTTP - Request: POST #{url}" if self.verbose
111
+
112
+ encoded_params = (json_params != nil) ? JSON.generate(json_params) : nil
113
+
114
+ puts "Crossbar::HTTP - Params: #{encoded_params}" if encoded_params != nil and self.verbose
115
+
116
+ uri = URI(self.url)
117
+
118
+ if self.key != nil and self.secret != nil and encoded_params != nil
119
+ signature, nonce, timestamp = self._compute_signature(encoded_params)
120
+ params = {
121
+ timestamp: timestamp,
122
+ seq: self.sequence.to_s,
123
+ nonce: nonce,
124
+ signature: signature,
125
+ key: self.key
126
+ }
127
+ uri.query = URI.encode_www_form(params)
128
+
129
+ puts "Crossbar::HTTP - Signature Params: #{params}" if self.verbose
130
+ end
131
+
132
+ # TODO: Not sure what this is supposed to be but this works
133
+ self.sequence += 1
134
+
135
+ # Create the request
136
+ req = Net::HTTP::Post.new(uri)
137
+ req['Content-Type'] = 'application/json'
138
+ req.body = encoded_params
139
+
140
+ res = Net::HTTP.start(uri.hostname, uri.port) do |http|
141
+ http.request(req)
142
+ end
143
+
144
+ case res
145
+ when Net::HTTPSuccess
146
+ puts "Crossbar::HTTP - Response Body: #{res.body}" if self.verbose
147
+ return JSON.parse(res.body, {:symbolize_names => true})
148
+ else
149
+ raise "Crossbar::HTTP - Code: #{res.code}, Error: #{res.message}"
150
+ end
151
+
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,5 @@
1
+ module Crossbar
2
+ module HTTP
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ =begin
2
+
3
+ Copyright (c) 2017 Eric Chapman
4
+
5
+ MIT License
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ "Software"), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ =end
27
+
28
+ require 'crossbar-http/version'
29
+ require 'crossbar-http/client'
30
+
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crossbar-http
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eric Chapman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-22 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: ''
56
+ email:
57
+ - eric.chappy@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - crossbar-http.gemspec
72
+ - lib/crossbar-http.rb
73
+ - lib/crossbar-http/client.rb
74
+ - lib/crossbar-http/version.rb
75
+ homepage: https://github.com/ericchapman/ruby-crossbar-http
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.5.1
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Crossbar.io HTTP Bridge Ruby Client
99
+ test_files: []