soar_transport_api 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: 423f7a78f84a806469883367022491e9cbf44181
4
+ data.tar.gz: a93ed9eababf76596cc4289464426bba3aaf9be5
5
+ SHA512:
6
+ metadata.gz: 7f21de49f6a5ed4cdbe9a3658f08f0cf6f00ebefca959ba52cf4823b20932b484a71157fdfcd1e9abc0b493a63f2c04fbba8788c732bcc3d8a024e88edeef6c8
7
+ data.tar.gz: d80bbe1a24b2f56e08f21d3f35e2d47982fe7a6b83e001e8919d93e536934dc6399484f06f730a8712947b3cbf7e2f875da898a34270faf7b32c8a1481a7e018
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+ *.swp
12
+ *.swo
13
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ soar_transport_api
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.2
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.1
5
+ before_install: gem install bundler -v 1.12.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in soar_transport_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ernst Van Graan
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,129 @@
1
+ # SoarTransportApi
2
+
3
+ This gem specifies the API to be implemented by transport providers that want to communicate across soar_comms communication fabric. The API allows for message identification, sending and receiving, both synchronously and asynchronously. Messages are simple Hash objects with formatting to be dictated by the transport provider and consumer.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'soar_transport_api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install soar_transport_api
20
+
21
+ ## Usage
22
+
23
+ ### Building a provider
24
+
25
+ Extend SoarTransportAPI::TransportAPI and override the methods of interest. For example, for a synchronous HTTP transport provider:
26
+
27
+ ```
28
+ class HttpTransportApi < SoarTransportApi::TransportAPI
29
+ attr_accessor :server
30
+ @received
31
+
32
+ def initialize(transport_identifier)
33
+ super(transport_identifier)
34
+ @@received = []
35
+ end
36
+
37
+ def send_message(uri, message)
38
+ uri = URI.parse(uri)
39
+ response = Net::HTTP.post_form(uri, {"message" => message})
40
+ @@received.push(response)
41
+ response
42
+ end
43
+
44
+ def receive_message
45
+ message = @@received.pop
46
+ message.body
47
+ end
48
+ end
49
+ ```
50
+
51
+ ### Sending messages
52
+
53
+ Use the API as below to send messages both in the synchronous and asynchronous cases:
54
+
55
+ ```
56
+ message = { 'body' => 'This is a message' }
57
+ provider = HttpTransportApi.new("http-example")
58
+ provider.send_message("http://localhost:9393/postbox", message)
59
+ ```
60
+
61
+ ### Receiving messages
62
+
63
+ Use the API as below for receiving in the synchronous case while sending:
64
+
65
+ ```
66
+ response = provider.send_message("http://localhost:9393/postbox", message)
67
+ ```
68
+
69
+ Alternatively after a send, in the synchronous case:
70
+ ```
71
+ response = provider.receive_message
72
+ ```
73
+
74
+ Use the API as below for receiving in the asynchronous case. Provide the transport API registered
75
+ back to the subscriber in order for it to know which transport (it may have multiple) pinged it
76
+ with a message. We are using an AMQP transport provider for this example:
77
+
78
+ ```
79
+ class Subscriber
80
+ def receive(message)
81
+ puts "Received #{message}"
82
+ end
83
+ end
84
+
85
+ class RabbitTransportProvider < SoarTransportApi::TransportAPI
86
+ def receive_messages(callback, transport_provider_id)
87
+ t = Thread.new {
88
+ @conn.start
89
+ ch = @conn.create_channel
90
+ q = ch.queue(@transport_identifier)
91
+ begin
92
+ message = nil
93
+ q.subscribe(:block => true) do |delivery_info, properties, body|
94
+ puts " [x] Received message"
95
+ callback.receive(transport_provider_id, body)
96
+ end
97
+ @conn.close
98
+ rescue Interrupt => _
99
+ @conn.close
100
+ end
101
+ }
102
+ t.abort_on_exception = true
103
+ end
104
+ end
105
+
106
+ provider = RabbitTransportProvider.new("bunnies")
107
+ subscriber = Subscriber.new
108
+ provider.receive_messages(suscriber, "bunnies")
109
+ ```
110
+
111
+ ### Exceptions
112
+
113
+ The transport API raises the following exceptions:
114
+
115
+ ```
116
+ TransportIdentifierInvalidError - a transport identifier was not provided (nil) or was not a String or was an empty string
117
+ SubscriberCallbackInvalidError - the subscriber specified for callbacks of received messages was not provided (nil) or does not have a receive method
118
+ NoMessageError - No message was provided to send
119
+ InvalidURIError - An invalid URI or no URI was provided
120
+ ```
121
+
122
+ ## Contributing
123
+
124
+ Bug reports and feature requests are welcome by email to ernst dot van dot graan at hetzner dot co dot za. This gem is sponsored by Hetzner (Pty) Ltd (http://hetzner.co.za)
125
+
126
+ ## License
127
+
128
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
129
+
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 "soar_transport_api"
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,9 @@
1
+ require "soar_transport_api/version"
2
+ require "soar_transport_api/transport_api"
3
+ require "soar_transport_api/transport_identifier_invalid_error"
4
+ require "soar_transport_api/subscriber_callback_invalid_error"
5
+ require "soar_transport_api/no_message_error"
6
+ require "soar_transport_api/invalid_URI_error"
7
+
8
+ module SoarTransportApi
9
+ end
@@ -0,0 +1,4 @@
1
+ module SoarTransportApi
2
+ class InvalidURIError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module SoarTransportApi
2
+ class NoMessageError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module SoarTransportApi
2
+ class SubscriberCallbackInvalidError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,48 @@
1
+ module SoarTransportApi
2
+ class TransportAPI
3
+ DELIVERY_PENDING = "Delivery pending" unless defined? DELIVERY_PENDING; DELIVERY_PENDING.freeze
4
+ DELIVERY_REJECTED = "Rejected for delivery" unless defined? DELIVERY_REJECTED; DELIVERY_REJECTED.freeze
5
+ DELIVERY_FAILURE = "Delivery failure" unless defined? DELIVERY_FAILURE; DELIVERY_FAILURE.freeze
6
+ DELIVERY_SUCCESS = "Delivered successfull" unless defined? DELIVERY_SUCCESS; DELIVERY_SUCCESS.freeze
7
+ DELIVERY_TIMEOUT = "Delivery timeout" unless defined? DELIVERY_TIMEOUT; DELIVERY_TIMEOUT.freeze
8
+
9
+ attr_accessor :transport_identifier
10
+
11
+ def initialize(transport_identifier)
12
+ raise SoarTransportApi::TransportIdentifierInvalidError.new("Invalid transport identifier") if invalid_transport_identifier?(transport_identifier)
13
+ @transport_identifier = transport_identifier
14
+ end
15
+
16
+ def send_message(uri, message)
17
+ raise SoarTransportApi::NoMessageError.new("No message provided") if message.nil?
18
+ raise SoarTransportApi::InvalidURIError.new("Invalid URI") if invalid_uri?(uri)
19
+ end
20
+
21
+ def receive_messages(subscriber, transport_provider_id)
22
+ raise SoarTransportApi::TransportIdentifierInvalidError.new("Invalid transport identifier") if invalid_transport_identifier?(transport_provider_id)
23
+ raise SoarTransportApi::SubscriberCallbackInvalidError.new("Invalid subscriber") if invalid_subscriber?(subscriber)
24
+ end
25
+
26
+ def receive_message
27
+ end
28
+
29
+ private
30
+
31
+ def invalid_uri?(uri)
32
+ return true if uri.nil?
33
+ not (uri =~ URI::DEFAULT_PARSER.regexp[:UNSAFE]).nil?
34
+ end
35
+
36
+ def invalid_transport_identifier?(transport_identifier)
37
+ transport_identifier.nil? or
38
+ (not transport_identifier.is_a?(String)) or
39
+ transport_identifier.strip == ""
40
+ end
41
+
42
+ def invalid_subscriber?(subscriber)
43
+ subscriber.nil? or
44
+ not subscriber.methods.include?(:receive)
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,4 @@
1
+ module SoarTransportApi
2
+ class TransportIdentifierInvalidError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module SoarTransportApi
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'soar_transport_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "soar_transport_api"
8
+ spec.version = SoarTransportApi::VERSION
9
+ spec.authors = ["Ernst Van Graan"]
10
+ spec.email = ["ernst.van.graan@hetzner.co.za"]
11
+
12
+ spec.summary = %q{API to be implemented by transport providers that want to communicate across soar_comms communication fabric}
13
+ spec.description = %q{API to be implemented by transport providers that want to communicate across soar_comms communication fabric}
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "byebug"
25
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: soar_transport_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ernst Van Graan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-18 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: API to be implemented by transport providers that want to communicate
70
+ across soar_comms communication fabric
71
+ email:
72
+ - ernst.van.graan@hetzner.co.za
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".ruby-gemset"
80
+ - ".ruby-version"
81
+ - ".travis.yml"
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - lib/soar_transport_api.rb
89
+ - lib/soar_transport_api/invalid_URI_error.rb
90
+ - lib/soar_transport_api/no_message_error.rb
91
+ - lib/soar_transport_api/subscriber_callback_invalid_error.rb
92
+ - lib/soar_transport_api/transport_api.rb
93
+ - lib/soar_transport_api/transport_identifier_invalid_error.rb
94
+ - lib/soar_transport_api/version.rb
95
+ - soar_transport_api.gemspec
96
+ homepage:
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.4.8
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: API to be implemented by transport providers that want to communicate across
120
+ soar_comms communication fabric
121
+ test_files: []