From 456847203177b02079e7e6a2624122455c848c7b Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Fri, 16 Mar 2018 17:35:30 -0500 Subject: [PATCH] Update to v0.24.2 --- Projectfile | 3 --- README.md | 9 +++---- shard.yml | 7 ++++++ spec/detect_language_spec.cr | 12 +++++----- spec/spec_helper.cr | 4 ++-- src/detect_language.cr | 4 ++-- src/detect_language/client.cr | 23 +++++++++--------- src/detect_language/configuration.cr | 33 +++++++++----------------- src/detect_language/detect_response.cr | 20 +++++++--------- src/detect_language/error.cr | 2 +- 10 files changed, 55 insertions(+), 62 deletions(-) delete mode 100644 Projectfile create mode 100644 shard.yml diff --git a/Projectfile b/Projectfile deleted file mode 100644 index 67600a2..0000000 --- a/Projectfile +++ /dev/null @@ -1,3 +0,0 @@ -deps do - # github "[your-github-name]/example" -end diff --git a/README.md b/README.md index a56b687..b410658 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/shard.yml b/shard.yml new file mode 100644 index 0000000..206c40f --- /dev/null +++ b/shard.yml @@ -0,0 +1,7 @@ +name: detect_language +version: 0.1.0 + +authors: + - Laurynas Butkus + +license: MIT diff --git a/spec/detect_language_spec.cr b/spec/detect_language_spec.cr index c80b17a..ec0a6fb 100644 --- a/spec/detect_language_spec.cr +++ b/spec/detect_language_spec.cr @@ -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 @@ -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 diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index d4ea67b..269fa64 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -6,7 +6,7 @@ API_KEY = "c38b179b0b2861a77322e27be51e94ce" def configured DetectLanguage.configure do |config| config.api_key = API_KEY - end + end yield -end +end diff --git a/src/detect_language.cr b/src/detect_language.cr index d25edd7..7122547 100644 --- a/src/detect_language.cr +++ b/src/detect_language.cr @@ -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 diff --git a/src/detect_language/client.cr b/src/detect_language/client.cr index a274304..68c23e4 100644 --- a/src/detect_language/client.cr +++ b/src/detect_language/client.cr @@ -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 @@ -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 @@ -47,4 +48,4 @@ module DetectLanguage @http_client ||= HTTP::Client.new(configuration.host, configuration.port, configuration.secure) end end -end \ No newline at end of file +end diff --git a/src/detect_language/configuration.cr b/src/detect_language/configuration.cr index ce054c5..de7e7f8 100644 --- a/src/detect_language/configuration.cr +++ b/src/detect_language/configuration.cr @@ -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 @@ -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 \ No newline at end of file +end diff --git a/src/detect_language/detect_response.cr b/src/detect_language/detect_response.cr index c63dcd6..dd10c79 100644 --- a/src/detect_language/detect_response.cr +++ b/src/detect_language/detect_response.cr @@ -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 \ No newline at end of file +end diff --git a/src/detect_language/error.cr b/src/detect_language/error.cr index e0cf54f..70881e6 100644 --- a/src/detect_language/error.cr +++ b/src/detect_language/error.cr @@ -1,4 +1,4 @@ module DetectLanguage class Error < Exception end -end \ No newline at end of file +end