Skip to content

Commit 70d722e

Browse files
committed
Use JSON payloads
1 parent 19402cf commit 70d722e

3 files changed

Lines changed: 16 additions & 28 deletions

File tree

README.md

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ Detect Language API Ruby Client
66

77
Detects language of the given text. Returns detected language codes and scores.
88

9-
Before using Detect Language API client you have to setup your personal API key.
10-
You can get it by signing up at https://detectlanguage.com
11-
129
## Installation
1310

1411
Add this line to your application's Gemfile:
@@ -17,23 +14,13 @@ Add this line to your application's Gemfile:
1714
gem 'detect_language'
1815
```
1916

20-
Or install it yourself as:
21-
22-
```
23-
gem install detect_language
24-
```
25-
2617
### Configuration
2718

28-
If you are using Rails, create initializer `config/initializers/detect_language.rb` and add following code there.
29-
Otherwise just integrate following code into your apps configuration.
19+
Get your personal API key by signing up at https://detectlanguage.com
3020

3121
```ruby
3222
DetectLanguage.configure do |config|
3323
config.api_key = "YOUR API KEY"
34-
35-
# enable secure mode (SSL) if you are passing sensitive data
36-
# config.secure = true
3724
end
3825
```
3926

@@ -51,12 +38,12 @@ DetectLanguage.detect("Buenos dias señor")
5138
[{"language"=>"es", "isReliable"=>true, "confidence"=>6.62}]
5239
```
5340

54-
### Simple language detection
41+
### Language code detection
5542

56-
If you need just a language code you can use `simple_detect`. It returns just the language code.
43+
If you need just a language code you can use `detect_code`.
5744

5845
```ruby
59-
DetectLanguage.simple_detect("Buenos dias señor")
46+
DetectLanguage.detect_code("Buenos dias señor")
6047
```
6148

6249
#### Result
@@ -69,10 +56,10 @@ DetectLanguage.simple_detect("Buenos dias señor")
6956

7057
It is possible to detect language of several texts with one request.
7158
This method is significantly faster than doing one request per text.
72-
To use batch detection just pass array of texts to `detect` method.
59+
To use batch detection just pass array of texts to `detect_batch` method.
7360

7461
```ruby
75-
DetectLanguage.detect(["Buenos dias señor", "Labas rytas"])
62+
DetectLanguage.detect_batch(["Buenos dias señor", "Labas rytas"])
7663
```
7764

7865
#### Result
@@ -87,7 +74,7 @@ Result is array of detections in the same order as the texts were passed.
8774
### Getting your account status
8875

8976
```ruby
90-
DetectLanguage.user_status
77+
DetectLanguage.account_status
9178
```
9279

9380
#### Result

lib/detect_language.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def detect(query)
2626
def detect_batch(queries)
2727
raise(ArgumentError, 'Expected an Array of queries') unless queries.is_a?(Array)
2828

29-
client.post('detect-batch', 'q[]': queries)
29+
client.post('detect-batch', q: queries)
3030
end
3131

3232
def detect_code(text)

lib/detect_language/client.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@ def initialize(config)
1010
@config = config
1111
end
1212

13-
def post(path, params = {})
14-
execute(Net::HTTP::Post, path, params)
13+
def post(path, payload = {})
14+
execute(Net::HTTP::Post, path, body: payload.to_json)
1515
end
1616

17-
def get(path, params = {})
18-
execute(Net::HTTP::Get, path, params)
17+
def get(path)
18+
execute(Net::HTTP::Get, path)
1919
end
2020

2121
private
2222

23-
def execute(method, path, params)
24-
uri = URI.parse(config.base_url)
23+
def execute(method, path, body: nil)
24+
uri = URI(config.base_url)
2525
http = setup_http_connection(uri)
2626
request = method.new(uri.path + path)
27-
request.set_form_data(params)
27+
request.body = body
2828

29+
request['Content-Type'] = 'application/json'
2930
request['Authorization'] = 'Bearer ' + config.api_key.to_s
3031
request['User-Agent'] = config.user_agent
3132

0 commit comments

Comments
 (0)