akane-imkayac 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: bd49543bd49ef4ab2bba3396875e3b90679356fb
4
+ data.tar.gz: f36a64cee9c99f744be4c42d557aa5131a7bded2
5
+ SHA512:
6
+ metadata.gz: aeb16720d67c435eea156ad39471e40b8fde3973fa51e679c9b34f6218a3e41bbe6f7ebfcae00499b4ccc488ec97335f786a1276dac34076c66f12fccbfa9e4c
7
+ data.tar.gz: 31283f3d3b904b431f322eb8ceba7558a558ef3a0a867d2b8735a9fa7ce71aca40bcf83337ea5162432f74974375f7ad30e0daa8f9545df2288c6e7281124357
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in akane-imkayac.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Shota Fukumori (sora_h)
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,29 @@
1
+ # Akane::Imkayac
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'akane-imkayac'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install akane-imkayac
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/akane-imkayac/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'akane-imkayac/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "akane-imkayac"
8
+ spec.version = AkaneImkayac::VERSION
9
+ spec.authors = ["Shota Fukumori (sora_h)"]
10
+ spec.email = ["her@sorah.jp"]
11
+ spec.summary = %q{imkayac storage plugin for akane | for notification}
12
+ spec.description = %q{storage plugin for akane | sends matched tweets to im.kayac}
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_dependency "akane", ">= 0.1.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "rspec", "~> 2.14.1"
25
+ spec.add_development_dependency "webmock", "~> 1.17.3"
26
+ spec.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,2 @@
1
+ require 'akane-imkayac/version'
2
+ require 'akane/storages/imkayac'
@@ -0,0 +1,3 @@
1
+ module AkaneImkayac
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "akane/imkayac/version"
2
+
3
+ module Akane
4
+ module Imkayac
5
+ # Your code goes here...
6
+ end
7
+ end
@@ -0,0 +1,82 @@
1
+ require 'net/http'
2
+ require 'erb'
3
+ require 'digest/sha1'
4
+ require 'logger'
5
+ require 'json'
6
+
7
+ require 'akane/storages/abstract_storage'
8
+ require 'akane-imkayac/version'
9
+
10
+ module Akane
11
+ module Storages
12
+ class Imkayac < AbstractStorage
13
+ class RequestError < StandardError; end
14
+
15
+ VERSION = AkaneImkayac::VERSION
16
+ DEFAULT_ENDPOINT = 'http://im.kayac.com/api/post'.freeze
17
+ DEFAULT_MESSAGE = '[tw] <%= tweet.user.screen_name %>: <%= tweet.text %>'
18
+
19
+ def initialize(config: raise(ArgumentError, 'missing config'), logger: Logger.new($stdout))
20
+ super
21
+
22
+ @config["keywords"] ||= []
23
+ @config["excludes"] ||= []
24
+ @config["message"] ||= DEFAULT_MESSAGE
25
+ @config["endpoint"] ||= DEFAULT_ENDPOINT
26
+
27
+
28
+ unless @config["user"]
29
+ raise ArgumentError, "config `user` is required"
30
+ end
31
+ unless @config["keywords"].kind_of?(Enumerable)
32
+ raise ArgumentError, "config `keywords` should be Enumerable"
33
+ end
34
+ unless @config["excludes"].kind_of?(Enumerable)
35
+ raise ArgumentError, "config `excludes` should be Enumerable"
36
+ end
37
+
38
+ @endpoint = URI.parse("#{@config["endpoint"].gsub(%r{/\z}, '')}/#{@config["user"]}")
39
+ end
40
+
41
+ def record_tweet(account, tweet)
42
+ unless tweet.text &&
43
+ !tweet.retweet? &&
44
+ @config["excludes"].all? { |_| ! tweet.text.include?(_.to_s) } &&
45
+ @config["keywords"].all? { |_| tweet.text.include?(_.to_s) }
46
+ return
47
+ end
48
+ payload = { message: ERB.new(@config["message"]).result(binding), }
49
+
50
+ payload[:password] = @config["password"] if @config["password"]
51
+
52
+ if @config["secret"]
53
+ payload[:sig] = Digest::SHA1.hexdigest("#{payload[:message]}#{@config["secret"]}")
54
+ end
55
+
56
+ if @config["handler"]
57
+ payload[:handler] = ERB.new(@config["handler"]).result(binding)
58
+ end
59
+
60
+ @logger.debug("im.kayac: sending #{payload[:message].inspect} to #{@config["user"].inspect}")
61
+ response = Net::HTTP.post_form(@endpoint, payload)
62
+ json = JSON.parse(response.body || '')
63
+ @logger.info("im.kayac: sent #{payload[:message].inspect} to #{@config["user"].inspect} -- #{json.inspect}")
64
+
65
+ raise RequestError, json["error"] if json["error"] && !json["error"].empty?
66
+
67
+ rescue JSON::ParserError => e
68
+ r.value
69
+ raise e
70
+ end
71
+
72
+ def mark_as_deleted(*)
73
+ end
74
+
75
+ def record_event(*)
76
+ end
77
+
78
+ def record_message(*)
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,118 @@
1
+ require 'spec_helper'
2
+ require 'akane/storages/imkayac'
3
+
4
+ require 'digest/sha1'
5
+ require 'json'
6
+
7
+ require 'twitter/tweet'
8
+ require 'twitter/user'
9
+
10
+ describe Akane::Storages::Imkayac do
11
+ let(:base_config) { {"user" => 'tester', "keywords" => %w(keyword), "excludes" => %w(ignore)} }
12
+ let(:config) { base_config }
13
+
14
+ let(:endpoint) { "http://im.kayac.com/api/post" }
15
+
16
+ let(:tweet_text) { 'keyword' }
17
+ let(:screen_name) { 'user' }
18
+ let(:tweet) { Twitter::Tweet.new(id: 1, text: tweet_text, user: {id: 2, screen_name: screen_name}, retweeted_status: nil) }
19
+ let(:retweet) { Twitter::Tweet.new(id: 2, text: "RT @#{screen_name}: #{tweet_text}", user: {id: 3, screen_name: 'retweeter'}, retweeted_status: tweet) }
20
+
21
+ let(:error) { nil }
22
+ let(:response) { {id: nil, error: error, result: error ? "" : "posted"} }
23
+
24
+ subject { described_class.new(config: config, logger: Logger.new(nil)) }
25
+
26
+ before do
27
+ stub_request(:post, "#{endpoint}/tester").to_return(:body => response.to_json)
28
+ end
29
+
30
+ describe "#record_tweet" do
31
+ describe "keyword matching:" do
32
+ context "when matched keywords" do
33
+ it "sends" do
34
+ subject.record_tweet('tester', tweet)
35
+
36
+ expect(a_request(:post, "#{endpoint}/tester").with(
37
+ body: {message: '[tw] user: keyword'}
38
+ )).to have_been_made
39
+ end
40
+
41
+ context "when error occured" do
42
+ let(:error) { "test error" }
43
+
44
+ it "raises error" do
45
+ expect {
46
+ subject.record_tweet('tester', tweet)
47
+ }.to raise_error(Akane::Storages::Imkayac::RequestError, "test error")
48
+ end
49
+ end
50
+ end
51
+
52
+ context "when matched both keywords and ignored keywords" do
53
+ let(:tweet_text) { 'keyword ignore' }
54
+
55
+ it "doesn't send" do
56
+ subject.record_tweet('tester', tweet)
57
+ expect(a_request(:post, "#{endpoint}/tester")).not_to have_been_made
58
+ end
59
+ end
60
+
61
+ context "when matched tweet is a retweet" do
62
+ it "doesn't send" do
63
+ subject.record_tweet('tester', retweet)
64
+ expect(a_request(:post, "#{endpoint}/tester")).not_to have_been_made
65
+ end
66
+ end
67
+ end
68
+
69
+ describe "message option:" do
70
+ let(:config) { base_config.merge("message" => '<%= tweet.id %> <%= tweet.user.id %> <%= tweet.text %> <%= tweet.user.screen_name %>') }
71
+
72
+ it "allows customize message" do
73
+ subject.record_tweet('tester', tweet)
74
+
75
+ expect(a_request(:post, "#{endpoint}/tester").with(
76
+ body: {message: '1 2 keyword user'}
77
+ )).to have_been_made
78
+ end
79
+ end
80
+
81
+ describe "handler option:" do
82
+ let(:config) { base_config.merge("handler" => 'myapp://<%= tweet.id %>') }
83
+
84
+ it "allows customize handler" do
85
+ subject.record_tweet('tester', tweet)
86
+
87
+ expect(a_request(:post, "#{endpoint}/tester").with(
88
+ body: {message: '[tw] user: keyword', handler: 'myapp://1'}
89
+ )).to have_been_made
90
+ end
91
+ end
92
+
93
+ describe "secret option:" do
94
+ let(:config) { base_config.merge("secret" => 'mysecret') }
95
+
96
+ it "sends `sig` for authentication" do
97
+ subject.record_tweet('tester', tweet)
98
+
99
+ message = '[tw] user: keyword'
100
+ expect(a_request(:post, "#{endpoint}/tester").with(
101
+ body: {message: message, sig: Digest::SHA1.hexdigest(message + 'mysecret')}
102
+ )).to have_been_made
103
+ end
104
+ end
105
+
106
+ describe "password option:" do
107
+ let(:config) { base_config.merge("password" => 'mypw') }
108
+
109
+ it "sends `password` for authentication" do
110
+ subject.record_tweet('tester', tweet)
111
+
112
+ expect(a_request(:post, "#{endpoint}/tester").with(
113
+ body: {message: '[tw] user: keyword', password: 'mypw'}
114
+ )).to have_been_made
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
18
+
19
+ require 'webmock/rspec'
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: akane-imkayac
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Shota Fukumori (sora_h)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: akane
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.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.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.14.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.14.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.17.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.17.3
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
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
+ description: storage plugin for akane | sends matched tweets to im.kayac
84
+ email:
85
+ - her@sorah.jp
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - akane-imkayac.gemspec
97
+ - lib/akane-imkayac.rb
98
+ - lib/akane-imkayac/version.rb
99
+ - lib/akane/imkayac.rb
100
+ - lib/akane/storages/imkayac.rb
101
+ - spec/imkayac_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: ''
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.2.0
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: imkayac storage plugin for akane | for notification
127
+ test_files:
128
+ - spec/imkayac_spec.rb
129
+ - spec/spec_helper.rb