activemessenger 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd837b371e4bef5e1e78f431df378fcb228c91fb
4
+ data.tar.gz: 4ad812cb1e10aba2cf886ffa682af7e33bdfdc33
5
+ SHA512:
6
+ metadata.gz: 7c00ebbebfc39e8a1ff510b6d8739998efed4c29a55ffbabef35c78c5a8a5126c492b4bb5926c773ff83b662d53f1757dd2e33310fb516eaa45c12bd202e4dbf
7
+ data.tar.gz: 8e4afb046d3b9c499e3b10e7c42b38d9ae4141987885e9fc6636d0294b75dfc0cf958b1f44e728fb27ec1bf8a16594a6ebfd89781388fc04b3e0f8a96a586a96
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activemessenger.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Izumiya Keisuke
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,84 @@
1
+ # ActiveMessenger
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'activemessenger'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install activemessenger
20
+
21
+ ## Usage
22
+
23
+ Write sub-class of `ActiveMessenger::Base` and put under `app/massengers/`
24
+ sub-class like...
25
+ ```ruby
26
+ class ReportMessenger < ActiveMessenger::Base
27
+ adapter :adapter_name
28
+
29
+ def all_report
30
+ @reports = Report.all
31
+ end
32
+ end
33
+ ```
34
+
35
+ Create messenger instance and call action on your code.
36
+
37
+ ```ruby
38
+ messenger = ReportMessenger.new
39
+
40
+ messenger.say!("Hello, World") # Send message "Hello, World"
41
+
42
+ messenger.post!(:all_reoprt) # Rendering 'app/views/messengers/report/all_report.html.haml' and send it
43
+ ```
44
+
45
+ ## Configure
46
+ ### Hipchat
47
+ Set class attributes
48
+ ```ruby
49
+ class YourMessenger < ActiveMessenger::Base
50
+ adapter :hipchat
51
+
52
+ self.api_token = 'XXXXXXX'
53
+ self.room_id = 123456
54
+ self.user_name = 'hoge'
55
+ end
56
+ ```
57
+ Or write and put on config file
58
+
59
+ ```yaml
60
+ # config/active_messenger.yml
61
+ development:
62
+ hipchat:
63
+ api_token: XXXXXXX
64
+ room_id: 123456
65
+ user_name: dev
66
+ production:
67
+ hipchat:
68
+ api_token: XXXXXXX
69
+ room_id: 123456
70
+ user_name: hoge
71
+ test:
72
+ hipchat:
73
+ api_token: XXXXXXX
74
+ room_id: 123456
75
+ user_name: test
76
+ ```
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it ( https://github.com/[my-github-username]/activemessenger/fork )
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'active_messenger/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "activemessenger"
7
+ spec.version = ActiveMessenger::VERSION
8
+ spec.authors = ["Keisuke Izumiya"]
9
+ spec.email = ["ksk.i.530@gmail.com"]
10
+ spec.summary = %q{The wrapper of messaging application.}
11
+ spec.description = %q{}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency 'activesupport', '>= 4.0'
21
+ spec.add_dependency 'hipchat', '>= 1.4'
22
+ spec.add_dependency 'slack-notifier', '>= 1.1'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,9 @@
1
+ require "active_messenger/version"
2
+ require "active_messenger/base"
3
+ require "active_messenger/errors"
4
+ require "active_messenger/adapters/hipchat"
5
+
6
+ require "active_messenger/railtie" if defined? Rails
7
+
8
+ module ActiveMessenger
9
+ end
@@ -0,0 +1,23 @@
1
+ module ActiveMessenger
2
+ module Adapters
3
+ class Base
4
+ cattr_accessor :config_path
5
+ cattr_reader :config
6
+
7
+ class << self
8
+ def load_config!
9
+ self.config = ActiveMessenger::Core::ConfigParsable.parse_config!(self.config_path)
10
+ end
11
+ end
12
+
13
+ def initialize
14
+ raise NotImplementedError.new("Adapter does not implement 'initialize'")
15
+ end
16
+
17
+ def send!
18
+ raise NotImplementedError.new("Adapter does not implement 'send!'")
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,23 @@
1
+ require 'hipchat'
2
+
3
+ require 'active_support/core_ext/class/attribute'
4
+ require "active_messenger/adapters/base"
5
+
6
+ module ActiveMessenger
7
+ module Adapters
8
+ class Hipchat < ActiveMessenger::Adapters::Base
9
+ cattr_accessor :api_token, :room_id, :user_name
10
+ attr_accessor :room_id, :user_name
11
+
12
+ def initialize
13
+ @client = ::HipChat::Client.new(self.class.api_token, api_version: 'v2')
14
+ @room_id = self.class.room_id
15
+ @user_name = self.class.user_name
16
+ end
17
+
18
+ def send!(msg)
19
+ @client[@room_id].send(@user_name, msg)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ require 'slack-notifier'
2
+
3
+ require 'active_support/core_ext/class/attribute'
4
+ require "active_messenger/adapters/base"
5
+
6
+ module ActiveMessenger
7
+ module Adapters
8
+ class Slack < ActiveMessenger::Adapters::Base
9
+ cattr_accessor :webhook_url, :channel, :username
10
+
11
+ def initialize
12
+ @client = ::Slack::Notifier.new(
13
+ self.class.webhook_url,
14
+ channel: self.class.channel,
15
+ username: self.class.username
16
+ )
17
+ end
18
+
19
+ def channnel=(ch)
20
+ @client.channel = ch
21
+ end
22
+
23
+ def username=(u_name)
24
+ @client.username = u_name
25
+ end
26
+
27
+ def send!(msg)
28
+ @client.ping(msg)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,39 @@
1
+ require 'active_support'
2
+ require "active_support/core_ext/string/inflections"
3
+ require 'active_support/core_ext/class/attribute'
4
+
5
+ require "active_messenger/core/postable"
6
+ require "active_messenger/core/config_parsable"
7
+ require "active_messenger/adapters/hipchat"
8
+ require "active_messenger/adapters/slack"
9
+
10
+ module ActiveMessenger
11
+ class Base
12
+ include Core::ConfigParsable
13
+ include Core::Postable
14
+
15
+ class_attribute :adapter
16
+
17
+ def initialize
18
+ @adapter = self.class.adapter_class.new
19
+ end
20
+
21
+ class << self
22
+ def adapter(adapter_sym)
23
+ self.adapter = adapter_sym
24
+ end
25
+
26
+ def adapter_class
27
+ "ActiveMessenger::Adapters::#{self.adapter.to_s.classify}".constantize
28
+ end
29
+
30
+ def method_missing(method_name, *args, &block)
31
+ if self.adapter_class.respond_to?(method_name)
32
+ self.adapter_class.__send__(method_name, *args, &block)
33
+ else
34
+ super
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,40 @@
1
+ require 'active_support/concern'
2
+ require 'yaml'
3
+
4
+ module ActiveMessenger
5
+ module Core
6
+ module ConfigParsable
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ cattr_accessor :config, :env
11
+ end
12
+
13
+ module ClassMethods
14
+ def load_config!(config_path)
15
+ self.config ||= parse_config!(config_path)
16
+ end
17
+
18
+ private
19
+
20
+ def parse_config!(config_path)
21
+ unless self.env
22
+ raise ActiveMessenger::ConfigParseError.new("Adapter should be set 'env' to parse config!")
23
+ end
24
+
25
+ unless self.adapter
26
+ raise ActiveMessenger::ConfigParseError.new("Adapter should be set 'adapter' to parse config!")
27
+ end
28
+
29
+ parsed_config = YAML.load_file(config_path)[self.env.to_s][self.adapter.to_s]
30
+
31
+ if parsed_config.nil?
32
+ raise ActiveMessenger::ConfigParseError.new("Config missing. Please check configure format!")
33
+ end
34
+ rescue SyntaxError => e
35
+ raise ActiveMessenger::ConfigParseError.new(e.msessage)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,13 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActiveMessenger
4
+ module Core
5
+ module Postable
6
+ extend ActiveSupport::Concern
7
+
8
+ def send!(msg)
9
+ @adapter.send!(msg)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveMessenger
2
+ class ConfigParseError < StandardError ;end
3
+ end
@@ -0,0 +1,12 @@
1
+ module ActiveMessenger
2
+ module Core
3
+ class Railtie < ::Rails::Railtie
4
+ initializer 'active_messenger' do
5
+ ActiveMessenger::Base.env = Rails.env
6
+ config_path = Rails.root.join('config', 'active_messenger.yml')
7
+
8
+ ActiveMessenger::Base.load_config!(config_path) if File.exist?(config_path)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveMessenger
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveMessenger do
4
+ describe 'raw_configured_hipchat_adapter' do
5
+ let(:messenger) { RawConfiguredHipchatMessenger.new }
6
+ it_behaves_like "postable"
7
+ end
8
+
9
+ describe 'config_given_hipchat_adapter' do
10
+ let(:messenger) { ConfigGivenHipchatMessenger.new }
11
+ it_behaves_like "postable"
12
+ end
13
+
14
+ describe 'raw_configured_slack_adapter' do
15
+ let(:messenger) { RawConfiguredSlackMessenger.new }
16
+ it_behaves_like "postable"
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ development:
2
+ hipchat:
3
+ api_token: hogehoge
4
+ room_id: 1234567
5
+ user_name: hoge
6
+ production:
7
+ hipchat:
8
+ api_token: hogehoge
9
+ room_id: 1234567
10
+ user_name: hoge
11
+ test:
12
+ hipchat:
13
+ api_token: hogehoge
14
+ room_id: 1234567
15
+ user_name: hoge
@@ -0,0 +1,7 @@
1
+ class ConfigGivenHipchatMessenger < ActiveMessenger::Base
2
+ adapter :hipchat
3
+
4
+ self.env = :test
5
+
6
+ load_config! File.expand_path('../../config/active_messenger.yml', __FILE__)
7
+ end
@@ -0,0 +1,7 @@
1
+ class RawConfiguredHipchatMessenger < ActiveMessenger::Base
2
+ adapter :hipchat
3
+
4
+ self.api_token = 'hogehoge'
5
+ self.room_id = 1234567
6
+ self.user_name = 'hoge'
7
+ end
@@ -0,0 +1,7 @@
1
+ class RawConfiguredSlackMessenger < ActiveMessenger::Base
2
+ adapter :slack
3
+
4
+ self.webhook_url = 'https://hooks.slack.com/services/hogehoge'
5
+ self.channel = '#general'
6
+ self.username = 'hoge'
7
+ end
@@ -0,0 +1,7 @@
1
+ shared_examples "postable" do
2
+ describe '#send!' do
3
+ subject! { messenger.send!("hoge") }
4
+ it 'send message' do
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH.unshift File.expand_path('../dummy/hipchat', __FILE__)
3
+ $LOAD_PATH.unshift File.expand_path('../dummy/slack', __FILE__)
4
+ $LOAD_PATH.unshift File.expand_path('../shared_examples', __FILE__)
5
+ require 'active_messenger'
6
+ require 'raw_configured_hipchat_messenger'
7
+ require 'config_given_hipchat_messenger'
8
+ require 'raw_configured_slack_messenger'
9
+ require 'postable'
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activemessenger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Keisuke Izumiya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hipchat
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: slack-notifier
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '1.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '1.1'
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.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
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: rspec
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'
97
+ description: ''
98
+ email:
99
+ - ksk.i.530@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - activemessenger.gemspec
112
+ - lib/active_messenger.rb
113
+ - lib/active_messenger/adapters/base.rb
114
+ - lib/active_messenger/adapters/hipchat.rb
115
+ - lib/active_messenger/adapters/slack.rb
116
+ - lib/active_messenger/base.rb
117
+ - lib/active_messenger/core/config_parsable.rb
118
+ - lib/active_messenger/core/postable.rb
119
+ - lib/active_messenger/errors.rb
120
+ - lib/active_messenger/railtie.rb
121
+ - lib/active_messenger/version.rb
122
+ - spec/activemessenger_spec.rb
123
+ - spec/dummy/config/active_messenger.yml
124
+ - spec/dummy/hipchat/config_given_hipchat_messenger.rb
125
+ - spec/dummy/hipchat/raw_configured_hipchat_messenger.rb
126
+ - spec/dummy/slack/raw_configured_slack_messenger.rb
127
+ - spec/shared_examples/postable.rb
128
+ - spec/spec_helper.rb
129
+ homepage: ''
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 2.2.2
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: The wrapper of messaging application.
153
+ test_files:
154
+ - spec/activemessenger_spec.rb
155
+ - spec/dummy/config/active_messenger.yml
156
+ - spec/dummy/hipchat/config_given_hipchat_messenger.rb
157
+ - spec/dummy/hipchat/raw_configured_hipchat_messenger.rb
158
+ - spec/dummy/slack/raw_configured_slack_messenger.rb
159
+ - spec/shared_examples/postable.rb
160
+ - spec/spec_helper.rb