Skip to content

Commit

Permalink
Merge pull request #1 from omarroth/v0.24.2
Browse files Browse the repository at this point in the history
Update to Crystal v0.24
  • Loading branch information
laurynas authored Mar 18, 2018
2 parents f7aed05 + 4568472 commit e7201af
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 62 deletions.
3 changes: 0 additions & 3 deletions Projectfile

This file was deleted.

9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ Detect Language API Crystal Client

## Installation

Add it to `Projectfile`
Add it to `shard.yml`

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

## Usage
Expand Down
7 changes: 7 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: detect_language
version: 0.1.0

authors:
- Laurynas Butkus <[email protected]>

license: MIT
12 changes: 6 additions & 6 deletions spec/detect_language_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ require "./spec_helper"
describe DetectLanguage do
it "configures" do
configured do
DetectLanguage.configuration.api_key.should eq(API_KEY)
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
end

it "allows configuring" do
Expand All @@ -23,12 +23,12 @@ describe DetectLanguage do
configured do
results = DetectLanguage.detect("Hello world")
results[0].language.should eq("en")
results[0].is_reliable?.should be_true
results[0].isReliable.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
results[0].language.should eq("lt")
end
end
end
end
end
4 changes: 2 additions & 2 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ API_KEY = "c38b179b0b2861a77322e27be51e94ce"
def configured
DetectLanguage.configure do |config|
config.api_key = API_KEY
end
end

yield
end
end
4 changes: 2 additions & 2 deletions src/detect_language.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ module DetectLanguage
end

def detect(data : String)
result = client.post(:detect, { "q" => data })
data = result.data as DetectData
result = client.post(:detect, {"q" => data})
data = result.data.not_nil!
data.detections
end
end
23 changes: 12 additions & 11 deletions src/detect_language/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ require "json"

module DetectLanguage
class Client
property :configuration
@headers : HTTP::Headers | Nil
property configuration : 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)
response = http_client.post(path: request_uri(method), headers: headers, form: request_params(params))
parse_response(response)
end

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

private def headers
Expand All @@ -25,17 +26,17 @@ module DetectLanguage
private def build_headers
headers = HTTP::Headers.new
headers.add("User-Agent", configuration.user_agent)
headers
return 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
error = result.error.not_nil!
raise error.message
end

result
end

Expand All @@ -47,4 +48,4 @@ module DetectLanguage
@http_client ||= HTTP::Client.new(configuration.host, configuration.port, configuration.secure)
end
end
end
end
33 changes: 11 additions & 22 deletions src/detect_language/configuration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@ 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
property api_key : String

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

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

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

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

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

alias_method :secure?, :secure
property secure : Bool

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

def port
@port || default_port
@port ||= 443
end

# Allows config options to be read like a hash
Expand All @@ -39,16 +39,5 @@ module DetectLanguage
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
end
20 changes: 9 additions & 11 deletions src/detect_language/detect_response.cr
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
module DetectLanguage
class DetectResponse
json_mapping({
data: { type: DetectData, nilable: true },
error: { type: ErrorData, nilable: true },
JSON.mapping({
data: {type: DetectData, nilable: true},
error: {type: ErrorData, nilable: true},
})
end

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

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

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

alias_method :is_reliable?, :isReliable
end
end
end
2 changes: 1 addition & 1 deletion src/detect_language/error.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module DetectLanguage
class Error < Exception
end
end
end

0 comments on commit e7201af

Please sign in to comment.