merriam 1.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: a19d0f6739752836fa732c2bfbf61d0d10c6de50
4
+ data.tar.gz: 8ebc11e0c2f123a03a74c3f0dd139feb619aa0ed
5
+ SHA512:
6
+ metadata.gz: 9ca5bc784da04ce11ec4ef668f7fd8e5d6e45d747df1e78b617c3a429041c79a854282d5b72566e9ec7381a629d4f997a4c2b67947a4aa0cabc43f1b6f6cc581
7
+ data.tar.gz: e6c8cfc3b60f8b9d7b605a4a7e421cba00c4616e1f692bcf92bf94d0b1536bb6a8b4ecac9cc22dd6d0380263984aac66834537de597eb57b77426a661487a188
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'http'
4
+ require 'merriam/core/language'
5
+ require 'merriam/core/response'
6
+ require 'merriam/core/result'
7
+ require 'merriam/core/translate_text'
8
+
9
+ module Merriam
10
+ class Client
11
+ include Merriam::Utils
12
+
13
+ BASE_URL = 'https://merriam.io/api/v1'
14
+
15
+ def languages
16
+ resp = HTTPClient.get("#{BASE_URL}/languages.json", {}, headers)
17
+ body = JSON.parse(resp.body)
18
+ resp.status_code.eql?(200) ? [body].flatten.map { |b| Language.new(b) } : body
19
+ end
20
+
21
+ def detect(t)
22
+ char_limit(t.join)
23
+ resp = HTTPClient.post("#{BASE_URL}/detect.json", t.to_json, headers)
24
+ body = JSON.parse(resp.body)
25
+ resp.status_code.eql?(200) ? [body].flatten.map { |b| Response.new(b) } : body
26
+ end
27
+
28
+ def translate_text(text, language_code)
29
+ char_limit(text)
30
+ data = {'t' => text, 'c' => language_code}
31
+ resp = HTTPClient.post("#{BASE_URL}/translate_text.json", data.to_json, headers)
32
+ body = JSON.parse(resp.body)
33
+ resp.status_code.eql?(200) ? TranslateText.new(body) : body
34
+ end
35
+
36
+ def translate_html(html, language_code)
37
+ char_limit(html)
38
+ data = {'t' => html, 'c' => language_code}
39
+ resp = HTTPClient.post("#{BASE_URL}/translate_html.json", data.to_json, headers)
40
+ body = JSON.parse(resp.body)
41
+ resp.status_code.eql?(200) ? TranslateText.new(body) : body
42
+ end
43
+
44
+ def translate_url(url, language_code)
45
+ str = open(url).read
46
+ char_limit(str)
47
+ data = {'t' => html, 'c' => language_code}
48
+ resp = HTTPClient.post("#{BASE_URL}/translate_html.json", data.to_json, headers)
49
+ body = JSON.parse(resp.body)
50
+ resp.status_code.eql?(200) ? TranslateText.new(body) : body
51
+ rescue => e
52
+ puts e.message
53
+ end
54
+
55
+ private
56
+
57
+ def headers
58
+ {'Content-Type' => 'application/json', 'key' => ENV['MERRIAM_API_KEY']}
59
+ end
60
+
61
+ def char_limit(str)
62
+ puts 'Character limit is 31250' if str.size > _31250
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Merriam
4
+ module Utils
5
+ class Language
6
+ attr_accessor :code, :name
7
+
8
+ def initialize(options)
9
+ @code = options['code']
10
+ @name = options['name']
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Merriam
4
+ module Utils
5
+ class Response
6
+ attr_accessor :text, :results
7
+
8
+ def initialize(options)
9
+ @text = options['text']
10
+ @results = options['results'].map { |r| Result.new(r) }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Merriam
4
+ module Utils
5
+ class Result
6
+ attr_accessor :confidence, :language
7
+
8
+ def initialize(options)
9
+ @confidence = options['confidence']
10
+ @language = options['language']
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Merriam
4
+ module Utils
5
+ class TranslateText
6
+ attr_accessor :text, :to, :origin, :from, :model, :detected
7
+
8
+ def initialize(options)
9
+ @text = options['text']
10
+ @to = options['to']
11
+ @origin = options['origin']
12
+ @from = options['from']
13
+ @model = options['model']
14
+ @detected = options['detected']
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Merriam
4
+ VERSION = '1.0.1'
5
+ end
data/lib/merriam.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'merriam/client'
4
+
5
+ # Merriam should be easy
6
+ module Merriam
7
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: merriam
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vajapravin
8
+ - CreoMark
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-04-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: http
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 0.6.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 0.6.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.0'
42
+ description: Detect text language and translate text language in Rails Controller,
43
+ Views, Model, Lib, etc
44
+ email: vajapravin23@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - lib/merriam.rb
50
+ - lib/merriam/client.rb
51
+ - lib/merriam/core/language.rb
52
+ - lib/merriam/core/response.rb
53
+ - lib/merriam/core/result.rb
54
+ - lib/merriam/core/translate_text.rb
55
+ - lib/merriam/version.rb
56
+ homepage: https://github.com/merriamIO/merriam-ruby
57
+ licenses:
58
+ - Apache 2.0
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '2.2'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.6.13
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Language detect and translate library for Rails app
80
+ test_files: []