detect_language 1.1.1 → 2.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: cc5fd49d8ac038b7cdba85373af25db35a0c2ef2
4
- data.tar.gz: 32fd418fc9c9d28a4fa82009e25e1f7090a2585b
2
+ SHA256:
3
+ metadata.gz: 6a4e106f6983e68e391bda11900c37879a48b4c096d4042091f55151b4f14383
4
+ data.tar.gz: cd9eceaf57be7d1bfe8dc08d55f291975726187c7d82aff60befd37d8a0f1471
5
5
  SHA512:
6
- metadata.gz: d92c0c5af430c9c476b77b37264e3f6b55743df12c579e697976a22850291dbd82d445ff4cfa21350c234f41c8af64a95a678556d7373b92dc3badba60197779
7
- data.tar.gz: 88437b1b6483416bce8e1b114dbe4674394d556f1103ae91d63eed7dc919480bdd07724327c607a8c141cf775537bbbf96614376d981a5b33ac06c4f5e496ebb
6
+ metadata.gz: ac0f022a7a4b7f9e0c8c2733db1e87a13b906f94ac05a3d8f650a8f07ed91b7ae9db7b5c67ede0a4fb32e1dbb1647331fd840f576e69d420220f6997c35998a7
7
+ data.tar.gz: 69d86e43675b5680ed4992afbb6cce417a88f96734f6af76fe111753441a29d3ca9eadf6327f496f11802601dcae76e35c6fac424962c65af5ab67cc1daa6a04
data/README.md CHANGED
@@ -1,88 +1,98 @@
1
- Detect Language API Client
1
+ Detect Language API Ruby Client
2
2
  ========
3
3
 
4
- [![Gem](https://img.shields.io/gem/v/detect_language.svg)](https://rubygems.org/gems/detect_language)
5
- [![Build Status](https://secure.travis-ci.org/detectlanguage/detectlanguage-ruby.svg)](http://travis-ci.org/detectlanguage/detectlanguage-ruby)
4
+ [![Gem Version](https://badge.fury.io/rb/detect_language.svg)](https://badge.fury.io/rb/detect_language)
5
+ [![Build Status](https://github.com/detectlanguage/detectlanguage-ruby/actions/workflows/main.yml/badge.svg)](https://github.com/detectlanguage/detectlanguage-ruby/actions)
6
6
 
7
+ Detects language of the given text. Returns detected language codes and scores.
7
8
 
8
- Detects language of given text. Returns detected language codes and scores.
9
-
10
- Before using Detect Language API client you have to setup your personal API key.
11
- You can get it by signing up at https://detectlanguage.com
12
9
 
13
10
  ## Installation
14
11
 
15
12
  Add this line to your application's Gemfile:
16
13
 
17
- gem 'detect_language'
14
+ ```
15
+ gem 'detect_language'
16
+ ```
18
17
 
19
- Or install it yourself as:
18
+ ### Upgrading
20
19
 
21
- $ gem install detect_language
20
+ When upgrading please check [changelog](CHANGELOG.md) for breaking changes.
22
21
 
23
22
  ### Configuration
24
23
 
25
- If you are using Rails, create initializer `config/initializers/detect_language.rb` and add following code there.
26
- Otherwise just integrate following code into your apps configuration.
27
-
28
- DetectLanguage.configure do |config|
29
- config.api_key = "YOUR API KEY"
24
+ Get your personal API key by signing up at https://detectlanguage.com
30
25
 
31
- # enable secure mode (SSL) if you are passing sensitive data
32
- # config.secure = true
33
- end
26
+ ```ruby
27
+ DetectLanguage.configure do |config|
28
+ config.api_key = 'YOUR API KEY'
29
+ end
30
+ ```
34
31
 
35
32
  ## Usage
36
33
 
37
- ### Language detection
34
+ ### Detect language
38
35
 
39
- DetectLanguage.detect("Buenos dias señor")
36
+ ```ruby
37
+ DetectLanguage.detect('Dolce far niente')
38
+ ```
40
39
 
41
40
  #### Result
42
41
 
43
- [ {"language"=>"es", "isReliable"=>false, "confidence"=>0.3271028037383178},
44
- {"language"=>"pt", "isReliable"=>false, "confidence"=>0.08356545961002786} ]
42
+ ```ruby
43
+ [{"language" => "it", "score" => 0.5074}]
44
+ ```
45
45
 
46
- ### Simple language detection
46
+ ### Detect single code
47
47
 
48
- If you need just a language code you can use `simple_detect`. It returns just the language code.
48
+ If you need just a language code you can use `detect_code`.
49
49
 
50
- DetectLanguage.simple_detect("Buenos dias señor")
50
+ ```ruby
51
+ DetectLanguage.detect_code('Dolce far niente')
52
+ ```
51
53
 
52
54
  #### Result
53
55
 
54
- "es"
55
-
56
+ ```ruby
57
+ "it"
58
+ ```
56
59
 
57
60
  ### Batch detection
58
61
 
59
62
  It is possible to detect language of several texts with one request.
60
63
  This method is significantly faster than doing one request per text.
61
- To use batch detection just pass array of texts to `detect` method.
64
+ To use batch detection just pass array of texts to `detect_batch` method.
62
65
 
63
- DetectLanguage.detect(["Buenos dias señor", "Labas rytas"])
66
+ ```ruby
67
+ DetectLanguage.detect_batch(['Dolce far niente', 'Labas rytas'])
68
+ ```
64
69
 
65
70
  #### Result
66
71
 
67
72
  Result is array of detections in the same order as the texts were passed.
68
73
 
69
- [ [ {"language"=>"es", "isReliable"=>false, "confidence"=>0.3271028037383178},
70
- {"language"=>"pt", "isReliable"=>false, "confidence"=>0.08356545961002786} ],
71
- [ {"language"=>"lt", "isReliable"=>false, "confidence"=>0.04918032786885246},
72
- {"language"=>"lv", "isReliable"=>false, "confidence"=>0.03350083752093803} ] ]
74
+ ```ruby
75
+ [[{"language" => "it", "score" => 0.5074}], [{"language" => "lt", "score" => 0.3063}]]
76
+ ```
73
77
 
74
- ### Getting your account status
78
+ ### Get your account status
75
79
 
76
- DetectLanguage.user_status
80
+ ```ruby
81
+ DetectLanguage.account_status
82
+ ```
77
83
 
78
84
  #### Result
79
85
 
80
- {"date"=>"2013-11-17", "requests"=>95, "bytes"=>2223, "plan"=>"FREE", "plan_expires"=>nil,
81
- "daily_requests_limit"=>5000, "daily_bytes_limit"=>1048576, "status"=>"ACTIVE"}
86
+ ```ruby
87
+ {"date"=>"2013-11-17", "requests"=>95, "bytes"=>2223, "plan"=>"FREE", "plan_expires"=>nil,
88
+ "daily_requests_limit"=>5000, "daily_bytes_limit"=>1048576, "status"=>"ACTIVE"}
89
+ ```
82
90
 
83
- ### Getting list detectable languages
91
+ ### Get list of supported languages
84
92
 
85
- DetectLanguage.languages
93
+ ```ruby
94
+ DetectLanguage.languages
95
+ ```
86
96
 
87
97
  #### Result
88
98
 
@@ -2,17 +2,20 @@
2
2
  require File.expand_path('../lib/detect_language/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
- gem.authors = ["Laurynas Butkus"]
6
- gem.email = ["laurynas.butkus@gmail.com"]
7
- gem.description = %q{Language Detection API Client}
8
- gem.summary = %q{Detects language of given text. Returns detected language codes and scores.}
9
- gem.homepage = "https://github.com/detectlanguage/detectlanguage-ruby"
5
+ gem.authors = ['Laurynas Butkus']
6
+ gem.email = ['laurynas.butkus@gmail.com']
7
+ gem.description = 'Language Detection API Client'
8
+ gem.summary = 'Detects language of given text. Returns detected language codes and scores.'
9
+ gem.homepage = 'https://github.com/detectlanguage/detectlanguage-ruby'
10
10
 
11
- gem.files = `git ls-files`.split($\)
12
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
- gem.name = "detect_language"
15
- gem.require_paths = ["lib"]
16
- gem.version = DetectLanguage::VERSION
17
- gem.license = 'MIT'
11
+ gem.files = %w(LICENSE README.md detect_language.gemspec)
12
+ gem.files += Dir.glob('lib/**/*.{rb}')
13
+
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.name = 'detect_language'
17
+ gem.require_paths = ['lib']
18
+ gem.version = DetectLanguage::VERSION
19
+ gem.license = 'MIT'
20
+ gem.required_ruby_version = '>= 2.0.0'
18
21
  end
@@ -1,44 +1,37 @@
1
- require 'cgi'
2
1
  require 'net/http'
3
2
  require 'net/https'
4
3
  require 'json'
5
4
 
6
5
  module DetectLanguage
7
6
  class Client
8
- attr_reader :configuration
7
+ attr_reader :config
9
8
 
10
- def initialize(configuration)
11
- @configuration = configuration
9
+ def initialize(config)
10
+ @config = config
12
11
  end
13
12
 
14
- def post(method, params = {})
15
- execute(method, params, :http_method => Net::HTTP::Post)
13
+ def post(path, payload = {})
14
+ execute(Net::HTTP::Post, path, body: payload.to_json)
16
15
  end
17
16
 
18
- def get(method, params = {})
19
- execute(method, params, :http_method => Net::HTTP::Get)
17
+ def get(path)
18
+ execute(Net::HTTP::Get, path)
20
19
  end
21
20
 
22
21
  private
23
22
 
24
- def execute(method, params, options)
25
- http = setup_http_connection
26
- http_method = options[:http_method]
27
- request = http_method.new(request_uri(method))
23
+ def execute(method, path, body: nil)
24
+ request = method.new(base_uri.path + path)
25
+ request.body = body
28
26
 
29
- if RUBY_VERSION == '1.8.7'
30
- set_form_data_18(request, params)
31
- else
32
- request.set_form_data(params)
33
- end
34
-
35
- request.add_field('Authorization', 'Bearer ' + configuration.api_key.to_s)
36
- request.add_field('User-Agent', configuration.user_agent)
27
+ request['Content-Type'] = 'application/json'
28
+ request['Authorization'] = 'Bearer ' + config.api_key.to_s
29
+ request['User-Agent'] = config.user_agent
37
30
 
38
- response = http.request(request)
31
+ response = connection.request(request)
39
32
 
40
33
  case response
41
- when Net::HTTPSuccess, Net::HTTPUnauthorized then
34
+ when Net::HTTPSuccess, Net::HTTPUnauthorized
42
35
  parse_response(response.body)
43
36
  else
44
37
  raise(Error, "Failure: #{response.class}")
@@ -55,44 +48,28 @@ module DetectLanguage
55
48
  end
56
49
  end
57
50
 
58
- def request_uri(method)
59
- "/#{configuration.api_version}/#{method}"
51
+ def base_uri
52
+ @base_uri ||= URI(config.base_url)
60
53
  end
61
54
 
62
- def setup_http_connection
63
- http =
64
- Net::HTTP::Proxy(configuration.proxy_host, configuration.proxy_port, configuration.proxy_user,
65
- configuration.proxy_pass).
66
- new(configuration.host, configuration.port)
67
-
68
- http.read_timeout = configuration.http_read_timeout
69
- http.open_timeout = configuration.http_open_timeout
55
+ def connection
56
+ @connection ||= setup_connection
57
+ end
70
58
 
71
- if configuration.secure?
72
- http.use_ssl = true
73
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
59
+ def setup_connection
60
+ http = if config.proxy
61
+ proxy = URI(config.proxy)
62
+ Net::HTTP.new(base_uri.hostname, base_uri.port, proxy.hostname, proxy.port, proxy.user, proxy.password)
74
63
  else
75
- http.use_ssl = false
64
+ Net::HTTP.new(base_uri.hostname, base_uri.port)
76
65
  end
77
66
 
78
- http
79
- end
80
-
81
- def set_form_data_18(request, params, sep = '&')
82
- request.body = params.map {|k,v|
83
- if v.instance_of?(Array)
84
- v.map {|e| "#{urlencode(k.to_s)}=#{urlencode(e.to_s)}"}.join(sep)
85
- else
86
- "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}"
87
- end
88
- }.join(sep)
89
-
90
- request.content_type = 'application/x-www-form-urlencoded'
91
- end
67
+ http.use_ssl = base_uri.scheme == 'https'
68
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER if http.use_ssl?
69
+ http.read_timeout = config.http_read_timeout
70
+ http.open_timeout = config.http_open_timeout
92
71
 
93
- def urlencode(str)
94
- CGI::escape(str)
72
+ http
95
73
  end
96
-
97
74
  end
98
75
  end
@@ -1,83 +1,28 @@
1
1
  module DetectLanguage
2
2
  class Configuration
3
- # The API key for your project, found on your homepage after you login into detectlanguage.com website
4
- # Defaults to 'demo', which has a limited number of requests.
3
+ # The API key for your project, found on your homepage after you log in into
4
+ # https://detectlanguage.com website
5
5
  attr_accessor :api_key
6
6
 
7
- # The API version you are using (defaults to 0.2).
8
- attr_accessor :api_version
7
+ # API base URL
8
+ attr_accessor :base_url
9
9
 
10
10
  # HTTP request user agent (defaults to 'Detect Language API ruby gem').
11
11
  attr_accessor :user_agent
12
12
 
13
- # The host to connect to (defaults to ws.detectlanguage.com).
14
- attr_accessor :host
15
-
16
- # The port on which your DetectLanguage server runs (defaults to 443 for secure
17
- # connections, 80 for insecure connections).
18
- attr_accessor :port
19
-
20
- # +true+ for https connections, +false+ for http connections.
21
- attr_accessor :secure
22
-
23
13
  # The HTTP open timeout in seconds.
24
14
  attr_accessor :http_open_timeout
25
15
 
26
16
  # The HTTP read timeout in seconds.
27
17
  attr_accessor :http_read_timeout
28
18
 
29
- # The hostname of your proxy server (if using a proxy).
30
- attr_accessor :proxy_host
31
-
32
- # The port of your proxy server (if using a proxy).
33
- attr_accessor :proxy_port
34
-
35
- # The username to use when logging into your proxy server (if using a proxy).
36
- attr_accessor :proxy_user
37
-
38
- # The password to use when logging into your proxy server (if using a proxy).
39
- attr_accessor :proxy_pass
40
-
41
- alias_method :secure?, :secure
19
+ # The HTTP proxy to use for requests. Example: 'http://my-proxy:8080'
20
+ attr_accessor :proxy
42
21
 
43
22
  def initialize
44
- @api_key = nil
45
- @api_version = "0.2"
46
- @host = "ws.detectlanguage.com"
47
- @user_agent = "Detect Language API ruby gem"
48
- end
49
-
50
- def protocol
51
- if secure?
52
- 'https'
53
- else
54
- 'http'
55
- end
23
+ @api_key = nil
24
+ @base_url = "https://ws.detectlanguage.com/v3/"
25
+ @user_agent = "detectlanguage-ruby/#{VERSION}"
56
26
  end
57
-
58
- def port
59
- @port || default_port
60
- end
61
-
62
- # Allows config options to be read like a hash
63
- #
64
- # @param [Symbol] option Key for a given attribute
65
- def [](option)
66
- send(option)
67
- end
68
-
69
- private
70
-
71
- # Determines what port should we use for sending requests.
72
- # @return [Fixnum] Returns 443 if you've set secure to true in your
73
- # configuration, and 80 otherwise.
74
- def default_port
75
- if secure?
76
- 443
77
- else
78
- 80
79
- end
80
- end
81
-
82
27
  end
83
- end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module DetectLanguage
2
- VERSION = '1.1.1'
2
+ VERSION = '2.0.0'
3
3
  end
@@ -5,44 +5,81 @@ require "detect_language/client"
5
5
 
6
6
  module DetectLanguage
7
7
  class << self
8
- attr_writer :configuration
8
+ attr_writer :config
9
9
 
10
10
  def configure
11
- yield(configuration)
11
+ yield(config)
12
12
  end
13
13
 
14
- # The configuration object.
15
- # @see DetectLanguage.configure
16
- def configuration
17
- @configuration ||= Configuration.new
14
+ def config
15
+ @config ||= Configuration.new
18
16
  end
19
17
 
20
18
  def client
21
- Thread.current[:detect_language_client] ||= Client.new(configuration)
19
+ Thread.current[:detect_language_client] ||= Client.new(config)
22
20
  end
23
21
 
24
- def detect(data)
25
- key = data.is_a?(Array) ? 'q[]' : 'q'
26
- result = client.post(:detect, key => data)
27
- result['data']['detections']
22
+ def client=(client)
23
+ Thread.current[:detect_language_client] = client
28
24
  end
29
25
 
30
- def simple_detect(text)
26
+ # @param query String
27
+ # @return [Array<Hash>] Array of language detections
28
+ def detect(query)
29
+ if query.is_a?(Array)
30
+ warn '[DEPRECATED] `DetectLanguage.detect` with an array of queries is deprecated. Use `DetectLanguage.detect_batch` instead.'
31
+ return detect_batch(query)
32
+ end
33
+
34
+ client.post('detect', q: query)
35
+ end
36
+
37
+ # @param queries Array<String> Array of queries to detect languages for
38
+ # @return [Array<Array<Hash>>] Array of language detections for each query
39
+ def detect_batch(queries)
40
+ raise(ArgumentError, 'Expected an Array of queries') unless queries.is_a?(Array)
41
+
42
+ client.post('detect-batch', q: queries)
43
+ end
44
+
45
+ # @param text String
46
+ # @return [String, nil] Detected language code or nil if no detection found
47
+ def detect_code(text)
31
48
  detections = detect(text)
32
49
 
33
- if detections.empty?
34
- nil
35
- else
36
- detections[0]['language']
37
- end
50
+ return if detections.empty?
51
+
52
+ detections[0]['language']
38
53
  end
39
54
 
40
- def user_status
41
- client.get('user/status')
55
+ # @return [Hash] Account status information
56
+ def account_status
57
+ client.get('account/status')
42
58
  end
43
59
 
60
+ # @return [Array<Hash>] List of supported languages
44
61
  def languages
45
62
  client.get('languages')
46
63
  end
64
+
65
+ ### DEPRECATED METHODS
66
+
67
+ # @deprecated Use `DetectLanguage.config` instead
68
+ def configuration
69
+ warn '[DEPRECATED] `DetectLanguage.configuration` is deprecated. Use `DetectLanguage.config` instead.'
70
+ config
71
+ end
72
+
73
+ # @deprecated Use `detect_code` instead
74
+ def simple_detect(text)
75
+ warn '[DEPRECATED] `DetectLanguage.simple_detect` is deprecated. Use `DetectLanguage.detect_code` instead.'
76
+ detect_code(text)
77
+ end
78
+
79
+ # @deprecated Use `DetectLanguage.account_status` instead
80
+ def user_status
81
+ warn '[DEPRECATED] `DetectLanguage.user_status` is deprecated. Use `DetectLanguage.account_status` instead.'
82
+ account_status
83
+ end
47
84
  end
48
85
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: detect_language
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Laurynas Butkus
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2018-06-24 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Language Detection API Client
14
13
  email:
@@ -17,26 +16,18 @@ executables: []
17
16
  extensions: []
18
17
  extra_rdoc_files: []
19
18
  files:
20
- - ".gitignore"
21
- - ".rspec"
22
- - ".travis.yml"
23
- - Gemfile
24
19
  - LICENSE
25
20
  - README.md
26
- - Rakefile
27
21
  - detect_language.gemspec
28
22
  - lib/detect_language.rb
29
23
  - lib/detect_language/client.rb
30
24
  - lib/detect_language/configuration.rb
31
25
  - lib/detect_language/errors.rb
32
26
  - lib/detect_language/version.rb
33
- - spec/detect_language_spec.rb
34
- - spec/spec_helper.rb
35
27
  homepage: https://github.com/detectlanguage/detectlanguage-ruby
36
28
  licenses:
37
29
  - MIT
38
30
  metadata: {}
39
- post_install_message:
40
31
  rdoc_options: []
41
32
  require_paths:
42
33
  - lib
@@ -44,18 +35,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
44
35
  requirements:
45
36
  - - ">="
46
37
  - !ruby/object:Gem::Version
47
- version: '0'
38
+ version: 2.0.0
48
39
  required_rubygems_version: !ruby/object:Gem::Requirement
49
40
  requirements:
50
41
  - - ">="
51
42
  - !ruby/object:Gem::Version
52
43
  version: '0'
53
44
  requirements: []
54
- rubyforge_project:
55
- rubygems_version: 2.6.11
56
- signing_key:
45
+ rubygems_version: 3.6.9
57
46
  specification_version: 4
58
47
  summary: Detects language of given text. Returns detected language codes and scores.
59
- test_files:
60
- - spec/detect_language_spec.rb
61
- - spec/spec_helper.rb
48
+ test_files: []
data/.gitignore DELETED
@@ -1,18 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- .idea
7
- Gemfile.lock
8
- InstalledFiles
9
- _yardoc
10
- coverage
11
- doc/
12
- lib/bundler/man
13
- pkg
14
- rdoc
15
- spec/reports
16
- test/tmp
17
- test/version_tmp
18
- tmp
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --color
2
- --format progress
data/.travis.yml DELETED
@@ -1,15 +0,0 @@
1
- rvm:
2
- - 1.9.3
3
- - 2.0.0
4
- - 2.1.1
5
- - 2.3.4
6
- - 2.4.1
7
- - 2.5.0
8
- - ruby-head
9
- - ruby-head-clang
10
- - jruby-1.7.26
11
- - jruby-9.1.9.0
12
- script: bundle exec rake spec
13
- env:
14
- global:
15
- secure: Xt9aOp3jO9r7e+ljIRJSIZwifiYXzirp0wZ3GbsQ8zOd5vmIjIehReu+7e4fRm1/Wfp8q85Yl5otYPxa2HOeDl67aA9WIiCvSH8cSb4QrM6jahAc9T2td5io9GSiWPWaQiA3Th8dOOuPmBaiLWgBZ9k9MzMMJ5lj1F1whW6gTEE=
data/Gemfile DELETED
@@ -1,10 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
4
-
5
- gem 'rake'
6
- gem 'json'
7
-
8
- group :test do
9
- gem "rspec"
10
- end
data/Rakefile DELETED
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
3
- require 'rspec/core/rake_task'
4
-
5
- RSpec::Core::RakeTask.new
6
-
7
- task :default => :spec
@@ -1,98 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe DetectLanguage do
6
-
7
- let(:api_key) { ENV['DETECTLANGUAGE_API_KEY'] }
8
-
9
- before do
10
- DetectLanguage.configuration.api_key = api_key
11
- end
12
-
13
- context "configuration" do
14
- it "should have default configuration values" do
15
- subject.configuration.api_version.should == '0.2'
16
- subject.configuration.host.should == 'ws.detectlanguage.com'
17
- subject.configuration.user_agent.should == 'Detect Language API ruby gem'
18
- end
19
-
20
- it "should allow configuring" do
21
- subject.configure do |config|
22
- config.user_agent = "Detect Language testing"
23
- end
24
-
25
- subject.configuration.user_agent.should == "Detect Language testing"
26
- end
27
- end
28
-
29
- context 'invalid api key' do
30
- let(:api_key) {'invalid'}
31
-
32
- it "should raise exception for invalid key" do
33
- lambda {
34
- subject.detect("Hello world")
35
- }.should raise_error(DetectLanguage::Error)
36
- end
37
- end
38
-
39
- context "detection" do
40
- it "should detect languages" do
41
- result = subject.detect("Hello world")
42
- result[0]['language'].should == "en"
43
-
44
- result = subject.detect("Jau saulelė vėl atkopdama budino svietą")
45
- result[0]['language'].should == "lt"
46
- end
47
-
48
- it "should have simple way to detect a language" do
49
- subject.simple_detect("Hello world").should == "en"
50
- end
51
-
52
- it "should allow sending batch requests" do
53
- result = subject.detect(["Hello world", "Jau saulelė vėl atkopdama budino svietą"])
54
-
55
- result[0][0]['language'].should == "en"
56
- result[1][0]['language'].should == "lt"
57
- end
58
-
59
- it "with empty text in the batch it detects correctly" do
60
- result = subject.detect(["", "Hello", "Jau saulelė vėl atkopdama budino svietą"])
61
-
62
- result[0].should be_empty
63
- result[1][0]['language'].should == "en"
64
- result[2][0]['language'].should == "lt"
65
- end
66
-
67
- context 'secure mode' do
68
- before { DetectLanguage.configuration.secure = true }
69
- after { DetectLanguage.configuration.secure = false }
70
-
71
- it "detects language" do
72
- result = subject.detect("Hello world")
73
- result[0]['language'].should == "en"
74
- end
75
- end
76
- end
77
-
78
- describe '.user_status' do
79
- subject(:user_status) {DetectLanguage.user_status}
80
-
81
- it 'fetches user status' do
82
- expect(user_status['date']).to be_kind_of(String)
83
- expect(user_status['requests']).to be_kind_of(Integer)
84
- expect(user_status['bytes']).to be_kind_of(Integer)
85
- expect(user_status['plan']).to be_kind_of(String)
86
- expect(user_status['daily_requests_limit']).to be_kind_of(Integer)
87
- expect(user_status['daily_bytes_limit']).to be_kind_of(Integer)
88
- end
89
- end
90
-
91
- describe '.languages' do
92
- subject(:languages) {DetectLanguage.languages}
93
-
94
- it 'fetches list of detectable languages' do
95
- expect(languages).to include({ 'code' => 'en', 'name' => 'ENGLISH' })
96
- end
97
- end
98
- end
data/spec/spec_helper.rb DELETED
@@ -1,7 +0,0 @@
1
- require 'rubygems'
2
- require 'bundler/setup'
3
- require 'detect_language'
4
-
5
- RSpec.configure do |config|
6
-
7
- end