bearychat-notifier 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7b40c1123177c0d04ca7651d331068fe9ed9ad40
4
+ data.tar.gz: f6bce4326649ae5c2a34b4119a5ebfddfe0478e3
5
+ SHA512:
6
+ metadata.gz: 3075dbd6a829f9464c65d51b799ed042f1af11028aece5e975585585fdcd085726c614332c6399ec2af9aa6cfd2f82a1a6217b4a4c2c31b1392381b228fc9150
7
+ data.tar.gz: 2c07a253b953baa050809f28c0b8e5dc9a8566b98af557301ecc762e3b1e5e80171081ef83754f82a020240f502c2dc730362345cbaf8923972ab8857dad516b
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bearychat-notifier.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 villins
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ # Bearychat::Notifier
2
+
3
+ A simple wrapper for posting to bearychat channels
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'bearychat-notifier'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install bearychat-notifier
18
+
19
+ ## Usage
20
+ ```ruby
21
+ options = {
22
+ text: "text, this field may accept markdown",
23
+ markdown: true,
24
+ channel: "bearychat-dev",
25
+ attachments: [
26
+ {
27
+ title: "title_1",
28
+ text: "attachment_text",
29
+ color: "#ffffff",
30
+ images: [
31
+ {"url": "http://example.com/index.jpg"}
32
+ ]
33
+ }]
34
+ }
35
+
36
+ @notifier = Bearychat::Notifier.new "webhook_url", options
37
+
38
+ @notifier.ping "Hello world"
39
+ ```
40
+
41
+ ## with exception_notification
42
+ ```ruby
43
+ Rails.application.config.middleware.use ExceptionNotification::Rack,
44
+ :email => {
45
+ :email_prefix => "[PREFIX] ",
46
+ :sender_address => %{"notifier" <notifier@example.com>},
47
+ :exception_recipients => %w{exceptions@example.com}
48
+ },
49
+ :bearychat => {
50
+ text: "hello world",
51
+ markdown: true,
52
+ channel: "all",
53
+ attachments: []
54
+ }
55
+ ```
56
+
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( https://github.com/[my-github-username]/bearychat-notifier/fork )
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create a new Pull Request
65
+ # bearychat-notifier
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bearychat-notifier/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bearychat-notifier"
8
+ spec.version = Bearychat::Notifier::VERSION
9
+ spec.authors = ["villins"]
10
+ spec.email = ["linshao512@gmail.com"]
11
+ spec.summary = %q{A simple wrapper for posting to bearychat channels}
12
+ spec.description = %q{A simple wrapper for posting to bearychat channels}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'json'
5
+ require 'bearychat-notifier/http_client'
6
+
7
+ module Bearychat
8
+ class Notifier
9
+ attr_reader :webhook_url, :payload
10
+
11
+ def initialize(webhook_url, options = {})
12
+ @webhook_url = webhook_url
13
+ @payload = options
14
+ end
15
+
16
+ def ping(text, options)
17
+ payload.merge!(options)
18
+ payload.merge!(text: text)
19
+ params = { payload: payload.to_json }
20
+ HttpClient.post(webhook_url, params)
21
+ end
22
+
23
+ def channel
24
+ payload[:channel]
25
+ end
26
+
27
+ def channel=(channel)
28
+ payload[:channel] = channel
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,44 @@
1
+ # coding: utf-8
2
+ module Bearychat
3
+ class Notifier
4
+ class HttpClient
5
+ class << self
6
+ def post(uri, params)
7
+ HttpClient.new(uri, params).call
8
+ end
9
+ end
10
+
11
+ attr_reader :uri, :params, :http_options
12
+
13
+ def initialize(url, params)
14
+ @uri = url
15
+ @http_options = params.delete(:http_options) || {}
16
+ @params = params
17
+ end
18
+
19
+ def call
20
+ http_post
21
+ end
22
+
23
+ private
24
+ def http_post
25
+ url = URI.parse(uri)
26
+ post_obj = Net::HTTP::Post.new(url.path)
27
+ post_obj.set_form_data(params)
28
+
29
+ socket = Net::HTTP.new(url.host, url.port)
30
+ socket.use_ssl = true if url.scheme.downcase == "https"
31
+
32
+ http_options.each do |opt, val|
33
+ if http.respond_to? "#{opt}="
34
+ http.send "#{opt}=", val
35
+ else
36
+ warn "Net::HTTP doesn't respond to `#{opt}=`, ignoring that option"
37
+ end
38
+ end
39
+
40
+ response = socket.start {|http| http.request(post_obj) }
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,6 @@
1
+ # coding: utf-8
2
+ module Bearychat
3
+ class Notifier
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
@@ -0,0 +1,81 @@
1
+ # coding: utf-8
2
+ module ExceptionNotifier
3
+ class BearychatNotifier
4
+ #include ExceptionNotifier::BacktraceCleaner # 不在 exception_notification 手动添加代码过来
5
+
6
+ attr_accessor :notifier
7
+
8
+ def initialize(options)
9
+ begin
10
+ @ignore_data_if = options[:ignore_data_if]
11
+
12
+ webhook_url = options.fetch(:webhook_url)
13
+ options.delete(:webhook_url)
14
+ @payload_options = options
15
+ @payload_options[:attachments] = options.fetch(:attachments, [])
16
+ @notifier = Bearychat::Notifier.new webhook_url, options
17
+ rescue
18
+ @notifier = nil
19
+ end
20
+ end
21
+
22
+ def call(exception, options={})
23
+ text = "An exception occurred: '#{exception.message}' on '#{exception.backtrace.first}'"
24
+
25
+ text = enrich_message_with_data(message, options)
26
+
27
+ attachment_options = {}
28
+ attachment_options[:text] = enrich_message_with_backtrace(message, exception)
29
+ @payload_options[:attachments].push(attachment_options)
30
+ @attachments[:text] = attachments_text
31
+
32
+ @notifier.ping(message, @attachments) if valid?
33
+ end
34
+
35
+ protected
36
+
37
+ def valid?
38
+ !@notifier.nil?
39
+ end
40
+
41
+ def enrich_message_with_data(message, options)
42
+ def deep_reject(hash, block)
43
+ hash.each do |k, v|
44
+ if v.is_a?(Hash)
45
+ deep_reject(v, block)
46
+ end
47
+
48
+ if block.call(k, v)
49
+ hash.delete(k)
50
+ end
51
+ end
52
+ end
53
+
54
+ data = ((options[:env] || {})['exception_notifier.exception_data'] || {}).merge(options[:data] || {})
55
+ deep_reject(data, @ignore_data_if) if @ignore_data_if.is_a?(Proc)
56
+ text = data.map{|k,v| "#{k}: #{v}"}.join(', ')
57
+
58
+ if text.present?
59
+ text = ['*Data:*', text].join("\n")
60
+ [message, text].join("\n")
61
+ else
62
+ message
63
+ end
64
+ end
65
+
66
+ def enrich_message_with_backtrace(message, exception)
67
+ backtrace = clean_backtrace(exception).first(10).join("\n")
68
+ [message, ['*Backtrace:*', backtrace]].join("\n")
69
+ end
70
+
71
+ def clean_backtrace(exception)
72
+ if defined?(Rails) && Rails.respond_to?(:backtrace_cleaner)
73
+ Rails.backtrace_cleaner.send(:filter, exception.backtrace)
74
+ else
75
+ exception.backtrace
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bearychat-notifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - villins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-16 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A simple wrapper for posting to bearychat channels
42
+ email:
43
+ - linshao512@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - bearychat-notifier.gemspec
54
+ - lib/bearychat-notifier.rb
55
+ - lib/bearychat-notifier/http_client.rb
56
+ - lib/bearychat-notifier/version.rb
57
+ - lib/exception_notifier/bearychat_notifier.rb
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.4.8
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: A simple wrapper for posting to bearychat channels
82
+ test_files: []