Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
laurynas committed Aug 23, 2015
0 parents commit f7aed05
Show file tree
Hide file tree
Showing 13 changed files with 289 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.deps/
/libs/
/.crystal/
/doc/


# Libraries don't need dependency lock
# Dependencies will be locked in application that uses them
/.deps.lock

1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: crystal
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Laurynas Butkus

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions Projectfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
deps do
# github "[your-github-name]/example"
end
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# detect_language

Detect Language API Crystal Client

## Installation

Add it to `Projectfile`

```crystal
deps do
github "detectlanguage/detectlanguage-crystal", name: "detect_language"
end
```

## Usage

```crystal
require "detect_language"
DetectLanguage.configure do |config|
config.api_key = "YOUR API KEY"
end
```

### Language detection

```crystal
results = DetectLanguage.detect("Buenos dias señor")
pp results[0].language
```

## Contributing

1. Fork it ( https://github.com/detectlanguage/detectlanguage-crystal/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request
34 changes: 34 additions & 0 deletions spec/detect_language_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "./spec_helper"

describe DetectLanguage do
it "configures" do
configured do
DetectLanguage.configuration.api_key.should eq(API_KEY)
DetectLanguage.configuration.host.should eq("ws.detectlanguage.com")
DetectLanguage.configuration.user_agent.should contain("Detect Language API Crystal Client")
DetectLanguage.configuration.user_agent.should contain(DetectLanguage::VERSION)
end
end

it "allows configuring" do
DetectLanguage.configure do |config|
config.user_agent = "Detect Language testing"
end

DetectLanguage.configuration.user_agent.should eq("Detect Language testing")
end

describe ".detect" do
it "detects languages" do
configured do
results = DetectLanguage.detect("Hello world")
results[0].language.should eq("en")
results[0].is_reliable?.should be_true
results[0].confidence.should be_a(Float64)

results = DetectLanguage.detect("Jau saulelė vėl atkopdama budino svietą")
results[0].language.should eq("lt")
end
end
end
end
12 changes: 12 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "spec"
require "../src/detect_language"

API_KEY = "c38b179b0b2861a77322e27be51e94ce"

def configured
DetectLanguage.configure do |config|
config.api_key = API_KEY
end

yield
end
28 changes: 28 additions & 0 deletions src/detect_language.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "json"
require "./detect_language/version"
require "./detect_language/configuration"
require "./detect_language/error"
require "./detect_language/client"
require "./detect_language/detect_response"

module DetectLanguage
extend self

def configure
yield(configuration)
end

def configuration
@@configuration ||= Configuration.new
end

def client
@@client ||= Client.new(configuration)
end

def detect(data : String)
result = client.post(:detect, { "q" => data })
data = result.data as DetectData
data.detections
end
end
50 changes: 50 additions & 0 deletions src/detect_language/client.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require "http/client"
require "json"

module DetectLanguage
class Client
property :configuration

def initialize(configuration)
@configuration = configuration
end

def post(method, params)
response = http_client.post_form(request_uri(method), request_params(params), headers)
parse_response(response)
end

private def request_params(params)
params.merge({ "key" => configuration.api_key })
end

private def headers
@headers ||= build_headers
end

private def build_headers
headers = HTTP::Headers.new
headers.add("User-Agent", configuration.user_agent)
headers
end

private def parse_response(response)
result = DetectResponse.from_json(response.body)

if result.error
error = result.error as ErrorData
raise Error.new(error.message)
end

result
end

def request_uri(method)
"/#{configuration.api_version}/#{method}"
end

private def http_client
@http_client ||= HTTP::Client.new(configuration.host, configuration.port, configuration.secure)
end
end
end
54 changes: 54 additions & 0 deletions src/detect_language/configuration.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module DetectLanguage
class Configuration
# The API key for your project, found on your homepage after you login into detectlanguage.com website
# Defaults to 'demo', which has a limited number of requests.
property :api_key

# The API version you are using (defaults to 0.2).
property :api_version

# HTTP request user agent (defaults to 'Detect Language API ruby gem').
property :user_agent

# The host to connect to (defaults to ws.detectlanguage.com).
property :host

# The port on which your DetectLanguage server runs (defaults to 443 for secure
# connections, 80 for insecure connections).
property :port

# +true+ for https connections, +false+ for http connections.
property :secure

alias_method :secure?, :secure

def initialize
@api_key = nil
@api_version = "0.2"
@host = "ws.detectlanguage.com"
@user_agent = "Detect Language API Crystal Client " + VERSION
end

def port
@port || default_port
end

# Allows config options to be read like a hash
#
# @param [Symbol] option Key for a given attribute
def [](option)
send(option)
end

# Determines what port should we use for sending requests.
# @return [Fixnum] Returns 443 if you've set secure to true in your
# configuration, and 80 otherwise.
private def default_port
if secure?
443
else
80
end
end
end
end
30 changes: 30 additions & 0 deletions src/detect_language/detect_response.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module DetectLanguage
class DetectResponse
json_mapping({
data: { type: DetectData, nilable: true },
error: { type: ErrorData, nilable: true },
})
end

class ErrorData
json_mapping({
message: String,
})
end

class DetectData
json_mapping({
detections: Array(Detection),
})
end

class Detection
json_mapping({
language: String,
isReliable: Bool,
confidence: Float64,
})

alias_method :is_reliable?, :isReliable
end
end
4 changes: 4 additions & 0 deletions src/detect_language/error.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module DetectLanguage
class Error < Exception
end
end
3 changes: 3 additions & 0 deletions src/detect_language/version.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module DetectLanguage
VERSION = "0.0.1"
end

0 comments on commit f7aed05

Please sign in to comment.