tumugi-plugin-webhook 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: 6bfa6d9f2a87159a2f6ffae0a07aa29e5ecf2466
4
+ data.tar.gz: 5137b0b4ccab9e6944eceb3ace1e01feb579e98b
5
+ SHA512:
6
+ metadata.gz: b91705e5abc8cb2690d638ab7f2f4062873ba1cbc0abf3b0c4d518c238f57bf15e8d5ba98554b5bf272cca8b39d2d5c3965197ee9f2696c210653f8795ed0d75
7
+ data.tar.gz: a1653b0619d0b46f9fa9e7cffcca891eef1eee426623b41bc69cf8984d379061a86316cddaa0cd52353c1891b56291a62163b42d3ed6634e1f09676370b00c56
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *~
2
+ /.bundle/
3
+ /Gemfile.lock
4
+ /pkg/
5
+ /tmp/
6
+ .ruby-version
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.1
5
+ - 2.3.1
6
+ - jruby-9.1.2.0
7
+ before_install:
8
+ - gem install bundler
9
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tumugi-plugin-webhook.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ [![Build Status](https://travis-ci.org/tumugi/tumugi-plugin-webhook.svg?branch=master)](https://travis-ci.org/tumugi/tumugi-plugin-webhook) [![Code Climate](https://codeclimate.com/github/tumugi/tumugi-plugin-webhook/badges/gpa.svg)](https://codeclimate.com/github/tumugi/tumugi-plugin-webhook) [![Coverage Status](https://coveralls.io/repos/github/tumugi/tumugi-plugin-webhook/badge.svg?branch=master)](https://coveralls.io/github/tumugi/tumugi-plugin-webhook) [![Gem Version](https://badge.fury.io/rb/tumugi-plugin-webhook.svg)](https://badge.fury.io/rb/tumugi-plugin-webhook)
2
+
3
+ # WebHook plugin for [tumugi](https://github.com/tumugi/tumugi)
4
+
5
+ tumugi-plugin-webhook is a plugin for integrate various WebHooks and [tumugi](https://github.com/tumugi/tumugi).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'tumugi-plugin-webhook'
13
+ ```
14
+
15
+ And then execute `bundle install`.
16
+
17
+ ## Task
18
+
19
+ ### Tumugi::Plugin::WebhookTask
20
+
21
+ `Tumugi::Plugin::WebhookTask` is a task to send a HTTP request for any WebHooks.
22
+
23
+ #### Parameters
24
+
25
+ - **url** WebHook URL (string, required)
26
+ - **http_method** HTTP method (string, optional, default: "post")
27
+ - **body** WebHook body (string or hash, optional, default: `nil`)
28
+ - **body_encoding** WebHook body encoding type (string, optional, default: "json")
29
+ - "json": MIME type is `application/json`
30
+ - "url_encoded": MIME type is `application/x-www-form-urlencoded`
31
+
32
+ #### Example
33
+
34
+ ```rb
35
+ task :main, type: :webhook do
36
+ url "http://httpbin.org/post"
37
+ body {
38
+ { text: "message" }
39
+ }
40
+ end
41
+ ```
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bundle install` to install dependencies.
46
+ Then run `bundle exec rake test` to run the tests.
47
+
48
+ Run this plugin with tumugi, run
49
+
50
+ ```
51
+ $ bundle exec tumugi run -f examples/example.rb main
52
+ ```
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [Apache License
57
+ Version 2.0](http://www.apache.org/licenses/).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,6 @@
1
+ task :main, type: :webhook do
2
+ url "http://httpbin.org/post"
3
+ body {
4
+ { text: "message" }
5
+ }
6
+ end
@@ -0,0 +1,62 @@
1
+ require 'tumugi'
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+ require 'uri'
5
+ require_relative '../webhook/logger'
6
+
7
+ module Tumugi
8
+ module Plugin
9
+ class WebhookTask < Tumugi::Task
10
+ Tumugi::Plugin.register_task('webhook', self)
11
+
12
+ METHODS = Set.new [:get, :post, :put, :delete]
13
+ BODY_ENCODINGS = Set.new [ :url_encoded, :json ]
14
+
15
+ param :url, type: :string, required: true
16
+ param :http_method, type: :string, default: "post"
17
+ param :body, type: :string
18
+ param :body_encoding, type: :string, default: "json"
19
+
20
+ def run
21
+ validate_parameters!
22
+
23
+ conn = Faraday.new(url: url) do |c|
24
+ c.request :retry
25
+ c.request body_encoding.downcase.to_sym
26
+ c.response :follow_redirects
27
+ c.response :webhook_logger, logger
28
+ c.adapter Faraday.default_adapter
29
+ end
30
+
31
+ m = http_method.downcase.to_sym
32
+ http_body = [:get, :delete].include?(m) ? nil : body
33
+ begin
34
+ res = conn.run_request(m, nil, http_body, nil)
35
+ if !res.success?
36
+ raise Tumugi::TumugiError.new("#{m} #{url} failed: #{res.status} #{res.body}")
37
+ end
38
+ rescue => e
39
+ raise Tumugi::TumugiError.new("#{m} #{url} failed", e)
40
+ end
41
+
42
+ if _output && _output.is_a?(Tumugi::Plugin::FileSystemTarget)
43
+ _output.open("w") do |f|
44
+ f.write(res.body)
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def validate_parameters!
52
+ if !METHODS.include?(http_method.downcase.to_sym)
53
+ raise Tumugi::TumugiError.new("Unsupported http method: #{http_method}")
54
+ end
55
+
56
+ if !BODY_ENCODINGS.include?(body_encoding.downcase.to_sym)
57
+ raise Tumugi::TumugiError.new("Unsupported body encoding: #{body_encoding}")
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,59 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module Tumugi
5
+ module Plugin
6
+ module Webhook
7
+ class Logger < Faraday::Response::Middleware
8
+ attr_reader :logger
9
+
10
+ def initialize(app, logger)
11
+ super(app)
12
+ @logger = logger
13
+ end
14
+
15
+ def call(env)
16
+ logger.info { "#{env[:method].upcase} #{env[:url]}" }
17
+ log_request(env)
18
+ super
19
+ end
20
+
21
+ def on_complete(env)
22
+ status = env[:status]
23
+ log_response_status(status) { "HTTP #{status}" }
24
+ log_response(env)
25
+ end
26
+
27
+ def log_request(env)
28
+ log(env[:request_headers], env[:body])
29
+ end
30
+
31
+ def log_response(env)
32
+ log(env[:response_headers], env[:body])
33
+ end
34
+
35
+ def log(headers, body)
36
+ logger.debug {
37
+ JSON.generate(
38
+ headers: headers,
39
+ body: body
40
+ )
41
+ }
42
+ end
43
+
44
+ def log_response_status(status, &block)
45
+ case status
46
+ when 200..399
47
+ logger.info(&block)
48
+ else
49
+ logger.error(&block)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ Faraday::Response.register_middleware({
58
+ webhook_logger: Tumugi::Plugin::Webhook::Logger
59
+ })
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "tumugi-plugin-webhook"
6
+ spec.version = "0.1.0"
7
+ spec.authors = ["Kazuyuki Honda"]
8
+ spec.email = ["hakobera@gmail.com"]
9
+
10
+ spec.summary = "WebHook plugin for tumugi"
11
+ spec.homepage = "https://github.com/hakobera/tumugi-plugin-webhook"
12
+ spec.license = "Apache License Version 2.0"
13
+
14
+ spec.required_ruby_version = '>= 2.1'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "tumugi", ">= 0.6.3"
22
+ spec.add_dependency "faraday", "~> 0.9.2"
23
+ spec.add_dependency "faraday_middleware", "~> 0.10.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.11"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "test-unit", "~> 3.1"
28
+ spec.add_development_dependency "test-unit-rr"
29
+ spec.add_development_dependency "coveralls"
30
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tumugi-plugin-webhook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kazuyuki Honda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tumugi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday_middleware
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.10.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.10.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: test-unit
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: test-unit-rr
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: coveralls
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - hakobera@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".travis.yml"
134
+ - Gemfile
135
+ - README.md
136
+ - Rakefile
137
+ - examples/example.rb
138
+ - lib/tumugi/plugin/task/webhook.rb
139
+ - lib/tumugi/plugin/webhook/logger.rb
140
+ - tumugi-plugin-webhook.gemspec
141
+ homepage: https://github.com/hakobera/tumugi-plugin-webhook
142
+ licenses:
143
+ - Apache License Version 2.0
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '2.1'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 2.5.1
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: WebHook plugin for tumugi
165
+ test_files: []