exception_notification-ikachan 0.0.1 → 0.1.0

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: d36d1fcfabd7fab2c2fd82a5ac0bcf93bc9ac8f7
4
- data.tar.gz: 322666c6c4a3b7c7ab4e2d2938a38163ab1a2719
3
+ metadata.gz: 174d70e10eacc6df6566279d6eed3f8f7cb7aae5
4
+ data.tar.gz: 83a1c7f985f431272dadf45ce13196d490a6e1b0
5
5
  SHA512:
6
- metadata.gz: 207b882420e3f7562c28a98cb464b1e6fba2b69342f4cab8ea60b6c91545572d66c7e7a04e4163ddc0bd4ba162a007c639a035e58e44c23cf6d6b207dd16a524
7
- data.tar.gz: ea8f7a77c755159f3a13f3d34c355db759cfd618855e3f03ed75e799fdfaa0ae8021d5bf8946e003f1da15db41d6d54d053461456b24428f7fea0192d080c80f
6
+ metadata.gz: f58b929cca9b113f786485fb9b87eb95174a27dedb0ad045014d16b6c0b6ecd2bbdb25abb661d58fa1b018444afc4537ecb7b6d794a9aa62861917ad8f8ab6b6
7
+ data.tar.gz: 7104a68e2760be7033df27d9b85ad5a710e59ea78982ef9fa9e037ca25f8bd441ff7b4346da47af81d79d3497d031c21a6db002113e43ee6d71876eeabcc4260
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # ExceptionNotification::Ikachan
2
2
 
3
- ExceptionNotification plugin for Ikachan!!!
3
+ ExceptionNotification plugin for Ikachan!!!
4
+
5
+ [![wercker status](https://app.wercker.com/status/6e059ec136a619b280a4f5b05e4a685b/m "wercker status")](https://app.wercker.com/project/bykey/6e059ec136a619b280a4f5b05e4a685b)
4
6
 
5
7
  ## Installation
6
8
 
@@ -30,6 +32,26 @@ end
30
32
 
31
33
  `:message_format` like `"%{class}: %{message}"` also available.
32
34
 
35
+ ### Message modifiers
36
+
37
+ * `:message_prefix` - Adds prefix to default format
38
+ * `:message_suffix` - Adds suffix to default format
39
+ * `:message_nocolor` - Decolorize the format
40
+
41
+ ## Keys available in `:message_format`
42
+
43
+ * `%{class}` - Exception class
44
+ * `%{messgae}` - Exception message
45
+ * `%{occurred}` - A line that the exception is first thrown (`exception.backtrace.first`)
46
+
47
+ ### Request keys
48
+
49
+ Nofitier can notify the information via web requests.
50
+ Keys named like `'%{request_path_info}', %{request_url}'` will be
51
+ converted to descriptions from `request.path_info, request.url`, and so on.
52
+
53
+ `request` should be an instance of `ActionDispatch::Request` (Rails) or `Rack::Request` (Other Rack apps)
54
+
33
55
  ## Contributing
34
56
 
35
57
  1. Fork it
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rake"
25
25
  spec.add_development_dependency "rspec"
26
26
  spec.add_development_dependency "webmock"
27
+ spec.add_development_dependency "rack"
27
28
  end
@@ -1,5 +1,5 @@
1
1
  module ExceptionNotification
2
2
  class Ikachan
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -40,26 +40,60 @@ module ExceptionNotifier
40
40
  @channels = channel.is_a?(Array) ? channel : [channel]
41
41
  @client = Client.new(options[:base_url])
42
42
  @message_format = build_message_format(options)
43
- end
44
- attr_reader :client, :channels, :message_format
43
+ @message = nil
45
44
 
46
- def call(exception, options = {})
47
- client.notice_all(channels, build_message(exception))
45
+ @request_param_names = message_format.scan(/%{(request_[a-zA-Z_?!]+)}/).flatten.uniq
46
+ @request_param_names.map{|n| [n, n.sub(/^request_/, '')] }.each do |param_name, attribute|
47
+ raise "Parameter name #{param_name} is unavailable" unless request_klass.method_defined?(attribute)
48
+ end
48
49
  end
50
+ attr_reader :client, :channels, :message_format, :message
51
+ DEFAULT_FORMAT = "\x02\x0315,4[ERROR]\x03 \x0313%{class}\x03 - %{message}\x03\x0f, %{occurred}"
52
+ IRC_SEQUENCE_RE = Regexp.new("[\x02\x03\x0f](\\d+)?(,\\d+)?")
49
53
 
50
- private
51
- def build_message_format(options)
52
- return options[:message_format] if options[:message_format]
53
- "\x02\x0315,4[ERROR]\x03 \x0313%{class}\x03 - \x038%{message}\x03, %{occurred}\x0f"
54
+ def call(exception, options = {})
55
+ build_message(exception, options)
56
+ client.notice_all(channels, message)
54
57
  end
55
58
 
56
- def build_message(exception)
59
+ def build_message(exception, options = {})
57
60
  params = {
58
61
  class: exception.class,
59
62
  message: exception.message,
60
63
  occurred: (exception.backtrace.first rescue nil),
61
64
  }
62
- return message_format % params
65
+ params.merge!(build_params_from_request(options[:env])) if options[:env]
66
+ @message = message_format % params
67
+ end
68
+
69
+ private
70
+ def request_klass
71
+ @request_klass ||= if defined?(ActionDispatch::Request)
72
+ ActionDispatch::Request
73
+ else
74
+ require 'rack/request'
75
+ Rack::Request
76
+ end
77
+ rescue LoadError, NameError
78
+ raise "Please use this notifier in some kind of Rack-based webapp"
79
+ end
80
+
81
+ def build_message_format(options)
82
+ return options[:message_format] if options[:message_format]
83
+ DEFAULT_FORMAT.dup.tap do |fmt|
84
+ fmt.prepend(options[:message_prefix]) if options[:message_prefix]
85
+ fmt.concat(options[:message_suffix]) if options[:message_suffix]
86
+ fmt.gsub!(IRC_SEQUENCE_RE, '') if options[:message_nocolor]
87
+ end
88
+ end
89
+
90
+ def build_params_from_request(env)
91
+ request = request_klass.new(env)
92
+ dest = {}
93
+ @request_param_names.map{|n| [n, n.sub(/^request_/, '')] }.each do |param_name, attribute|
94
+ dest[param_name.to_sym] = request.send(attribute)
95
+ end
96
+ dest
63
97
  end
64
98
 
65
99
  # alias
@@ -17,6 +17,104 @@ describe ExceptionNotifier::IkachanNotifier do
17
17
  expect(notifier.client.base_url).to eq("http://ikachan.udzura.jp/")
18
18
  end
19
19
  end
20
+
21
+ describe "message with invalid request key" do
22
+ let(:options) do
23
+ {
24
+ base_url: 'ikachan.udzura.jp',
25
+ channel: '#udzura',
26
+ message_format: '%{request_noattr}'
27
+ }
28
+ end
29
+
30
+ it "should just raise error" do
31
+ expect {
32
+ notifier
33
+ }.to raise_error(RuntimeError, "Parameter name request_noattr is unavailable")
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "#build_message and #message" do
39
+ before do
40
+ require 'rack/request'
41
+ stub_request(:post, "http://ikachan.udzura.jp/join")
42
+ stub_request(:post, "http://ikachan.udzura.jp/notice")
43
+ end
44
+ let(:exception) { StandardError.new("Hello, exception!")}
45
+
46
+ let(:options) do
47
+ {
48
+ base_url: 'ikachan.udzura.jp',
49
+ channel: '#udzura',
50
+ }.merge(extra_options)
51
+ end
52
+
53
+ describe "message with request info" do
54
+ let(:extra_options) do
55
+ {message_format: '%{request_path_info}'}
56
+ end
57
+
58
+ it "should include request's path info" do
59
+ notifier.build_message(exception, {env: {'PATH_INFO' => '/foo/bar'}})
60
+ expect(notifier.message).to eq("/foo/bar")
61
+ end
62
+ end
63
+
64
+ describe "message with more request info" do
65
+ let(:env_sane) do
66
+ {"HTTP_HOST" => "example.udzura.jp:80", "rack.url_scheme" => 'http', "PATH_INFO" => "/hello.html"}
67
+ end
68
+ let(:extra_options) do
69
+ {message_format: '%{request_url} / ssl? = %{request_ssl?}'}
70
+ end
71
+
72
+ it do
73
+ notifier.build_message(exception, {env: env_sane})
74
+ expect(notifier.message).to eq("http://example.udzura.jp/hello.html / ssl? = false")
75
+ end
76
+ end
77
+
78
+ describe "message modifiers" do
79
+ before { notifier.build_message(exception, {env: {'PATH_INFO' => '/foo/bar'}}) }
80
+ let(:default_message) do
81
+ ExceptionNotifier::IkachanNotifier::DEFAULT_FORMAT % {
82
+ class: "StandardError",
83
+ message: "Hello, exception!",
84
+ occurred: ''
85
+ }
86
+ end
87
+
88
+ describe ':message_prefix' do
89
+ let(:extra_options) do
90
+ { message_prefix: 'hello - ' }
91
+ end
92
+
93
+ it do
94
+ expect(notifier.message).to eq('hello - ' + default_message)
95
+ end
96
+ end
97
+
98
+ describe ':message_suffix' do
99
+ let(:extra_options) do
100
+ { message_suffix: ' - world' }
101
+ end
102
+
103
+ it do
104
+ expect(notifier.message).to eq(default_message + ' - world')
105
+ end
106
+ end
107
+
108
+ describe ':message_nocolor' do
109
+ let(:extra_options) do
110
+ { message_nocolor: true }
111
+ end
112
+
113
+ it do
114
+ expect(notifier.message).to eq("[ERROR] StandardError - Hello, exception!, ")
115
+ end
116
+ end
117
+ end
20
118
  end
21
119
 
22
120
  describe "#call" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: exception_notification-ikachan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uchio KONDO
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-18 00:00:00.000000000 Z
11
+ date: 2014-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: exception_notification
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: ExceptionNotification ikachan plugin
84
98
  email:
85
99
  - udzura@paperboy.co.jp