columbo-client 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: 0434c660df5e43225a76ec249d66c10d68fe6240
4
+ data.tar.gz: 4b1dba18f46db3338fcc6995cb9a53460c99343e
5
+ SHA512:
6
+ metadata.gz: 3cb900a4ab3f9681dd5e051c4e0aa70cdc26606f01e43d062c56fc5a9f86e8a15d2c2c76654b80a77852072480c28c6dae62731ecb12ad9117bee67ece3aa7d7
7
+ data.tar.gz: 9c78bc90e10bd00acb38d244a6fe87aeb995079d7b294740c74e565b3adf7122fb181d87cb510749b737701be570bb6bb6cd01ee9adab4a42d7f69942ff3534e
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .rvmrc
11
+ *.gem
12
+ *~
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in columbo.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Wifirst
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,123 @@
1
+ # Columbo Ruby Client
2
+
3
+ This gem provides a ruby integration for Columbo. It provides:
4
+
5
+ * HTTP client
6
+ * AMQP client (RabbitMQ implementation only, due to [Bunny](https://github.com/ruby-amqp/bunny) gem dependance)
7
+
8
+ For integration with Rails applications, see the [columbo-rails-client](https://git.wifirst.net/gems/columbo-rails-client) gem.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'columbo-client'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install columbo-client
25
+
26
+ ## Configuration
27
+
28
+ ### Using HTTP
29
+
30
+ ```ruby
31
+ Columbo.configure do |config|
32
+ config.system.uid = 'system_uid'
33
+ config.system.label = 'system_label'
34
+ config.system.type = 'system_type'
35
+
36
+ config.client = Columbo::Client::HTTP.new('http://example.com/push')
37
+ end
38
+ ```
39
+
40
+ ### Using AMQP
41
+
42
+ For the AMQP client, the exchange must be configurated through the
43
+ `Columbo::Client::AMQP::exchange` method by giving a name and a block
44
+ as seen in the example below. `durable`, `auto_delete` and `arguments`
45
+ exchange options can also be set as explained in the Bunny gem
46
+ [here](http://reference.rubybunny.info/Bunny/Exchange.html#initialize-instance_method).
47
+
48
+ ```ruby
49
+ Columbo.configure do |config|
50
+ config.system.uid = 'system_uid'
51
+ config.system.label = 'system_label'
52
+ config.system.type = 'system_type'
53
+
54
+ config.client = Columbo::Client::AMQP.new("amqp://guest:guest@localhost:5672")
55
+
56
+ config.client.exchange 'exchange-name' do |exchange_options|
57
+ exchange_options.type = 'topic'
58
+ exchange_options.durable = true
59
+ end
60
+ end
61
+ ```
62
+
63
+ ## Usage
64
+
65
+ We show how to push an event to Columbo by sending the next crafted event:
66
+
67
+ ```ruby
68
+ example_event = {
69
+ "system": {
70
+ "uid": "myCompany",
71
+ "type": "application",
72
+ "label": "MyCompany"
73
+ },
74
+ "action": "contact.created",
75
+ "actor": {
76
+ "uid": "john.smith@mycompany.com",
77
+ "type": "commercial",
78
+ "label": "John Smith"
79
+ },
80
+ "resource": {
81
+ "uid": "1",
82
+ "type": "contact",
83
+ "label": "Eric Martin",
84
+ "attributes": {
85
+ "first_name": "Eric",
86
+ "last_name": "Martin",
87
+ "email": "eric.martin@customer.com",
88
+ "phone": "11111111"
89
+ }
90
+ },
91
+ "context": {},
92
+ "related_resources": [],
93
+ "timestamp": "2016-12-29T10:00:00.000000+00:00"
94
+ }
95
+ ```
96
+
97
+ For each client (HTTP and AMQP), 2 methods are available:
98
+
99
+ * `publish` will return `true` or `false` depending on the successfulness
100
+ of the publishing.
101
+ * `publish!` will raise an error if the publishing fails.
102
+
103
+ ### Using HTTP
104
+
105
+ The `publish` and `publish!` methods take an event `Hash` as parameter.
106
+
107
+ ```ruby
108
+ Columbo.client.publish(example_event)
109
+ ```
110
+
111
+ ### Using AMQP
112
+
113
+ The `publish` and `publish!` methods take an event `Hash` as first
114
+ parameter and an optional `Hash` parameter. The possible values for
115
+ the second parameter are available in Bunny documentation
116
+ [here](http://reference.rubybunny.info/Bunny/Exchange.html#publish-instance_method).
117
+
118
+
119
+ ```ruby
120
+ options[:routing_key] = 'resource.action'
121
+
122
+ Columbo.client.publish(example_event, options)
123
+ ```
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 "columbo-client"
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,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'columbo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "columbo-client"
8
+ spec.version = Columbo::VERSION
9
+ spec.authors = ["Wifirst"]
10
+ spec.homepage = 'https://github.com/wifirst-lab/columbo-ruby-client'
11
+
12
+ spec.summary = "A simple Ruby client for the Columbo REST and AMQP API"
13
+ spec.description = "A simple Ruby client for the Columbo REST and AMQP API"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "bunny", "~> 2.0"
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
@@ -0,0 +1,26 @@
1
+ require 'json'
2
+ require 'logger'
3
+ require 'columbo/version'
4
+ require 'columbo/configuration'
5
+ require 'columbo/clients/amqp'
6
+ require 'columbo/clients/http'
7
+
8
+ module Columbo
9
+ class << self
10
+ def configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ def logger
15
+ @logger ||= configuration.logger
16
+ end
17
+
18
+ def configure
19
+ yield(configuration) if block_given?
20
+ end
21
+
22
+ def client
23
+ @client ||= configuration.client
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,53 @@
1
+ require 'bunny'
2
+
3
+ module Columbo
4
+ module Client
5
+ class AMQP
6
+ def initialize(connection_uri)
7
+ @connection_uri = connection_uri
8
+ end
9
+
10
+ def publish(event, options = {})
11
+ begin
12
+ publish_event(event, options)
13
+ true
14
+ rescue Exception => e
15
+ Columbo.logger.warn(e.message)
16
+ false
17
+ end
18
+ end
19
+
20
+ def publish!(event, options = {})
21
+ publish_event(event, options)
22
+ end
23
+
24
+ def exchange(name)
25
+ raise ArgumentError, 'Please provide a name for the AMQP exchange' if name.nil?
26
+
27
+ @channel = connection.create_channel
28
+
29
+ if block_given?
30
+ exchange_options = OpenStruct.new(arguments: {})
31
+ yield exchange_options
32
+
33
+ type = exchange_options.delete_field(:type)
34
+ @exchange = @channel.send(type, name, exchange_options.to_h)
35
+ else
36
+ raise ArgumentError, 'Please provide a block to configure the exchange'
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def publish_event(event, options = {})
43
+ raise 'No exchange has been configurated for the Columbo Client' if @exchange.nil?
44
+
45
+ @exchange.publish(event.to_json, options)
46
+ end
47
+
48
+ def connection
49
+ @connection ||= Bunny.new(@connection_uri).start
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,57 @@
1
+ require 'net/http'
2
+
3
+ module Columbo
4
+ module Client
5
+ class HTTP
6
+ DEFAULT_OPEN_TIMEOUT = 2
7
+ DEFAULT_READ_TIMEOUT = 2
8
+
9
+ def initialize(push_url, opts = {})
10
+ raise ArgumentError.new('Please provide a push url') if push_url.nil?
11
+
12
+ @push_url = push_url
13
+ @open_timeout = opts[:open_timeout] || DEFAULT_OPEN_TIMEOUT
14
+ @read_timeout = opts[:read_timeout] || DEFAULT_READ_TIMEOUT
15
+ end
16
+
17
+ def publish(event, options = nil)
18
+ begin
19
+ publish_event(event)
20
+ rescue Exception => e
21
+ Columbo.logger.warn(e.message)
22
+ false
23
+ end
24
+ end
25
+
26
+ def publish!(event, options = nil)
27
+ publish_event(event)
28
+ end
29
+
30
+ private
31
+
32
+ def publish_event(event)
33
+ post(@push_url, event.to_json)
34
+ end
35
+
36
+ def post(uri, body)
37
+ uri = URI(uri)
38
+ path = uri.path.empty? ? '/' : uri.path
39
+
40
+ proxy = URI.parse(ENV['HTTP_PROXY'] || ENV['http_proxy']) rescue nil
41
+ proxy_host = proxy.nil? ? nil : proxy.host
42
+ proxy_port = proxy.nil? ? nil : proxy.port
43
+ http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port)
44
+ http.use_ssl = uri.instance_of?(URI::HTTPS)
45
+ http.open_timeout = @open_timeout
46
+ http.read_timeout = @read_timeout
47
+
48
+ headers = {
49
+ 'Content-Type' => 'application/json',
50
+ 'User-Agent' => "Columbo-Ruby-Client/#{Columbo::VERSION}"
51
+ }
52
+ response = http.post(path, body, headers)
53
+ response.kind_of?(Net::HTTPSuccess) ? true : response.error!
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,10 @@
1
+ module Columbo
2
+ class Configuration
3
+ attr_accessor :amqp, :logger, :system, :client
4
+
5
+ def initialize
6
+ @system = OpenStruct.new
7
+ @logger = Logger.new(STDOUT)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ module Columbo
2
+ class NoConfigurationFoundError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Columbo
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: columbo-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Wifirst
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bunny
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: A simple Ruby client for the Columbo REST and AMQP API
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - ".rspec"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - columbo-client.gemspec
85
+ - lib/columbo-client.rb
86
+ - lib/columbo/clients/amqp.rb
87
+ - lib/columbo/clients/http.rb
88
+ - lib/columbo/configuration.rb
89
+ - lib/columbo/error.rb
90
+ - lib/columbo/version.rb
91
+ homepage: https://github.com/wifirst-lab/columbo-ruby-client
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: A simple Ruby client for the Columbo REST and AMQP API
115
+ test_files: []