cinch-translate 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cinch-translate.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (C) 2011 by Arthur Chiu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
20
+
21
+
@@ -0,0 +1,76 @@
1
+ Cinch-Translate
2
+ ===========
3
+
4
+ The Cinch Translate Plugin. Translate your messages via the Google
5
+ Translate API!
6
+
7
+ Installation
8
+ ---------------------
9
+
10
+ if you haven't already...
11
+
12
+ $ gem install cinch
13
+
14
+ then install this gem.
15
+
16
+ $ gem install cinch-translate
17
+
18
+ Installation and Setup
19
+ ----------
20
+
21
+ #### Configuration ####
22
+
23
+ * :api_key - Your Google Translate API Key
24
+ * :to - The language you want to translate to(using codemap key, this is for auto translate). default is 'en'
25
+ * :from - The language you want to tranlsate from(using codemap key, or can be set to auto to auto-discover). default is 'auto'
26
+ * :auto - turn auto translate on or off. default is off
27
+
28
+ #### Commands ####
29
+
30
+ * !translate [codemap] [message] - translate the given message to the language codemap
31
+ * !translate-from [from] [to] [message] - translate from the given codemape to the to codemap
32
+ * !languages? - List all the available languages
33
+ * !codemap [language] - return the codemap for the given language
34
+ * !auto_translate - toggle auto translate on or off
35
+ * !auto_translate to [codemap] - set the to language for auto translate
36
+ * !auto_translate from [codemap] - set the from language for auto translate
37
+
38
+ ## Integration with Cinch ##
39
+
40
+ It's simple. follow the guide on cinch or do something like:
41
+
42
+ # mybot.rb
43
+ require 'cinch'
44
+ require 'cinch/plugins/translate'
45
+
46
+ bot = Cinch::Bot.new do
47
+ configure do |c|
48
+ c.server = "irc.freenode.net"
49
+ c.nick = "cinch"
50
+ c.channels = ["#padrino"]
51
+ c.plugins.plugins = [Cinch::Plugins::Memo::Base]
52
+ c.plugins.options[Cinch::Plugins::Translate][:api_key] = 'your_api_key"
53
+ c.plugins.options[Cinch::Plugins::Translate][:to] = 'fr'
54
+ c.plugins.options[Cinch::Plugins::Translate][:from] = 'en'
55
+ c.plugins.options[Cinch::Plugins::Translate][:auto] = true
56
+ end
57
+
58
+ end
59
+
60
+ bot.start
61
+
62
+ Finally, run your bot.
63
+
64
+ ruby -rubygems mybot.rb
65
+
66
+ And there you go!
67
+
68
+
69
+ TODO
70
+ -----
71
+
72
+ * Maybe add bot as middleman in conversation
73
+ * Come up with more cool ideas.
74
+
75
+
76
+
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/*_test.rb'
8
+ test.verbose = true
9
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cinch/plugins/translate/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cinch-translate"
7
+ s.version = Cinch::Translate::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Arthur Chiu"]
10
+ s.email = ["mr.arthur.chiu@gmail.com"]
11
+ s.homepage = "http://arthurchiu.tumblr.com"
12
+ s.summary = %q{Cinch Bot translation plugin}
13
+ s.description = %q{Cinch Bot translation plugin powered by Google Translate}
14
+
15
+ s.rubyforge_project = "cinch-translate"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ s.add_dependency 'cinch', '~>1.1.1'
22
+ s.add_dependency 'to_lang'
23
+ s.add_development_dependency 'riot', '~>0.12.0'
24
+ s.add_development_dependency 'mocha', '~>0.9.10'
25
+ end
@@ -0,0 +1,73 @@
1
+ require 'cinch'
2
+ require 'to_lang'
3
+
4
+ module Cinch
5
+ module Plugins
6
+ class Translate
7
+ include Cinch::Plugin
8
+
9
+ match %r{translate (.+?) (.+)}, :method => :translate
10
+ match %r{translate\-from (.+?) (.+?) (.+)}, :method => :translate_from
11
+ match "languages?", :method => :languages
12
+ match %r{codemap (.+)}, :method => :codemap
13
+ match "auto_translate", :method => :auto_translate
14
+ match %r{auto\_translate to (.+)}, :method => :auto_to
15
+ match %r{auto\_translate from (.+)}, :method => :auto_from
16
+
17
+ listen_to :channel
18
+
19
+ def initialize(*args)
20
+ super
21
+ @auto = config[:auto] || false
22
+ @from = config[:from] || nil
23
+ @to = config[:to] || 'en'
24
+ ::ToLang.start config[:api_key]
25
+ end
26
+
27
+ def listen(m)
28
+ return if m.message =~ /^!/ # ignore any commands
29
+ if @auto
30
+ if @from
31
+ translation = m.message.translate(@to, :from => @from) rescue "That's not a valid language."
32
+ else
33
+ translation = m.message.translate(@to) rescue "That's not a valid language."
34
+ end
35
+ m.reply "#{m.user.nick}: #{translation}"
36
+ end
37
+ end
38
+
39
+ def auto_translate(m)
40
+ msg = (@auto = !@auto ? "on" : "off")
41
+ m.reply "Auto Translate is #{msg}"
42
+ end
43
+
44
+ def auto_to(m, lang)
45
+ m.reply "To Language set to #{@to = lang}"
46
+ end
47
+
48
+ def auto_from(m, lang)
49
+ @from = (lang == 'auto' ? nil : lang)
50
+ m.reply "From Language set to #{lang}"
51
+ end
52
+
53
+ def translate(m, lang, message)
54
+ translation = message.translate(lang) rescue "That's not a valid language."
55
+ m.reply translation
56
+ end
57
+
58
+ def translate_from(m, from, to, message)
59
+ translation = message.translate(to, :from => from) rescue "That's not a valid language."
60
+ m.reply translation
61
+ end
62
+
63
+ def languages(m)
64
+ m.reply ::ToLang::CODEMAP.keys.join(',')
65
+ end
66
+
67
+ def codemap(m, lang)
68
+ code = ::ToLang::CODEMAP[lang] || 'No code exists for that language.'
69
+ m.reply code
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ module Cinch
2
+ module Translate
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'riot'
3
+ require 'mocha'
4
+ require File.expand_path('../../lib/cinch/plugins/translate',__FILE__)
5
+
6
+ class Riot::Situation
7
+ include Mocha::API
8
+ end
9
+
10
+ class Riot::Context
11
+ # Turn off bot register hook
12
+ def dont_register!
13
+ setup { Cinch::Plugins::Translate.stubs(:__register_with_bot).with(any_parameters).returns(true) }
14
+ end
15
+ end
16
+
17
+ class Object
18
+ def capture(stream)
19
+ begin
20
+ stream = stream.to_s
21
+ eval "$#{stream} = StringIO.new"
22
+ yield
23
+ result = eval("$#{stream}").string
24
+ ensure
25
+ eval("$#{stream} = #{stream.upcase}")
26
+ end
27
+ result
28
+ end
29
+ end
30
+
@@ -0,0 +1,102 @@
1
+ require File.expand_path('../teststrap',__FILE__)
2
+
3
+ context "Translate" do
4
+ dont_register!
5
+ setup { @bot = mock() }
6
+
7
+ helper :base do
8
+ Cinch::Plugins::Translate.any_instance.stubs(:config).returns({:api_key => '12345'})
9
+ ToLang.expects(:start).with('12345').returns(true)
10
+ Cinch::Plugins::Translate
11
+ end
12
+
13
+ context "#initialize" do
14
+ setup { base.new(@bot) }
15
+ asserts_topic.assigns :auto
16
+ asserts_topic.assigns :to
17
+ end
18
+
19
+ context "#translate" do
20
+ context "with language" do
21
+ asserts 'that it translates' do
22
+ @text.expects(:translate).with('en').returns('hello')
23
+ @m.expects(:reply).with('hello').returns(true)
24
+ base.new(@bot).translate(@m,'en',@text)
25
+ end
26
+ end
27
+
28
+ context "with invalid language" do
29
+ asserts 'that it returns a message' do
30
+ @text.expects(:translate).with("baka").raises(RuntimeError)
31
+ @m.expects(:reply).with("That's not a valid language.").returns(true)
32
+ base.new(@bot).translate(@m, 'baka', @text)
33
+ end
34
+ end
35
+ end
36
+
37
+ context "#translate_from" do
38
+ context "with language" do
39
+ asserts 'that its translates' do
40
+ @text.expects(:translate).with('fr',:from=>'en').returns('hello')
41
+ @m.expects(:reply).with('hello').returns(true)
42
+ base.new(@bot).translate_from @m, 'en', 'fr', @text
43
+ end
44
+ end
45
+
46
+ context "with invalid langauge" do
47
+ asserts 'that it returns a message' do
48
+ @text.expects(:translate).with('baka').raises(RuntimeError)
49
+ @m.expects(:reply).with("That's not a valid language.").returns(true)
50
+ end
51
+ end
52
+ end
53
+
54
+ context "#languages" do
55
+
56
+ context "with no options" do
57
+ asserts "that it shows list" do
58
+ @m.expects(:reply).with(regexp_matches(/english/)).returns(true)
59
+ base.new(@bot).languages @m
60
+ end
61
+ end
62
+ end
63
+
64
+ context "#codemap" do
65
+
66
+ context "with language" do
67
+ asserts "that it shows code" do
68
+ @m.expects(:reply).with('en').returns(true)
69
+ base.new(@bot).codemap @m, 'english'
70
+ end
71
+ end
72
+
73
+ context "with no language" do
74
+ asserts "that it shows message" do
75
+ @m.expects(:reply).with('No code exists for that language.').returns(true)
76
+ base.new(@bot).codemap @m, 'bakaaa'
77
+ end
78
+ end
79
+ end
80
+
81
+ context "#auto_translate" do
82
+
83
+ asserts "that it gets switched" do
84
+ @m.expects(:reply).with("Auto Translate is on").returns(true)
85
+ base.new(@bot).auto_translate @m
86
+ end
87
+ end
88
+
89
+ context "#auto_to" do
90
+ asserts 'that it assigns to language' do
91
+ @m.expects(:reply).with("To Language set to en").returns(true)
92
+ base.new(@bot).auto_to @m, 'en'
93
+ end
94
+ end
95
+
96
+ context "#auto_from" do
97
+ asserts 'that it assigns from language' do
98
+ @m.expects(:reply).with('From Language set to auto').returns(true)
99
+ base.new(@bot).auto_from @m, 'auto'
100
+ end
101
+ end
102
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cinch-translate
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Arthur Chiu
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-30 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: cinch
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 1
31
+ - 1
32
+ version: 1.1.1
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: to_lang
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: riot
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ - 12
59
+ - 0
60
+ version: 0.12.0
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: mocha
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ - 9
74
+ - 10
75
+ version: 0.9.10
76
+ type: :development
77
+ version_requirements: *id004
78
+ description: Cinch Bot translation plugin powered by Google Translate
79
+ email:
80
+ - mr.arthur.chiu@gmail.com
81
+ executables: []
82
+
83
+ extensions: []
84
+
85
+ extra_rdoc_files: []
86
+
87
+ files:
88
+ - .gitignore
89
+ - Gemfile
90
+ - LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - cinch-translate.gemspec
94
+ - lib/cinch/plugins/translate.rb
95
+ - lib/cinch/plugins/translate/version.rb
96
+ - test/teststrap.rb
97
+ - test/translate_test.rb
98
+ has_rdoc: true
99
+ homepage: http://arthurchiu.tumblr.com
100
+ licenses: []
101
+
102
+ post_install_message:
103
+ rdoc_options: []
104
+
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ requirements: []
124
+
125
+ rubyforge_project: cinch-translate
126
+ rubygems_version: 1.3.7
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: Cinch Bot translation plugin
130
+ test_files:
131
+ - test/teststrap.rb
132
+ - test/translate_test.rb