idobata_hook 0.0.1

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: 8e2c15c01a918c8cf2894618e31a710025cf57cd
4
+ data.tar.gz: a0ee2cafffdabfac256fe98d5a9bc583d53b49ed
5
+ SHA512:
6
+ metadata.gz: 4c549fcf485c2fbfda2b497337e5952dc169a24518c9c16a98834cad6224c2205cb6c54525534f0fa380ffa5860a18cb8ad0a3905da92a31fab24f5b94842029
7
+ data.tar.gz: 9e25eae5b251381c6bc0e73983fd096bd1cefb55a183ce9bf2eca2a92d6abced5f9c2af72c92a0356191da88a078e320fb3b254babb9811abb03444e96d72e8c
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1
5
+ script: bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in idobata_hook.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 fukayatsu
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.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # IdobataHook
2
+
3
+ A client for Idobata's Generic Hook
4
+
5
+ [![Build Status](https://travis-ci.org/fukayatsu/idobata_hook.svg?branch=master)](https://travis-ci.org/fukayatsu/idobata_hook)
6
+ [![Coverage Status](https://img.shields.io/coveralls/fukayatsu/idobata_hook.svg)](https://coveralls.io/r/fukayatsu/idobata_hook)
7
+ [![Code Climate](https://codeclimate.com/github/fukayatsu/idobata_hook.png)](https://codeclimate.com/github/fukayatsu/idobata_hook)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'idobata_hook'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install idobata_hook
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ client = IdobataHook::Client.new("https://idobata.io/hook/generic/***")
27
+ client.send('hello, world')
28
+ client.send("<h1>hi</h1>", format: :html)
29
+ client.send('Look at this', image_path: '/path/to/image.png')
30
+ client.send([
31
+ {'label-success' => 'build passing!' }, #=> <span class="label label-success">build passing!</span>
32
+ { badge: '123' }, #=> <span class="badge">123</span>
33
+ { emoji: 'smile'}, #=> <img class="emoji" alt=":smile:" src="/images/emoji/smile.png" height="20" width="20">
34
+ "Foooo!", #=> Foooo!
35
+ { fa: 'camera-retro' }, #=> <i class="fa fa-camera-retro"></i>
36
+ ])
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it ( https://github.com/fukayatsu/idobata_hook/fork )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'idobata_hook/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "idobata_hook"
8
+ spec.version = IdobataHook::VERSION
9
+ spec.authors = ["fukayatsu"]
10
+ spec.email = ["fukayatsu@gmail.com"]
11
+ spec.summary = %q{A client for Idobata's Generic Hook}
12
+ spec.description = %q{A client for Idobata's Generic Hook}
13
+ spec.homepage = "https://github.com/fukayatsu/idobata_hook"
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_dependency 'faraday', '~> 0.9.0'
22
+ spec.add_dependency 'mime-types', '~> 1.25.1'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency 'rspec', '~> 3.0.0'
28
+ spec.add_development_dependency 'coveralls'
29
+ end
@@ -0,0 +1,39 @@
1
+ require 'faraday'
2
+ require 'mime/types'
3
+ require 'idobata_hook/source'
4
+
5
+ module IdobataHook
6
+ class Client
7
+ def initialize(hook_url)
8
+ @hook_url = hook_url
9
+ end
10
+
11
+ def send(messages, format: nil, image_path: nil)
12
+ source = Source.new(messages)
13
+ params = { source: source.to_s }
14
+ params[:format] = :html if format.to_s == 'html' || source.force_html?
15
+ params[:image] = image_io(image_path) if image_path
16
+ http_client.post(nil, params)
17
+ end
18
+
19
+ private
20
+
21
+ def image_io(image_path)
22
+ mime_type = MIME::Types.type_for(image_path).first.simplified
23
+ Faraday::UploadIO.new(image_path, mime_type)
24
+ end
25
+
26
+ def http_client
27
+ @faraday ||= Faraday.new(url: @hook_url, headers: default_headers) do |conn|
28
+ conn.request :multipart
29
+ conn.request :url_encoded # form-encode POST params
30
+ conn.response :logger # log requests to STDOUT
31
+ conn.adapter Faraday.default_adapter # make requests with Net::HTTP
32
+ end
33
+ end
34
+
35
+ def default_headers
36
+ { 'User-Agent' => "RubyGems::IdobataHook / #{IdobataHook::VERSION} " }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,51 @@
1
+ module IdobataHook
2
+ class Source
3
+ def initialize(*messages)
4
+ @text, @force_html = parse(messages.flatten)
5
+ end
6
+
7
+ def force_html?
8
+ @force_html
9
+ end
10
+
11
+ def to_s
12
+ @text
13
+ end
14
+
15
+ def parse(messages)
16
+ force_html = false
17
+ text = messages.map do |message|
18
+ next message if message.is_a? String
19
+ force_html ||= true
20
+ Source.htmlize(message)
21
+ end.join
22
+ [text, force_html]
23
+ end
24
+
25
+ class << self
26
+ def htmlize(message)
27
+ return message if message.is_a? String
28
+
29
+ type, text = message.first
30
+ case type.to_s
31
+ when 'label'
32
+ %(<span class="label">#{text}</span>)
33
+ when /label-(.+)/
34
+ %(<span class="label label-#{$1}">#{text}</span>)
35
+ when 'badge'
36
+ %(<span class="badge">#{text}</span>)
37
+ when /badge-(.+)/
38
+ %(<span class="badge badge-#{$1}">#{text}</span>)
39
+ when 'emoji'
40
+ %(<img class="emoji" alt=":#{text}:" src="/images/emoji/#{text}.png">)
41
+ when 'fa'
42
+ %(<i class="fa fa-#{text}"></i>)
43
+ when 'commit-id'
44
+ %(<span class="commit-id">#{text}</span>)
45
+ else
46
+ raise "unsupported type: #{type}"
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module IdobataHook
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ require "idobata_hook/version"
2
+ require 'idobata_hook/client'
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe IdobataHook::Client do
4
+ subject(:client) { described_class.new('https://idobata.io/hook/generic/api-key-123abc') }
5
+
6
+ describe '#send' do
7
+ it 'send plain text' do
8
+ expect(subject).to receive_message_chain(:http_client, :post).with(nil, source: "text message")
9
+ response = client.send('text message')
10
+ end
11
+
12
+ it 'send html text' do
13
+ expect(subject).to receive_message_chain(:http_client, :post).with(nil, source: "<h1>html</h1>", format: :html)
14
+ response = client.send('<h1>html</h1>', format: :html)
15
+ end
16
+
17
+ it 'send text and image' do
18
+ expect(subject).to receive(:image_io).with('~/sushi.png') { '[sushi io]' }
19
+ expect(subject).to receive_message_chain(:http_client, :post).with(nil, source: "Look at this!", image: '[sushi io]')
20
+ response = client.send('Look at this!', image_path: '~/sushi.png')
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe IdobataHook::Source do
4
+ describe '#new' do
5
+ subject(:source) { described_class.new(message) }
6
+
7
+ context 'only strings' do
8
+ let(:message) { ['aaa', 'bbb'] }
9
+ it 'joins strings' do
10
+ expect(subject.to_s).to eq('aaabbb')
11
+ end
12
+ it 'not force_html' do
13
+ expect(subject.force_html?).to be false
14
+ end
15
+ end
16
+
17
+ context 'with label' do
18
+ let(:message) { ['aaa', {label: 'bar'}, 'baz'] }
19
+ it 'joins strings and html' do
20
+ expect(subject.to_s).to eq('aaa<span class="label">bar</span>baz')
21
+ end
22
+ it 'force_html' do
23
+ expect(subject.force_html?).to be true
24
+ end
25
+ end
26
+ end
27
+
28
+ describe '.htmlize' do
29
+ it 'just a string' do
30
+ expect(described_class.htmlize('abc'))
31
+ .to eq('abc')
32
+ end
33
+ it 'default label' do
34
+ expect(described_class.htmlize({label: 'foo'}))
35
+ .to eq('<span class="label">foo</span>')
36
+ end
37
+ it 'important label' do
38
+ expect(described_class.htmlize({'label-important' => 'bar'}))
39
+ .to eq('<span class="label label-important">bar</span>')
40
+ end
41
+ it 'default badge' do
42
+ expect(described_class.htmlize({badge: '123'}))
43
+ .to eq('<span class="badge">123</span>')
44
+ end
45
+ it 'important badge' do
46
+ expect(described_class.htmlize({'badge-important' => '234'}))
47
+ .to eq('<span class="badge badge-important">234</span>')
48
+ end
49
+ it 'emoji' do
50
+ expect(described_class.htmlize({emoji: :smile}))
51
+ .to eq('<img class="emoji" alt=":smile:" src="/images/emoji/smile.png">')
52
+ end
53
+ it 'font awesome' do
54
+ expect(described_class.htmlize({fa: 'camera-retro'}))
55
+ .to eq('<i class="fa fa-camera-retro"></i>')
56
+ end
57
+ it 'commit-id' do
58
+ expect(described_class.htmlize({'commit-id' => '123abc'}))
59
+ .to eq('<span class="commit-id">123abc</span>')
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,82 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+ require 'idobata_hook'
4
+
5
+ # This file was generated by the `rspec --init` command. Conventionally, all
6
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
7
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
8
+ # file to always be loaded, without a need to explicitly require it in any files.
9
+ #
10
+ # Given that it is always loaded, you are encouraged to keep this file as
11
+ # light-weight as possible. Requiring heavyweight dependencies from this file
12
+ # will add to the boot time of your test suite on EVERY test run, even for an
13
+ # individual file that may not need all of that loaded. Instead, make a
14
+ # separate helper file that requires this one and then use it only in the specs
15
+ # that actually need it.
16
+ #
17
+ # The `.rspec` file also contains a few flags that are not defaults but that
18
+ # users commonly want.
19
+ #
20
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
21
+ RSpec.configure do |config|
22
+ # The settings below are suggested to provide a good initial experience
23
+ # with RSpec, but feel free to customize to your heart's content.
24
+
25
+ # These two settings work together to allow you to limit a spec run
26
+ # to individual examples or groups you care about by tagging them with
27
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
28
+ # get run.
29
+ config.filter_run :focus
30
+ config.run_all_when_everything_filtered = true
31
+
32
+ # Many RSpec users commonly either run the entire suite or an individual
33
+ # file, and it's useful to allow more verbose output when running an
34
+ # individual spec file.
35
+ if config.files_to_run.one?
36
+ # Use the documentation formatter for detailed output,
37
+ # unless a formatter has already been configured
38
+ # (e.g. via a command-line flag).
39
+ config.default_formatter = 'doc'
40
+ end
41
+
42
+ # Print the 10 slowest examples and example groups at the
43
+ # end of the spec run, to help surface which specs are running
44
+ # particularly slow.
45
+ config.profile_examples = 10
46
+
47
+ # Run specs in random order to surface order dependencies. If you find an
48
+ # order dependency and want to debug it, you can fix the order by providing
49
+ # the seed, which is printed after each run.
50
+ # --seed 1234
51
+ config.order = :random
52
+
53
+ # Seed global randomization in this process using the `--seed` CLI option.
54
+ # Setting this allows you to use `--seed` to deterministically reproduce
55
+ # test failures related to randomization by passing the same `--seed` value
56
+ # as the one that triggered the failure.
57
+ Kernel.srand config.seed
58
+
59
+ # rspec-expectations config goes here. You can use an alternate
60
+ # assertion/expectation library such as wrong or the stdlib/minitest
61
+ # assertions if you prefer.
62
+ config.expect_with :rspec do |expectations|
63
+ # Enable only the newer, non-monkey-patching expect syntax.
64
+ # For more details, see:
65
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
66
+ expectations.syntax = :expect
67
+ end
68
+
69
+ # rspec-mocks config goes here. You can use an alternate test double
70
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
71
+ config.mock_with :rspec do |mocks|
72
+ # Enable only the newer, non-monkey-patching expect syntax.
73
+ # For more details, see:
74
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
75
+ mocks.syntax = :expect
76
+
77
+ # Prevents you from mocking or stubbing a method that does not exist on
78
+ # a real object. This is generally recommended.
79
+ mocks.verify_partial_doubles = true
80
+ end
81
+
82
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: idobata_hook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - fukayatsu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: mime-types
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.25.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.25.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 3.0.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
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
+ description: A client for Idobata's Generic Hook
112
+ email:
113
+ - fukayatsu@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".coveralls.yml"
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - idobata_hook.gemspec
127
+ - lib/idobata_hook.rb
128
+ - lib/idobata_hook/client.rb
129
+ - lib/idobata_hook/source.rb
130
+ - lib/idobata_hook/version.rb
131
+ - spec/idobata_hook/client_spec.rb
132
+ - spec/idobata_hook/source_spec.rb
133
+ - spec/spec_helper.rb
134
+ homepage: https://github.com/fukayatsu/idobata_hook
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.2.2
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: A client for Idobata's Generic Hook
158
+ test_files:
159
+ - spec/idobata_hook/client_spec.rb
160
+ - spec/idobata_hook/source_spec.rb
161
+ - spec/spec_helper.rb