detect_language 1.0.3 → 1.0.4

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: 3eec3e59102acef4077e83636c3b1ce52866800f
4
+ data.tar.gz: 7e5402f124611bc840d2ac82e9db819e86e12a76
5
+ SHA512:
6
+ metadata.gz: 82bba14b46f34f1afa1f32414d156cf49c2a1cc2a7329cbdd5812b566e5e344326d09d410f40abc70be2acb9f3a3886eaae20a223adcf08e24b6d55449fab04e
7
+ data.tar.gz: 2435578b1aded7ad01896501e0de0b161de96c6028f306aea4332d7efa458935686d01381ac5c9d636e02a84a0a0b223cec856cdd35e6f23cc799c34d72eebd4
data/.travis.yml CHANGED
@@ -2,6 +2,7 @@ rvm:
2
2
  - 1.8.7
3
3
  - 1.9.2
4
4
  - 1.9.3
5
+ - 2.0.0
5
6
  - jruby-18mode # JRuby in 1.8 mode
6
7
  - jruby-19mode # JRuby in 1.9 mode
7
8
  - rbx-18mode
data/README.md CHANGED
@@ -3,7 +3,7 @@ Detect Language API Client [![Build Status](https://secure.travis-ci.org/detectl
3
3
 
4
4
  Detects language of given text. Returns detected language codes and scores.
5
5
 
6
- Before using Detect Language API client you setup your personal API key.
6
+ Before using Detect Language API client you have to setup your personal API key.
7
7
  You can get it by signing up at http://detectlanguage.com
8
8
 
9
9
  ## Installation
@@ -64,6 +64,23 @@ Result is array of detections in the same order as the texts were passed.
64
64
  [ {"language"=>"lt", "isReliable"=>false, "confidence"=>0.04918032786885246},
65
65
  {"language"=>"lv", "isReliable"=>false, "confidence"=>0.03350083752093803} ] ]
66
66
 
67
+ ### Getting your account status
68
+
69
+ DetectLanguage.user_status
70
+
71
+ #### Result
72
+
73
+ {"date"=>"2013-11-17", "requests"=>95, "bytes"=>2223, "plan"=>"FREE", "plan_expires"=>nil,
74
+ "daily_requests_limit"=>5000, "daily_bytes_limit"=>1048576, "status"=>"ACTIVE"}
75
+
76
+ ### Getting list detectable languages
77
+
78
+ DetectLanguage.languages
79
+
80
+ #### Result
81
+
82
+ Array of language codes and names.
83
+
67
84
  ## License
68
85
 
69
86
  Detect Language API Client is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
@@ -11,12 +11,21 @@ module DetectLanguage
11
11
  @configuration = configuration
12
12
  end
13
13
 
14
- def execute(method, params)
15
- http = setup_http_connection
14
+ def post(method, params = {})
15
+ execute(method, params, :http_method => Net::HTTP::Post)
16
+ end
17
+
18
+ def get(method, params = {})
19
+ execute(method, params, :http_method => Net::HTTP::Get)
20
+ end
16
21
 
17
- request_params = params.merge(:key => configuration.api_key)
22
+ private
18
23
 
19
- request = Net::HTTP::Post.new(request_uri(method))
24
+ def execute(method, params, options)
25
+ http = setup_http_connection
26
+ http_method = options[:http_method]
27
+ request_params = params.merge(:key => configuration.api_key)
28
+ request = http_method.new(request_uri(method))
20
29
 
21
30
  if RUBY_VERSION == '1.8.7'
22
31
  set_form_data_18(request, request_params)
@@ -36,12 +45,10 @@ module DetectLanguage
36
45
  end
37
46
  end
38
47
 
39
- private
40
-
41
48
  def parse_response(response_body)
42
49
  response = JSON.parse(response_body)
43
50
 
44
- if response["error"].nil?
51
+ if response.is_a?(Array) || response["error"].nil?
45
52
  response
46
53
  else
47
54
  raise Exception.new(response["error"]["message"])
@@ -88,4 +95,4 @@ module DetectLanguage
88
95
  end
89
96
 
90
97
  end
91
- end
98
+ end
@@ -1,3 +1,3 @@
1
1
  module DetectLanguage
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
@@ -23,7 +23,7 @@ module DetectLanguage
23
23
 
24
24
  def detect(data)
25
25
  key = data.is_a?(Array) ? 'q[]' : 'q'
26
- result = client.execute(:detect, key => data)
26
+ result = client.post(:detect, key => data)
27
27
  result['data']['detections']
28
28
  end
29
29
 
@@ -36,5 +36,13 @@ module DetectLanguage
36
36
  detections[0]['language']
37
37
  end
38
38
  end
39
+
40
+ def user_status
41
+ client.post('user/status')
42
+ end
43
+
44
+ def languages
45
+ client.get('languages')
46
+ end
39
47
  end
40
48
  end
@@ -4,9 +4,14 @@ require 'spec_helper'
4
4
 
5
5
  describe DetectLanguage do
6
6
 
7
+ let(:api_key) { '93dfb956a294140a4370a09584af2ef6' }
8
+
9
+ before do
10
+ DetectLanguage.configuration.api_key = api_key
11
+ end
12
+
7
13
  context "configuration" do
8
14
  it "should have default configuration values" do
9
- subject.configuration.api_key.should be_nil
10
15
  subject.configuration.api_version.should == '0.2'
11
16
  subject.configuration.host.should == 'ws.detectlanguage.com'
12
17
  subject.configuration.user_agent.should == 'Detect Language API ruby gem'
@@ -21,12 +26,17 @@ describe DetectLanguage do
21
26
  end
22
27
  end
23
28
 
24
- context "detection" do
25
- before do
26
- # testing key
27
- subject.configuration.api_key = "24c3185fef623b537a4df60df0a8d4d9"
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::Exception)
28
36
  end
37
+ end
29
38
 
39
+ context "detection" do
30
40
  it "should detect languages" do
31
41
  result = subject.detect("Hello world")
32
42
  result[0]['language'].should == "en"
@@ -47,16 +57,24 @@ describe DetectLanguage do
47
57
  end
48
58
  end
49
59
 
50
- it "should raise exception for invalid key" do
51
- old_api_key = subject.configuration.api_key
60
+ describe '.user_status' do
61
+ subject(:user_status) { DetectLanguage.user_status }
52
62
 
53
- subject.configuration.api_key = "invalid"
63
+ it 'fetches user status' do
64
+ expect(user_status['date']).to be_kind_of(String)
65
+ expect(user_status['requests']).to be_kind_of(Integer)
66
+ expect(user_status['bytes']).to be_kind_of(Integer)
67
+ expect(user_status['plan']).to be_kind_of(String)
68
+ expect(user_status['daily_requests_limit']).to be_kind_of(Integer)
69
+ expect(user_status['daily_bytes_limit']).to be_kind_of(Integer)
70
+ end
71
+ end
54
72
 
55
- lambda {
56
- subject.detect("Hello world")
57
- }.should raise_error(::DetectLanguage::Exception)
73
+ describe '.languages' do
74
+ subject(:languages) { DetectLanguage.languages }
58
75
 
59
- subject.configuration.api_key = old_api_key
76
+ it 'fetches list of detectable languages' do
77
+ expect(languages).to include({'code' => 'en', 'name' => 'ENGLISH'})
78
+ end
60
79
  end
61
-
62
- end
80
+ end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: detect_language
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
5
- prerelease:
4
+ version: 1.0.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Laurynas Butkus
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-17 00:00:00.000000000 Z
11
+ date: 2013-11-17 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Language Detection API Client
15
14
  email:
@@ -35,33 +34,26 @@ files:
35
34
  - spec/spec_helper.rb
36
35
  homepage: https://github.com/detectlanguage/detect_language
37
36
  licenses: []
37
+ metadata: {}
38
38
  post_install_message:
39
39
  rdoc_options: []
40
40
  require_paths:
41
41
  - lib
42
42
  required_ruby_version: !ruby/object:Gem::Requirement
43
- none: false
44
43
  requirements:
45
- - - ! '>='
44
+ - - '>='
46
45
  - !ruby/object:Gem::Version
47
46
  version: '0'
48
- segments:
49
- - 0
50
- hash: -807280730191114243
51
47
  required_rubygems_version: !ruby/object:Gem::Requirement
52
- none: false
53
48
  requirements:
54
- - - ! '>='
49
+ - - '>='
55
50
  - !ruby/object:Gem::Version
56
51
  version: '0'
57
- segments:
58
- - 0
59
- hash: -807280730191114243
60
52
  requirements: []
61
53
  rubyforge_project:
62
- rubygems_version: 1.8.24
54
+ rubygems_version: 2.0.6
63
55
  signing_key:
64
- specification_version: 3
56
+ specification_version: 4
65
57
  summary: Detects language of given text. Returns detected language codes and scores.
66
58
  test_files:
67
59
  - spec/detect_language_spec.rb