tg 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (10) hide show
  1. data/Gemfile +5 -0
  2. data/LICENSE +22 -0
  3. data/README.md +26 -0
  4. data/Rakefile +2 -0
  5. data/bin/tg +29 -0
  6. data/lib/tg.rb +13 -0
  7. data/lib/tg/tg.rb +47 -0
  8. data/lib/tg/version.rb +3 -0
  9. data/tg.gemspec +26 -0
  10. metadata +103 -0
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "json"
4
+ gem "rainbow"
5
+ gem "rb-readline"
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Irakli Janiashvili
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,26 @@
1
+ # tg
2
+
3
+ eng → geo and geo → eng translator using translate.ge api
4
+
5
+ simple CLI tool
6
+
7
+ ## Installation
8
+
9
+ $ gem install tg
10
+
11
+ ## Usage
12
+
13
+ $ tg dog
14
+ $ tg ძაღლი
15
+
16
+ ## Interactive Mode
17
+
18
+ $ tg
19
+
20
+ ## Contributing
21
+
22
+ 1. Fork it
23
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
24
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
25
+ 4. Push to the branch (`git push origin my-new-feature`)
26
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/bin/tg ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+
4
+ require 'tg'
5
+
6
+ tg = Tg::Tg.new
7
+
8
+ begin
9
+ if ARGV[0].nil?
10
+ loop do
11
+ text = Readline::readline('> '.color :green)
12
+
13
+ raise Interrupt if text == '\q'
14
+ Readline::HISTORY.push text
15
+
16
+ tg.translate text
17
+ end
18
+ else
19
+ text = ARGV[0]
20
+
21
+ tg.translate text
22
+ end
23
+ rescue Interrupt
24
+ puts 'exiting...'
25
+ rescue Exception => e
26
+ print 'Error:'.color(:white).background(:red)
27
+ print ' '
28
+ puts e
29
+ end
@@ -0,0 +1,13 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require "tg/version"
4
+ require "tg/tg"
5
+
6
+ require "json"
7
+ require "rainbow"
8
+ require "readline"
9
+ require "net/http"
10
+
11
+ module Tg
12
+
13
+ end
@@ -0,0 +1,47 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Tg
4
+ class Tg
5
+ def api_server
6
+ "beta.translate.ge"
7
+ end
8
+
9
+ def api_uri text, language
10
+ "/Main/Translate?text=#{URI.escape(text)}&lang=#{language}"
11
+ end
12
+
13
+ def translate text
14
+ highlight = %w{noun verb adverb adjective}
15
+
16
+ lang = text.ascii_only? ? :en : :ge;
17
+
18
+ json_response = Net::HTTP.get_response(api_server, api_uri(text, lang)).body
19
+
20
+ # weird unicode pair \xC2\xAD (some sort of hyphen) fix
21
+ json_response.force_encoding 'utf-8'
22
+ json_response.gsub!(/\xC2\xAD/i, '')
23
+
24
+ parsed = JSON.parse json_response, :symbolize_names => true
25
+
26
+ parsed.each do |data|
27
+ word, text = data[:Word], data[:Text]
28
+ text = text.gsub(/\s+/, ' ').strip
29
+
30
+ text.gsub!(/(#{highlight.join('|')})/) { $1.color :blue }
31
+ text.gsub!(/({\p{Word}+})/) { $1.color :magenta }
32
+ text.gsub!(/(\[.+\])/) { $1.color :yellow }
33
+
34
+ puts word.capitalize.color :green
35
+ text = text.split(/\d\) ?/).drop_while {|t| t == '' }
36
+ text.map! {|t| t.split(/; ?/) }
37
+
38
+ text.each_with_index do |g, i|
39
+ print "#{i + 1}) "
40
+ g.each {|t| puts t }
41
+ end
42
+
43
+ puts
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Tg
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path("../lib/tg/version", __FILE__)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.authors = ["Irakli Janiashvili"]
7
+ s.email = ["irakli.janiashvili@gmail.com"]
8
+ s.description = %q{eng → geo and geo → eng translator using translate.ge api}
9
+ s.summary = %q{simple CLI tool}
10
+ s.homepage = "http://github.com/irakli-janiashvili/tg"
11
+
12
+ s.bindir = "bin"
13
+ s.executables = "tg"
14
+
15
+ s.add_dependency "json"
16
+ s.add_dependency "rainbow"
17
+ s.add_dependency "rb-readline"
18
+
19
+ s.files = %w{LICENSE README.md Rakefile Gemfile tg.gemspec}
20
+ s.files += Dir.glob("lib/**/*.rb")
21
+ s.files += Dir.glob("bin/**/*")
22
+
23
+ s.name = "tg"
24
+ s.require_paths = ["lib"]
25
+ s.version = Tg::VERSION
26
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Irakli Janiashvili
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rainbow
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rb-readline
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: eng → geo and geo → eng translator using translate.ge api
63
+ email:
64
+ - irakli.janiashvili@gmail.com
65
+ executables:
66
+ - tg
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - LICENSE
71
+ - README.md
72
+ - Rakefile
73
+ - Gemfile
74
+ - tg.gemspec
75
+ - bin/tg
76
+ - lib/tg.rb
77
+ - lib/tg/tg.rb
78
+ - lib/tg/version.rb
79
+ homepage: http://github.com/irakli-janiashvili/tg
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.24
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: simple CLI tool
103
+ test_files: []