brm_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Stephane Bellity
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = brm_client
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Stephane Bellity. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,72 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'metric_fu' rescue LoadError
6
+ MetricFu::Configuration.run do |config|
7
+ config.metrics = [:flog, :flay, :reek, :saikuro]
8
+ config.graphs = [:flog, :flay, :reek]
9
+ config.graph_engine = :gchart
10
+ end
11
+ rescue LoadError
12
+ puts "Metric_fu (or a dependency) not available. Install it with: gem install metric_fu"
13
+ end
14
+
15
+ begin
16
+ require 'jeweler'
17
+ Jeweler::Tasks.new do |gem|
18
+ gem.name = "brm_client"
19
+ gem.summary = "BRM Event logging client"
20
+ gem.description = ""
21
+ gem.email = "sbellity@gmail.com"
22
+ gem.homepage = "http://github.com/sbellity/brm_client"
23
+ gem.authors = ["Stephane Bellity"]
24
+ gem.add_development_dependency "rspec", ">= 1.2.9"
25
+ gem.add_dependency "bunny"
26
+ gem.add_dependency "mongo", ">= 0.18.3"
27
+ gem.add_dependency "activesupport"
28
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
29
+ end
30
+ Jeweler::GemcutterTasks.new
31
+ rescue LoadError
32
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
33
+ end
34
+
35
+ require 'spec/rake/spectask'
36
+ Spec::Rake::SpecTask.new(:spec) do |spec|
37
+ spec.libs << 'lib' << 'spec'
38
+ spec.spec_files = FileList['spec/**/*_spec.rb']
39
+ end
40
+
41
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
42
+ spec.libs << 'lib' << 'spec'
43
+ spec.pattern = 'spec/**/*_spec.rb'
44
+ spec.rcov = true
45
+ end
46
+
47
+ task :spec => :check_dependencies
48
+
49
+ begin
50
+ require 'reek/adapters/rake_task'
51
+ Reek::RakeTask.new do |t|
52
+ t.fail_on_error = true
53
+ t.verbose = false
54
+ t.source_files = 'lib/**/*.rb'
55
+ end
56
+ rescue LoadError
57
+ task :reek do
58
+ abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
59
+ end
60
+ end
61
+
62
+ task :default => :spec
63
+
64
+ require 'rake/rdoctask'
65
+ Rake::RDocTask.new do |rdoc|
66
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
67
+
68
+ rdoc.rdoc_dir = 'rdoc'
69
+ rdoc.title = "brm_client #{version}"
70
+ rdoc.rdoc_files.include('README*')
71
+ rdoc.rdoc_files.include('lib/**/*.rb')
72
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,134 @@
1
+ module BrmClient
2
+ module Event
3
+
4
+
5
+ def message type, recipients, msg, context=nil, event_ref=nil
6
+ data = {
7
+ "message" => msg,
8
+ "recipients" => recipients,
9
+ "type" => type
10
+ }
11
+ event "Message", data, context, event_ref
12
+ end
13
+
14
+ def action(type, context=nil, event_ref="")
15
+ event "Action", { "type" => type }, context, event_ref
16
+ end
17
+
18
+ def sign_in(service_name, agent_id=nil, agent_type=nil, context=nil, event_ref="")
19
+ session_event("start", service_name, agent_id, agent_type, context, event_ref)
20
+ end
21
+
22
+ def sign_out(service_name, agent_id=nil, agent_type=nil, context=nil, event_ref="")
23
+ session_event("end", service_name, agent_id, agent_type, context, event_ref)
24
+ end
25
+
26
+ def sign_in_failed(service_name, agent_id=nil, agent_type=nil, context=nil, event_ref="")
27
+ session_event("failed", service_name, agent_id, agent_type, context, event_ref)
28
+ end
29
+
30
+ def session_expired(service_name, agent_id=nil, agent_type=nil, context=nil, event_ref="")
31
+ session_event("expired", service_name, agent_id, agent_type, context, event_ref)
32
+ end
33
+
34
+ def location(location_id=nil, location_type="url", referrer_id=nil, referrer_type="url", context=nil, event_ref="")
35
+ data = { "location" => { "type" => location_type, "id" => location_id } }
36
+ data["referrer"] = { "id" => referrer_id, "type" => referrer_type } if referrer_id
37
+ event("Location", data, context, event_ref)
38
+ end
39
+
40
+ def create(resource_id, resource_type, resource_attributes=nil, context=nil, event_ref="")
41
+ resource_event("create", resource_id, resource_type, resource_attributes || {}, context, event_ref)
42
+ end
43
+
44
+ def update(resource_id, resource_type, resource_attributes=nil, context=nil, event_ref="")
45
+ resource_event("update", resource_id, resource_type, resource_attributes || {}, context, event_ref)
46
+ end
47
+
48
+ def delete(resource_id, resource_type, context=nil, event_ref="")
49
+ resource_event("delete", resource_id, resource_type, {}, context, event_ref)
50
+ end
51
+
52
+ def share(destination, resource_id, resource_type, context=nil, event_ref="")
53
+ resource = {"id" => resource_id, "type" => resource_type }
54
+ event("Share", {"resource" => resource, "destination" => destination }, context, event_ref)
55
+ end
56
+
57
+ def message(type, recipients, msg, context=nil, event_ref="")
58
+ data = { "message" => msg, "recipients" => recipients, "type" => type }
59
+ event("Message", data, context, event_ref)
60
+ end
61
+
62
+ def connect(type, action, with_id, with_type, attributes=nil, context=nil, event_ref="")
63
+ data = {
64
+ "action" => action,
65
+ "type" => type,
66
+ "withAgent" => { "id" => with_id, "type" => with_type }
67
+ }
68
+ data["attributes"] = attributes if attributes;
69
+ event("Connect", data, context, event_ref)
70
+ end
71
+
72
+ def transaction(type, resources, from_id, from_type, context=nil, event_ref="")
73
+ data = {
74
+ "type" => type,
75
+ "resources" => resources,
76
+ "fromAgent" => { "id" => from_id, "type" => from_type }
77
+ }
78
+ event("Transaction", data, context, event_ref)
79
+ end
80
+
81
+
82
+ def system(type, msg="", context=nil, event_ref="")
83
+ event("System", { "type" => type, "message" => msg }, context, event_ref)
84
+ end
85
+
86
+ def describe(type, resource, context=nil, event_ref="")
87
+ event("Describe", { "type" => type, "resource" => resource }, context, event_ref)
88
+ end
89
+
90
+
91
+ def sign_up(service, step_status="success", context=nil, event_ref="")
92
+ register_event(service, "register", 1, step_status, true, context, event_ref)
93
+ end
94
+
95
+ def unsubscribe(service, context=nil, event_ref="")
96
+ register_event(service, "unsubscribe", -1, "success", true, context, event_ref)
97
+ end
98
+
99
+ def session_event(type, service_name, agent_id, agent_type, context, event_ref)
100
+ if type != "start"
101
+ user_id = nil
102
+ facet_id = nil
103
+ elsif agent_id
104
+ if agent_type == "facet"
105
+ facet_id = agent_id
106
+ else
107
+ user_id = agent_id
108
+ end
109
+ end
110
+
111
+ event("Session", { "type" => type, "service" => service_name }, context, event_ref)
112
+ end
113
+
114
+
115
+ def resource_event(type, resource_id, resource_type, attributes=nil, context=nil, event_ref="")
116
+ resource = attributes || {}
117
+ resource.update("id" => resource_id, "type" => resource_type)
118
+ event "Resource", { "resource" => resource, "type" => type }, context, event_ref
119
+ end
120
+
121
+ def register_event(service, step_name, step_num=0, step_status="success", complete=true, context=nil, event_ref="")
122
+ data = {
123
+ "service" => service,
124
+ "step" => {
125
+ "num" => step_num,
126
+ "name" => step_name,
127
+ "status" => step_status
128
+ },
129
+ "complete" => complete
130
+ }
131
+ event("Register", data, context, event_ref)
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,33 @@
1
+ require "bunny"
2
+
3
+ module BrmClient
4
+ module Gateway
5
+ class Amqp
6
+ attr_reader :connection, :queue
7
+
8
+ def initialize opts
9
+ @connection = Bunny.new({
10
+ :logging => opts[:logging] || true,
11
+ :host => opts[:host] || "localhost",
12
+ :port => opts[:port] || 5672,
13
+ :user => opts[:user] || "guest",
14
+ :pass => opts[:pass] || "guest",
15
+ :vhost => opts[:vhost] || opts[:application]
16
+ })
17
+
18
+ @connection.start
19
+ @queue = @connection.queue "reactor.#{opts[:application]}"
20
+ end
21
+
22
+ def disconnect
23
+ @connection.stop
24
+ end
25
+
26
+ def send_event e
27
+ @queue.publish(e.to_json)
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ require "mongo"
2
+
3
+ module BrmClient
4
+ module Gateway
5
+ class File
6
+ attr_reader :path, :file
7
+ def initialize opts
8
+ @path = opts[:path] || "/tmp/#{opts[:application]}.log"
9
+ @file = open(@path, "w")
10
+ end
11
+
12
+ def disconnect
13
+ @file.close
14
+ end
15
+
16
+ def send_event e
17
+ @file.write "#{e.to_json}\n"
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ require "mongo"
2
+
3
+ module BrmClient
4
+ module Gateway
5
+ class Mongo
6
+ attr_reader :connection, :collection
7
+ def initialize opts
8
+ @connection = ::Mongo::Connection.new({
9
+ :host => opts[:host] || "localhost",
10
+ :port => opts[:port] || ::Mongo::Connection::DEFAULT_PORT
11
+ })
12
+ @collection = @connection.db(opts[:db] || opts[:application]).collection(opts[:collection] || "logs")
13
+ end
14
+
15
+ def disconnect
16
+ end
17
+
18
+ def send_event e
19
+ collection.insert(e)
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ require "brm_client/gateway/amqp"
2
+ require "brm_client/gateway/mongo"
3
+ require "brm_client/gateway/file"
@@ -0,0 +1,56 @@
1
+ module BrmClient
2
+ class Logger
3
+ include BrmClient::Event
4
+
5
+ def sequence_number
6
+ @sequence_number ||= 0
7
+ @sequence_number += 1
8
+ end
9
+
10
+ attr_reader :gateway, :application, :facet_id, :user_id
11
+
12
+ def initialize(application, gateway_type, *args)
13
+ @application = application
14
+ gateway_options = args.extract_options! || {}
15
+ gateway_options[:application] = application
16
+ @gateway = BrmClient::Gateway.const_get(gateway_type).new(gateway_options)
17
+ end
18
+
19
+ def disconnect
20
+ @gateway.disconnect
21
+ end
22
+
23
+ private
24
+
25
+ def send_event e
26
+ @gateway.send_event(e)
27
+ end
28
+
29
+ def event(event_name, data=nil, context=nil, event_ref="")
30
+ event = HashWithIndifferentAccess.new
31
+ event["data"] = data || {}
32
+ event["context"] ||= {}
33
+
34
+ event["data"]["agent"] = {:id => facet_id, :type => "facet"} if facet_id
35
+
36
+ if user_id
37
+ event["data"]["agent"] ||= {:id => user_id, :type => "user"}
38
+ event["context"]["userID"] = user_id
39
+ end
40
+ event["metaData"] = {
41
+ "timestamp" => Time.now.to_i * 1000,
42
+ "eventName" => event_name,
43
+ "application" => application,
44
+ "loggerVersion" => VERSION,
45
+ "loggerType" => "ruby",
46
+ "sequenceNumber" => sequence_number
47
+ }
48
+ event["metaData"]["eventRef"] = event_ref
49
+
50
+ send_event event
51
+ end
52
+
53
+
54
+
55
+ end
56
+ end
data/lib/brm_client.rb ADDED
@@ -0,0 +1,12 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require "rubygems"
3
+ require "active_support"
4
+
5
+ require "brm_client/event"
6
+ require "brm_client/logger"
7
+ require "brm_client/gateway"
8
+
9
+
10
+ module BrmClient
11
+ VERSION = File.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).chomp
12
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "BrmClient" do
4
+
5
+ before(:each) do
6
+ application = "brm_client_test"
7
+ # @logger = BrmClient::Logger.new(application, "File")
8
+ # @logger = BrmClient::Logger.new(application, "Amqp", { :host => "10.101.0.215", :vhost => "/" })
9
+ end
10
+
11
+ after(:each) do
12
+ @logger.disconnect
13
+ end
14
+
15
+ it "should send custom events" do
16
+ 10.times { |i| @logger.message("think", { "id" => "123", "type" => "facet" }, "Salut #{i}") }
17
+ end
18
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'brm_client'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brm_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stephane Bellity
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-10 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: bunny
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: mongo
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.18.3
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: activesupport
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ description: ""
56
+ email: sbellity@gmail.com
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.rdoc
64
+ files:
65
+ - .document
66
+ - .gitignore
67
+ - LICENSE
68
+ - README.rdoc
69
+ - Rakefile
70
+ - VERSION
71
+ - lib/brm_client.rb
72
+ - lib/brm_client/event.rb
73
+ - lib/brm_client/gateway.rb
74
+ - lib/brm_client/gateway/amqp.rb
75
+ - lib/brm_client/gateway/file.rb
76
+ - lib/brm_client/gateway/mongo.rb
77
+ - lib/brm_client/logger.rb
78
+ - spec/brm_client_spec.rb
79
+ - spec/spec.opts
80
+ - spec/spec_helper.rb
81
+ has_rdoc: true
82
+ homepage: http://github.com/sbellity/brm_client
83
+ licenses: []
84
+
85
+ post_install_message:
86
+ rdoc_options:
87
+ - --charset=UTF-8
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ version:
102
+ requirements: []
103
+
104
+ rubyforge_project:
105
+ rubygems_version: 1.3.5
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: BRM Event logging client
109
+ test_files:
110
+ - spec/brm_client_spec.rb
111
+ - spec/spec_helper.rb