sinamak 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 17011eb62c5ba9d43bbb311f599493d7c7f98b450ec14d76ac718113524ad2ad
4
+ data.tar.gz: 6913bb5ae33b085bc5531b22a6497b6ebb6317d3f5cfdc2d7646f43245788cb7
5
+ SHA512:
6
+ metadata.gz: b1418daa0e4189d3e321561aaa78e581ac7dd044a54b5bb81abdacb5e82e8c10177e7a566db27d8bb759f2752cd324a3f0ac94df7a41a593e0d67da8d84fb580
7
+ data.tar.gz: 24bf97064ce9c085cf08c80d71d225d162235d6592d65a85f484805abdb1ef83eb8bd95b8db6667fa00fd665e4c282190e6059fa7bc17ea52dc60b551e1796df
data/.fasterer.yml ADDED
@@ -0,0 +1,2 @@
1
+ exclude_paths:
2
+ - 'vendor/**/*.rb'
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/
10
+ Gemfile.lock
11
+ *.env
data/.reek.yml ADDED
@@ -0,0 +1,11 @@
1
+ ---
2
+ detectors:
3
+ IrresponsibleModule:
4
+ enabled: false
5
+ UnusedParameters:
6
+ enabled: false
7
+ UncommunicativeVariableName:
8
+ enabled: false
9
+
10
+ exclude_paths:
11
+ - vendor/
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6.2
3
+
4
+ Style/Documentation:
5
+ Enabled: false
6
+
7
+ Style/EmptyMethod:
8
+ EnforcedStyle: expanded
9
+
10
+ Metrics/LineLength:
11
+ Max: 120
12
+
13
+ Metrics/ModuleLength:
14
+ Max: 120
15
+
16
+ Metrics/ClassLength:
17
+ Max: 160
18
+
19
+ Metrics/BlockLength:
20
+ Max: 120
21
+
22
+ Metrics/MethodLength:
23
+ Max: 24
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 F
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Sinamak
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sinamak`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'sinamak'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install sinamak
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sinamak.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'sinamak'
6
+
7
+ Pry.start
data/bin/rake ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'pathname'
5
+
6
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', Pathname.new(__FILE__).realpath)
7
+
8
+ bundle_binstub = File.expand_path('bundle', __dir__)
9
+ if File.file?(bundle_binstub)
10
+ message = 'Run `bundle binstubs bundler --force` then run this command again'
11
+ abort(message) unless File.read(bundle_binstub, 300) =~ / generated /
12
+ load(bundle_binstub)
13
+ end
14
+
15
+ require 'rubygems'
16
+ require 'bundler/setup'
17
+
18
+ load Gem.bin_path('rake', 'rake')
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -euo pipefail
4
+ IFS=$'\n\t'
5
+ set -vx
6
+
7
+ bundle install
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sinamak
4
+ module Environment
5
+ require 'base64'
6
+
7
+ URI = Base64.decode64(ENV['SINAMAK_URI'].to_s)
8
+ KEY = Base64.decode64(ENV['SINAMAK_KEY'].to_s)
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ class ClientError
5
+ def display_message
6
+ message = JSON.parse(response[:body])['message']
7
+ puts "[ClientError] [Code #{response[:status]}] #{message}"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sinamak
4
+ class NoopResponse
5
+ attr_reader :body_object
6
+
7
+ def initialize
8
+ @body_object = OpenStruct.new
9
+ end
10
+
11
+ def successful?
12
+ false
13
+ end
14
+
15
+ private
16
+
17
+ def status_200?
18
+ false
19
+ end
20
+ end
21
+ end
22
+
23
+ module Faraday
24
+ class Response
25
+ def successful?
26
+ status_200? && body
27
+ end
28
+
29
+ def body_object
30
+ OpenStruct.new(JSON.parse(body))
31
+ rescue JSON::ParserError
32
+ OpenStruct.new
33
+ end
34
+
35
+ private
36
+
37
+ def status_200?
38
+ status && status == 200
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sinamak
4
+ LANGUAGES = {
5
+ US_ENGLISH: 'en',
6
+ AU_ENGLISH: 'en',
7
+ CA_ENGLISH: 'en',
8
+ BRAZILIAN_PORTUGUESE: 'pt',
9
+ CATALAN: 'ca',
10
+ CROATIAN: 'hr',
11
+ CZECH: 'cs',
12
+ DANISH: 'da',
13
+ DUTCH: 'nl',
14
+ FINNISH: 'fi',
15
+ FRENCH: 'fr',
16
+ GERMAN: 'de',
17
+ GREEK: 'el',
18
+ HINDI: 'hi',
19
+ HUNGARIAN: 'hu',
20
+ INDONESIAN: 'id',
21
+ ITALIAN: 'it',
22
+ JAPANESE: 'ja',
23
+ KOREAN: 'ko',
24
+ MALAY: 'ms',
25
+ MEXICAN_SPANISH: 'es',
26
+ NORWEGIAN: 'no',
27
+ POLISH: 'pl',
28
+ PORTUGUESE: 'pt',
29
+ ROMANIAN: 'ro',
30
+ RUSSIAN: 'ru',
31
+ SIMPLIFIED_CHINESE: 'zh',
32
+ SLOVAK: 'sk',
33
+ SPANISH: 'es',
34
+ SWEDISH: 'sv',
35
+ THAI: 'th',
36
+ TRADITIONAL_CHINESE: 'zh',
37
+ TURKISH: 'tr',
38
+ UK_ENGLISH: 'en',
39
+ UKRANIAN: 'uk',
40
+ VIETNAMESE: 'vi'
41
+ }.freeze
42
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sinamak
4
+ class Polyglot
5
+ def initialize
6
+ @translation_source = TranslationSource.new if CONNECTION
7
+ end
8
+
9
+ def available_languages
10
+ translation_source.supported_languages
11
+ end
12
+
13
+ def detect_language(text)
14
+ translation_source.detect_language(text)
15
+ end
16
+
17
+ def translate(text, from: 'en', to:)
18
+ translation_source.translate(text, from: from, to: to)
19
+ end
20
+
21
+ private
22
+
23
+ def translation_source
24
+ @translation_source || NoopTranslationSource.new
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sinamak
4
+ begin
5
+ CONNECTION = Faraday.new(url: Environment::URI) do |connection|
6
+ connection.request :url_encoded
7
+ connection.response :raise_error
8
+ connection.adapter Faraday.default_adapter
9
+ end
10
+ rescue URI::InvalidURIError
11
+ nil
12
+ end
13
+
14
+ class TranslationSource
15
+ require_relative 'faraday/client_error'
16
+ require_relative 'faraday/response'
17
+
18
+ module ResponseCodes
19
+ TRANSLATION = {
20
+ OperationSuccess: 200,
21
+ InvalidAPIKey: 401,
22
+ BlockedAPIKey: 402,
23
+ ExceedDailyLimit: 404,
24
+ ExceedMaxTextSize: 413,
25
+ CannotTranslate: 422,
26
+ Unsupported: 501
27
+ }.freeze
28
+ end
29
+
30
+ def initialize
31
+ @connection = CONNECTION
32
+ end
33
+
34
+ def translate(text, from:, to:)
35
+ request_translation(text, from, to).body_object.text
36
+ end
37
+
38
+ def detect_language(text)
39
+ request_language(text).body_object.lang
40
+ end
41
+
42
+ def supported_languages
43
+ supported_languages_response.body_object.dirs
44
+ end
45
+
46
+ private
47
+
48
+ def noop_response
49
+ @noop_response ||= NoopResponse.new
50
+ end
51
+
52
+ def request_translation(text, *lang)
53
+ @connection.post('translate') do |request|
54
+ request['Content-Type'] = 'application/x-www-form-urlencoded'
55
+ request.body = { key: Environment::KEY, text: text.to_s, lang: lang.to_a.join('-') }
56
+ end
57
+ rescue Faraday::ClientError => e
58
+ rescue_handle(e)
59
+ end
60
+
61
+ def request_language(text)
62
+ @connection.post('detect') do |request|
63
+ request['Content-Type'] = 'application/x-www-form-urlencoded'
64
+ request.body = { key: Environment::KEY, text: text.to_s }
65
+ end
66
+ rescue Faraday::ClientError => e
67
+ rescue_handle(e)
68
+ end
69
+
70
+ def request_supported_languages
71
+ @connection.post('getLangs') do |request|
72
+ request['Content-Type'] = 'application/x-www-form-urlencoded'
73
+ request.body = { key: Environment::KEY, ui: 'en' }
74
+ end
75
+ rescue Faraday::ClientError => e
76
+ rescue_handle(e)
77
+ end
78
+
79
+ def supported_languages_response
80
+ @supported_languages_response ||= request_supported_languages
81
+ end
82
+
83
+ def rescue_handle(error)
84
+ error.display_message
85
+ noop_response
86
+ end
87
+ end
88
+ private_constant :TranslationSource
89
+
90
+ class NoopTranslationSource
91
+ def detect_language(text)
92
+ end
93
+
94
+ def languages
95
+ end
96
+ end
97
+ private_constant :NoopTranslationSource
98
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sinamak
4
+ module Version
5
+ MAJOR = '0'
6
+ MINOR = '1'
7
+ PATCH = '1'
8
+ end
9
+
10
+ VERSION = [Version::MAJOR, Version::MINOR, Version::PATCH].compact.join('.')
11
+ end
data/lib/sinamak.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dotenv/load'
4
+ require 'faraday'
5
+ begin
6
+ require 'pry'
7
+ rescue StandardError
8
+ LoadError
9
+ end
10
+
11
+ module Sinamak
12
+ require 'sinamak/version'
13
+ require 'sinamak/environment'
14
+ require 'sinamak/translation_source'
15
+ require 'sinamak/polyglot'
16
+ end
data/sinamak.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'sinamak/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'sinamak'
9
+ spec.version = Sinamak::VERSION
10
+ spec.authors = ['FFD']
11
+ spec.email = ['fdelacruz.mail@gmail.com']
12
+ spec.summary = 'Simple Language Translator'
13
+ spec.description = 'Simple Language Translator'
14
+ spec.license = 'MIT'
15
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
16
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ end
18
+ spec.require_paths = ['lib']
19
+ spec.add_runtime_dependency 'dotenv', '~> 2.7.5'
20
+ spec.add_runtime_dependency 'faraday', '~> 0.17.0'
21
+ spec.add_development_dependency 'bundler', '~> 2.0'
22
+ spec.add_development_dependency 'fasterer', '~> 0.8.0'
23
+ spec.add_development_dependency 'pry', '~> 0.12.2'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'reek', '~> 5.5.0'
26
+ spec.add_development_dependency 'rubocop', '~> 0.76.0'
27
+ end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinamak
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - FFD
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dotenv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.17.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.17.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: fasterer
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.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.8.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.12.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.12.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: reek
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 5.5.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 5.5.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.76.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.76.0
125
+ description: Simple Language Translator
126
+ email:
127
+ - fdelacruz.mail@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".fasterer.yml"
133
+ - ".gitignore"
134
+ - ".reek.yml"
135
+ - ".rubocop.yml"
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - bin/console
141
+ - bin/rake
142
+ - bin/setup
143
+ - lib/sinamak.rb
144
+ - lib/sinamak/environment.rb
145
+ - lib/sinamak/faraday/client_error.rb
146
+ - lib/sinamak/faraday/response.rb
147
+ - lib/sinamak/languages.rb
148
+ - lib/sinamak/polyglot.rb
149
+ - lib/sinamak/translation_source.rb
150
+ - lib/sinamak/version.rb
151
+ - sinamak.gemspec
152
+ homepage:
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubygems_version: 3.0.3
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: Simple Language Translator
175
+ test_files: []