rnotifier 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b2114e0d1bbcc910c636ec5bb458b464a83032f7
4
- data.tar.gz: ece609826496d1734fca46620eaafdaa3d4744b2
3
+ metadata.gz: cdc39bc4bb388b4f04a7da32a3a1c78f94f02776
4
+ data.tar.gz: 416474803f5349c511b8fd5f95b38cccc2208ffc
5
5
  SHA512:
6
- metadata.gz: 35cc8f988db0717644c723ecf7de928285ae4341f52544cb490f86849b3802c8974d47d4b509de0ec91d043a79ad43973507c23dfb8ce59f2694dcc1b3a92997
7
- data.tar.gz: ac4c232292e0407259f64d0c0dba2c86b052fdd20be652a44404611f490885d080deefc8bb16822888d4a7f227c7aed8901e8cb4f2f2d6a707abdf97b422cba8
6
+ metadata.gz: 8d8f7981351c4a9ced9be049a1f7732b77b5eaf55d8fb29cb1d837cea7165d795ad81d1261f6981371a118f34f80c256adede6935b622ae7eeaef281c5dc7b97
7
+ data.tar.gz: 8af20c69d211af538a61f6da359f95cded4cbd778c790fc5f30c7d398039f1e5b29083803773d3152a6926c36752ecb37918d8150f6e56ec74c21d63658f83a1
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  *.rbc
3
+ .DS_Store
3
4
  .bundle
4
5
  .config
5
6
  .yardoc
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Rnotifier
2
2
 
3
- Exception catcher for rack base applications.
3
+ Events and Exception catcher libraray.
4
4
 
5
5
  [![Build Status](https://travis-ci.org/jiren/rnotifier.png?branch=master)](https://travis-ci.org/jiren/rnotifier)
6
6
  [![Coverage Status](https://coveralls.io/repos/jiren/rnotifier/badge.png?branch=master)](https://coveralls.io/r/jiren/rnotifier?branch=master)
@@ -21,7 +21,7 @@ Or install it yourself as:
21
21
 
22
22
  ## Usage
23
23
 
24
- rnotifier install 'API-KEY' # This will create 'config/rnotifier.yaml' file.
24
+ rnotifier install 'API-KEY' # This will create 'config/rnotifier.yaml' file.
25
25
 
26
26
  ### Config file options
27
27
 
@@ -33,6 +33,25 @@ rnotifier install 'API-KEY' # This will create 'config/rnotifier.yaml' file.
33
33
 
34
34
  rnotifier test #this will send test exception to rnotifier.
35
35
 
36
+
37
+ ### Send events and alerts
38
+
39
+ Rnotifier.event(:sign_up, {:username => 'Jiren', :email => 'jiren@example.com', :using => 'facebook' })
40
+
41
+ Rnotifier.alert(:order_fail, {:user => 'Jiren', :product => 'PS3', :order_id => '321' })
42
+
43
+
44
+ You can also sends tags with 'event' and 'alert'
45
+ i.e
46
+
47
+ Rnotifier.event(
48
+ :sign_up,
49
+ {:username => 'Jiren', :email => 'jiren@example.com', :using => 'facebook' },
50
+ {:tags => ['newsletter']}
51
+ )
52
+
53
+
54
+
36
55
  ## Contributing
37
56
 
38
57
  1. Fork it
data/bin/rnotifier CHANGED
@@ -15,6 +15,7 @@ install api-key [environments] #Create config/rnotifier.yaml with api_key and en
15
15
  require 'rack'
16
16
  require 'rnotifier'
17
17
  require 'rnotifier/config_test.rb'
18
+ ENV['RACK_ENV'] = 'rnotifier_test'
18
19
  Rnotifier.load_config('config/rnotifier.yaml')
19
20
  if Rnotifier::Config.valid?
20
21
  Rnotifier::ConfigTest.test
@@ -3,7 +3,7 @@ module Rnotifier
3
3
  DEFAULT = {
4
4
  :api_host => 'http://api.rnotifier.com',
5
5
  :api_version => 'v1',
6
- :notify_path => 'exception',
6
+ :exception_path => 'exception',
7
7
  :event_path => 'event',
8
8
  :ignore_env => ['development', 'test'],
9
9
  :http_open_timeout => 2,
@@ -13,7 +13,7 @@ module Rnotifier
13
13
  CLIENT = "RRG:#{Rnotifier::VERSION}"
14
14
 
15
15
  class << self
16
- attr_accessor :api_key, :notification_path, :event_path, :environments, :current_env,
16
+ attr_accessor :api_key, :exception_path, :event_path, :environments, :current_env,
17
17
  :valid, :app_env, :api_host, :ignore_exceptions, :capture_code
18
18
 
19
19
  def [](val)
@@ -46,12 +46,11 @@ module Rnotifier
46
46
  return if self.api_key.to_s.length == 0
47
47
 
48
48
  self.api_host ||= DEFAULT[:api_host]
49
- self.notification_path = '/' + [DEFAULT[:api_version], DEFAULT[:notify_path], self.api_key].join('/')
49
+ self.exception_path = '/' + [DEFAULT[:api_version], DEFAULT[:exception_path]].join('/')
50
50
  self.app_env = get_app_env
51
51
  self.ignore_exceptions = self.ignore_exceptions.split(',') if self.ignore_exceptions.is_a?(String)
52
52
 
53
- self.event_path = '/' + [DEFAULT[:api_version], DEFAULT[:event_path], self.api_key].join('/')
54
-
53
+ self.event_path = '/' + [DEFAULT[:api_version], DEFAULT[:event_path]].join('/')
55
54
  self.valid = true
56
55
  end
57
56
 
@@ -83,16 +82,6 @@ module Rnotifier
83
82
  (defined?(Rails) && Rails.respond_to?(:root)) ? Rails.root.to_s : Dir.pwd
84
83
  end
85
84
 
86
- def event_app_env
87
- {
88
- :env => self.current_env,
89
- :pid => $$,
90
- :host => (Socket.gethostname rescue ''),
91
- :language => 'ruby',
92
- :platform => (RUBY_PLATFORM rescue '')
93
- }
94
- end
95
-
96
85
  end
97
86
  end
98
87
  end
@@ -5,16 +5,17 @@ module Rnotifier
5
5
  EVENT = 0
6
6
  ALERT = 1
7
7
 
8
- def initialize(name, type, data = {})
8
+ def initialize(name, type, data = {}, tags = nil)
9
9
  @data = {
10
10
  :name => name,
11
11
  :data => data,
12
12
  :app_env => EventData.app_env,
13
- :occurred_at => Time.now.utc.to_s,
13
+ :occurred_at => Time.now.to_i,
14
14
  :rnotifier_client => Config::CLIENT,
15
15
  :type => type,
16
16
  }
17
17
  @data[:context_data] = Thread.current[:rnotifier_context] if Thread.current[:rnotifier_context]
18
+ @data[:tags] = tags if tags
18
19
  end
19
20
 
20
21
  def notify
@@ -27,7 +28,12 @@ module Rnotifier
27
28
  end
28
29
 
29
30
  def self.app_env
30
- @env ||= Rnotifier::Config.event_app_env
31
+ @app_env ||= {
32
+ :env => Config.current_env,
33
+ :pid => $$,
34
+ :host => (Socket.gethostname rescue ''),
35
+ :language => 'ruby'
36
+ }
31
37
  end
32
38
 
33
39
  end
@@ -1,13 +1,23 @@
1
1
  module Rnotifier
2
2
  class ExceptionCode
3
+
4
+ SYNTAX_ERROR_REGX = /\A(.*:\d*):/
5
+
3
6
  class << self
4
7
 
5
- def get(backtrace)
6
- return unless backtrace
7
- bline = backtrace.find do |l|
8
- l.index(Config.app_env[:app_root]) == 0 && !Gem.path.any?{|path| l.index(path) == 0}
8
+ def get(exception)
9
+ return unless exception.backtrace
10
+
11
+ if exception.class == SyntaxError && exception.message.match(SYNTAX_ERROR_REGX)
12
+ bline = $1
13
+ else
14
+ bline = exception.backtrace.find do |l|
15
+ l.index(Config.app_env[:app_root]) == 0 && !Gem.path.any?{|path| l.index(path) == 0}
16
+ end
9
17
  end
18
+
10
19
  filename, line, method = (bline|| backtrace[0]).split(':')
20
+
11
21
  self.find(filename, line.to_i, 3)
12
22
  end
13
23
 
@@ -13,19 +13,19 @@ module Rnotifier
13
13
  end
14
14
 
15
15
  def notify
16
- return unless Config.valid?
16
+ return if !Config.valid?
17
17
  return if Config.ignore_exceptions && Config.ignore_exceptions.include?(exception.class.to_s)
18
18
 
19
19
  begin
20
20
  data = options[:type] == :rack ? self.rack_exception_data : {:extra => self.env }
21
21
  data[:app_env] = Rnotifier::Config.app_env
22
- data[:occurred_at] = Time.now.utc.to_s
22
+ data[:occurred_at] = Time.now.to_i
23
23
  data[:exception] = self.exception_data
24
24
  data[:context_data] = Thread.current[:rnotifier_context] if Thread.current[:rnotifier_context]
25
25
  data[:data_from] = options[:type]
26
26
  data[:rnotifier_client] = Config::CLIENT
27
27
 
28
- return Notifier.send(data)
28
+ return Notifier.send(data, Config.exception_path)
29
29
  rescue Exception => e
30
30
  Rlogger.error("[NOTIFY] #{e.message}")
31
31
  Rlogger.error("[NOTIFY] #{e.backtrace}")
@@ -36,15 +36,14 @@ module Rnotifier
36
36
  def rack_exception_data
37
37
  data = {}
38
38
  data[:request] = {
39
- :url => request.url,
40
- :referer_url => request.referer,
41
- :ip => request.ip,
42
- :http_method => "#{request.request_method}#{' # XHR' if request.xhr?}",
43
- :params => filtered_params,
44
- :headers => self.headers,
45
- :session => request.session
39
+ :url => request.url,
40
+ :referer_url => request.referer,
41
+ :ip => request.ip,
42
+ :http_method => "#{request.request_method}#{' # XHR' if request.xhr?}",
43
+ :params => filtered_params,
44
+ :headers => self.headers,
45
+ :session => request.session
46
46
  }
47
-
48
47
  data
49
48
  end
50
49
 
@@ -55,7 +54,7 @@ module Rnotifier
55
54
  :backtrace => exception.backtrace,
56
55
  :fingerprint => (self.fingerprint rescue nil)
57
56
  }
58
- e_data[:code] = ExceptionCode.get(e_data[:backtrace]) if Config.capture_code
57
+ e_data[:code] = ExceptionCode.get(exception) if Config.capture_code
59
58
  e_data
60
59
  end
61
60
 
@@ -8,9 +8,9 @@ module Rnotifier
8
8
  end
9
9
  end
10
10
 
11
- def send(data, url = nil)
11
+ def send(data, path)
12
12
  response = self.connection.post do |req|
13
- req.url(url || Config.notification_path)
13
+ req.url(path)
14
14
  req.headers['Content-Type'] = 'application/json'
15
15
  req.headers['Api-Key'] = Config.api_key
16
16
  req.options[:timeout] = Config[:http_open_timeout]
@@ -1,6 +1,7 @@
1
1
  module Rnotifier
2
2
  class Rlogger
3
3
  TAG = '[RNOTIFIER]'
4
+
4
5
  class << self
5
6
 
6
7
  ['info', 'error', 'warn'].each do |level|
@@ -1,3 +1,3 @@
1
1
  module Rnotifier
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/rnotifier.rb CHANGED
@@ -50,12 +50,16 @@ module Rnotifier
50
50
  Rnotifier::ExceptionData.new(exception, params, {:type => :rescue}).notify
51
51
  end
52
52
 
53
- def event(name, params = {})
54
- Rnotifier::EventData.new(name, Rnotifier::EventData::EVENT, params).notify if Rnotifier::Config.valid?
53
+ def event(name, params, tags = {})
54
+ if Rnotifier::Config.valid? && params.is_a?(Hash)
55
+ Rnotifier::EventData.new(name, Rnotifier::EventData::EVENT, params, tags[:tags]).notify
56
+ end
55
57
  end
56
58
 
57
- def alert(name, params = {})
58
- Rnotifier::EventData.new(name, Rnotifier::EventData::ALERT, params).notify if Rnotifier::Config.valid?
59
+ def alert(name, params, tags = {})
60
+ if Rnotifier::Config.valid? && params.is_a?(Hash)
61
+ Rnotifier::EventData.new(name, Rnotifier::EventData::ALERT, params, tags[:tags]).notify
62
+ end
59
63
  end
60
64
 
61
65
  end
data/spec/config_spec.rb CHANGED
@@ -4,9 +4,11 @@ require 'spec_helper'
4
4
  describe Rnotifier::Config do
5
5
  before(:all) do
6
6
  @api_key = 'API-KEY'
7
- @notification_path = '/' + [ Rnotifier::Config::DEFAULT[:api_version],
8
- Rnotifier::Config::DEFAULT[:notify_path],
9
- @api_key].join('/')
7
+ @exception_path = '/' + [ Rnotifier::Config::DEFAULT[:api_version],
8
+ Rnotifier::Config::DEFAULT[:exception_path]].join('/')
9
+
10
+ @event_path = '/' + [ Rnotifier::Config::DEFAULT[:api_version],
11
+ Rnotifier::Config::DEFAULT[:event_path]].join('/')
10
12
  end
11
13
 
12
14
  before(:each) do
@@ -25,7 +27,8 @@ describe Rnotifier::Config do
25
27
  it 'has default config values' do
26
28
  Rnotifier::Config.tap do |c|
27
29
  expect(c.current_env).to eq @environments
28
- expect(c.notification_path).to eq @notification_path
30
+ expect(c.exception_path).to eq @exception_path
31
+ expect(c.event_path).to eq @event_path
29
32
  expect(c.api_key).to eq @api_key
30
33
  end
31
34
  end
@@ -53,7 +56,7 @@ describe Rnotifier::Config do
53
56
  Rnotifier.load_config("#{Dir.pwd}/spec/fixtures/rnotifier.yaml")
54
57
 
55
58
  expect(Rnotifier::Config.api_key).to eq @api_key
56
- expect(Rnotifier::Config.environments).to eq ['production', 'staging']
59
+ expect(Rnotifier::Config.environments).to eq ['production', 'staging', 'test']
57
60
  expect(Rnotifier::Config.valid?).to be_true
58
61
  end
59
62
 
@@ -19,13 +19,29 @@ describe Rnotifier::EventData do
19
19
  end
20
20
  end
21
21
 
22
- it 'sends event data to server' do
23
- path = '/' + [ Rnotifier::Config::DEFAULT[:api_version], Rnotifier::Config::DEFAULT[:event_path], 'API-KEY'].join('/')
24
- stubs = stub_faraday_request({:path => path})
22
+ it 'is initialize event_data object with tags' do
23
+ [:event, :alert].each do |e|
24
+ e_data = Rnotifier::EventData.new(@name, e, @data, [:create, :update])
25
25
 
26
+ expect(e_data.data[:name]).to eq @name
27
+ expect(e_data.data[:data]).to eq @data
28
+ expect(e_data.data[:type]).to eq e
29
+ expect(e_data.data[:tags]).to eq [:create, :update]
30
+ end
31
+ end
32
+
33
+ it 'sends event data to server' do
34
+ stubs = stub_faraday_request({:path => Rnotifier::Config.event_path})
26
35
  Rnotifier::EventData.new(@name, @data).notify
27
36
 
28
37
  expect { stubs.verify_stubbed_calls }.to_not raise_error
29
38
  end
30
39
 
40
+ it 'sends event data with tags to server' do
41
+ stubs = stub_faraday_request({:path => Rnotifier::Config.event_path})
42
+ Rnotifier.event(@name, @data, {:tags => [:new]})
43
+
44
+ expect { stubs.verify_stubbed_calls }.to_not raise_error
45
+ end
46
+
31
47
  end
@@ -17,6 +17,12 @@ module RnotifierTest
17
17
  [200, {}, 'OK']
18
18
  end
19
19
 
20
+ get '/event' do
21
+ Rnotifier.event(:product, {:id => 1, :name => 'PS3' }, [:create])
22
+
23
+ [200, {}, 'OK']
24
+ end
25
+
20
26
  end
21
27
 
22
28
  end
@@ -1,2 +1,2 @@
1
1
  apikey: API-KEY
2
- environments: production,staging
2
+ environments: production,staging,test
data/spec/spec_helper.rb CHANGED
@@ -34,7 +34,7 @@ end
34
34
  def stub_faraday_request(opts = {})
35
35
  opts[:status] ||= 200
36
36
  opts[:message] ||= 'ok'
37
- opts[:path] ||= '/' + [ Rnotifier::Config::DEFAULT[:api_version], Rnotifier::Config::DEFAULT[:notify_path], 'API-KEY'].join('/')
37
+ opts[:path] ||= '/' + [ Rnotifier::Config::DEFAULT[:api_version], Rnotifier::Config::DEFAULT[:exception_path]].join('/')
38
38
 
39
39
  stubs = Faraday::Adapter::Test::Stubs.new
40
40
  conn = Faraday.new do |builder|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rnotifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jiren Patel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-10 00:00:00.000000000 Z
11
+ date: 2013-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday