linguin 1.0.0

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
+ SHA256:
3
+ metadata.gz: 9c59595249f391eca362ace8550146028defcab40662a9d8d84c8d2ba064b739
4
+ data.tar.gz: 0472447d4afb70e4b3f5be9834c6c8deb5270b175c4d36c45acd47fa4c980637
5
+ SHA512:
6
+ metadata.gz: 41dcf5f9ed3c89e95955f81ee4e82cf171ac7704707e4cd3cb747e796b08ef69b9a5693b6e4949bd7639f6abe0234a904393a99c7ab480c008b1181a68e1cd85
7
+ data.tar.gz: 0a2a092184db1c9f83afd9961c3f0f55166d5f8d1e6e3880e5d4b9439ebebea6fdcebc796bb3d4ebed0e5a85787a8789d4c47dd528a820b2d01b4a342ceab7bb
@@ -0,0 +1,16 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.1
14
+ bundler-cache: true
15
+ - name: Run the default task
16
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,40 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Documentation cache and generated files:
20
+ /.yardoc/
21
+ /_yardoc/
22
+ /doc/
23
+ /rdoc/
24
+
25
+ ## Environment normalization:
26
+ /.bundle/
27
+ /vendor/bundle
28
+ /lib/bundler/man/
29
+
30
+ # for a library or gem, you might want to ignore these files since the code is
31
+ # intended to run in multiple environments; otherwise, check them in:
32
+ Gemfile.lock
33
+ .ruby-version
34
+ .ruby-gemset
35
+
36
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
37
+ .rvmrc
38
+
39
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
40
+ .rubocop-https?--*
data/.rubocop.yml ADDED
@@ -0,0 +1,74 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ Style/StringLiterals:
4
+ Enabled: true
5
+ EnforcedStyle: double_quotes
6
+ Style/StringLiteralsInInterpolation:
7
+ Enabled: true
8
+ EnforcedStyle: double_quotes
9
+ Layout/LineLength:
10
+ Max: 120
11
+ Layout/EmptyLineBetweenDefs:
12
+ AllowAdjacentOneLineDefs: true
13
+ Metrics/MethodLength:
14
+ Max: 15
15
+ Gemspec/DateAssignment: # (new in 1.10)
16
+ Enabled: true
17
+ Layout/SpaceBeforeBrackets: # (new in 1.7)
18
+ Enabled: true
19
+ Lint/AmbiguousAssignment: # (new in 1.7)
20
+ Enabled: true
21
+ Lint/DeprecatedConstants: # (new in 1.8)
22
+ Enabled: true
23
+ Lint/DuplicateBranch: # (new in 1.3)
24
+ Enabled: true
25
+ Lint/DuplicateRegexpCharacterClassElement: # (new in 1.1)
26
+ Enabled: true
27
+ Lint/EmptyBlock: # (new in 1.1)
28
+ Enabled: true
29
+ Lint/EmptyClass: # (new in 1.3)
30
+ Enabled: true
31
+ Lint/LambdaWithoutLiteralBlock: # (new in 1.8)
32
+ Enabled: true
33
+ Lint/NoReturnInBeginEndBlocks: # (new in 1.2)
34
+ Enabled: true
35
+ Lint/NumberedParameterAssignment: # (new in 1.9)
36
+ Enabled: true
37
+ Lint/OrAssignmentToConstant: # (new in 1.9)
38
+ Enabled: true
39
+ Lint/RedundantDirGlobSort: # (new in 1.8)
40
+ Enabled: true
41
+ Lint/SymbolConversion: # (new in 1.9)
42
+ Enabled: true
43
+ Lint/ToEnumArguments: # (new in 1.1)
44
+ Enabled: true
45
+ Lint/TripleQuotes: # (new in 1.9)
46
+ Enabled: true
47
+ Lint/UnexpectedBlockArity: # (new in 1.5)
48
+ Enabled: true
49
+ Lint/UnmodifiedReduceAccumulator: # (new in 1.1)
50
+ Enabled: true
51
+ Style/ArgumentsForwarding: # (new in 1.1)
52
+ Enabled: true
53
+ Style/CollectionCompact: # (new in 1.2)
54
+ Enabled: true
55
+ Style/DocumentDynamicEvalDefinition: # (new in 1.1)
56
+ Enabled: true
57
+ Style/EndlessMethod: # (new in 1.8)
58
+ Enabled: true
59
+ Style/HashConversion: # (new in 1.10)
60
+ Enabled: true
61
+ Style/HashExcept: # (new in 1.7)
62
+ Enabled: true
63
+ Style/IfWithBooleanLiteralBranches: # (new in 1.9)
64
+ Enabled: true
65
+ Style/NegatedIfElseCondition: # (new in 1.2)
66
+ Enabled: true
67
+ Style/NilLambda: # (new in 1.3)
68
+ Enabled: true
69
+ Style/RedundantArgument: # (new in 1.4)
70
+ Enabled: true
71
+ Style/StringChars: # (new in 1.12)
72
+ Enabled: true
73
+ Style/SwapValues: # (new in 1.1)
74
+ Enabled: true
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [1.0.0] - 2021-05-12
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at jan@general-scripting.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 General Scripting GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # Linguin AI Ruby wrapper
2
+
3
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard) ![build](https://github.com/LinguinAI/linguin-ruby/actions/workflows/main.yml/badge.svg)
4
+
5
+ This is a Ruby wrapper for the [Linguin AI](https://linguin.ai) API (see [API docs](https://linguin.ai/api-docs/v1)) providing Language Detection as a Service.
6
+
7
+ Linguin AI is free for up to 100 detections per day. You can get your API key [here](https://linguin.ai).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem "linguin"
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install linguin
24
+
25
+ ## Usage
26
+
27
+ Use the Linguin singleton to get started with just a few lines of code:
28
+
29
+ ```ruby
30
+ require "linguin"
31
+
32
+ Linguin.api_key = "YOUR-API-KEY" # goto https://linguin.ai to get your key
33
+
34
+ response = Linguin.detect("test")
35
+ response.success? # => true
36
+ response.results # => [{ lang: "en", confidence: 0.97 }, ...]
37
+ ```
38
+
39
+ If something goes wrong (here: empty text):
40
+
41
+ ```ruby
42
+ response = Linguin.detect("")
43
+ response.success? # => false
44
+ response.error
45
+ # => { code: 400, message: "The language of an empty text is more of a philosophical question." }
46
+
47
+ # if you prefer to handle exceptions instead you can use `#detect!`:
48
+ response = Linguin.detect!("")
49
+ # => raises Linguin::InputError
50
+ ```
51
+
52
+ See the list of all exceptions [here](https://github.com/LinguinAI/linguin-ruby/blob/main/lib/linguin/exceptions.rb).
53
+
54
+ ### Bulk detection
55
+
56
+ To detect the language of multiple texts with one API call, you can pass them as an array. The results will be returned in the same order as the texts.
57
+ All texts have to not be empty. Using `detect!` will result in an exception as for single detections.
58
+
59
+ ```ruby
60
+ response = Linguin.detect(["test", "bahnhof", "12341234"])
61
+ response.results
62
+ # => [ [{ lang: "en", confidence: 0.97 }, ...], [{ ... }], [] ]
63
+
64
+ response = Linguin.detect(["test", ""])
65
+ response.success? # => false
66
+ response.error
67
+ # => { code: 400, message: "At least one of the texts provided was empty." }
68
+
69
+ Linguin.detect!(["test", ""])
70
+ # => raises Linguin::InputError
71
+ ```
72
+
73
+ ### Account status
74
+
75
+ You can fetch the status of your account:
76
+
77
+ ```ruby
78
+ status = Linguin.status
79
+ status.detections_today # => 4_500
80
+ status.daily_limit # => 50_000 or nil for no limit
81
+ status.remaining_today # => 45_500 or Float::INFINITY for unlimited
82
+ ```
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
87
+
88
+ To install this gem onto your local machine, run `bundle exec rake install`.
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/LinguinAI/linguin-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/LinguinAI/linguin-ruby/blob/master/CODE_OF_CONDUCT.md).
93
+
94
+ ## License
95
+
96
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
97
+
98
+ ## Code of Conduct
99
+
100
+ Everyone interacting in the Linguin project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/LinguinAI/linguin-ruby/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "linguin"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/linguin.rb ADDED
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "linguin/version"
4
+ require_relative "linguin/exceptions"
5
+ require_relative "linguin/client"
6
+ require_relative "linguin/base_response"
7
+ require_relative "linguin/detection"
8
+ require_relative "linguin/bulk_detection"
9
+ require_relative "linguin/status"
10
+
11
+ # == Linguin API wrapper module
12
+ # Can be used as a singleton to access all API methods.
13
+ # Alternatively, Linguin::Client can be instantiated.
14
+ #
15
+ # = Linguin.detect
16
+ # [:+text+:] The text to be used for language detection.
17
+ # Attempts to detect the language of the given text.
18
+ # Returns a Linguin::Response object.
19
+ #
20
+ # = Linguin.detect!
21
+ # [:+text+:] The text to be used for language detection.
22
+ # Just like #detect but raises an exception if anything goes wrong.
23
+ #
24
+ # = Linguin.status
25
+ # Returns the status of your Linguin account as Linguin::Status
26
+ # * number of requests today
27
+ # * daily detection limit of your account (false for unlimited)
28
+ # * remaining detections today (can be Infinity)
29
+ module Linguin
30
+ class << self
31
+ def api_key=(api_key)
32
+ default_client.api_key = api_key
33
+ end
34
+
35
+ def detect(text)
36
+ default_client.detect(text)
37
+ end
38
+
39
+ def detect!(text)
40
+ default_client.detect!(text)
41
+ end
42
+
43
+ def status
44
+ default_client.status
45
+ end
46
+
47
+ private
48
+
49
+ def default_client
50
+ @default_client ||= Client.new
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Linguin
4
+ # == Linguin::BaseResponse
5
+ # Base class for Linguin::Detection && Linguin::Status
6
+ class BaseResponse
7
+ CODE_MAP = {
8
+ 400 => InputError,
9
+ 401 => AuthenticationError,
10
+ 404 => NotFoundError,
11
+ 422 => InputError,
12
+ 429 => RateLimitError,
13
+ 500 => InternalError,
14
+ 503 => InternalError
15
+ }.freeze
16
+
17
+ attr_accessor :error
18
+
19
+ def initialize
20
+ yield self
21
+ end
22
+
23
+ class << self
24
+ def from_httparty(response:)
25
+ if response.code == 200
26
+ response = JSON.parse response, symbolize_names: true
27
+ success(response)
28
+ else
29
+ error(response.code, response.body)
30
+ end
31
+ end
32
+ end
33
+
34
+ def raise_on_error!
35
+ return self unless error
36
+
37
+ error_klass = CODE_MAP[error[:code]] || Error
38
+ error_message = error[:message].empty? ? "unknown" : error[:message]
39
+ raise error_klass, "#{error[:code]} / #{error_message}"
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Linguin
4
+ # == Linguin::BulkDetection
5
+ # Returned by Linguin#detect(!) when called with an array of strings.
6
+ #
7
+ # #success? - Bool - checks if detection results were found
8
+ # #error - Hash - contains `error` and `message` about what went wrong
9
+ # #results - Array - contains the detection results for each text, ordered by confidence descending
10
+ class BulkDetection < Detection; end
11
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "httparty"
4
+
5
+ module Linguin
6
+ # == Linguin::Client
7
+ # Is used internally when working with Linguin directly.
8
+ # Can be instanciated to work with multiple clients.
9
+ class Client
10
+ include HTTParty
11
+
12
+ base_uri "https://api.linguin.ai/v1"
13
+
14
+ # we are parsing the JSON response in Linguin::BaseResponse
15
+ # in order to have symbolized keys
16
+ # see https://github.com/jnunemaker/httparty/tree/master/docs#parsing-json
17
+ format :plain
18
+
19
+ attr_accessor :headers
20
+
21
+ def initialize(key = nil)
22
+ configure_api_key(key)
23
+ end
24
+
25
+ def api_key=(key)
26
+ configure_api_key(key)
27
+ end
28
+
29
+ def detect(text)
30
+ ensure_api_key!
31
+
32
+ return bulk(text) if text.is_a?(Array)
33
+
34
+ text = sanitize(text)
35
+
36
+ return Detection.error(400, "The language of an empty text is more of a philosophical question.") if text.empty?
37
+
38
+ httparty_response = self.class.post("/detect", headers: headers, body: { q: text })
39
+ Detection.from_httparty(response: httparty_response)
40
+ end
41
+
42
+ def detect!(text)
43
+ detect(text).raise_on_error!
44
+ end
45
+
46
+ def bulk(texts)
47
+ texts = texts.map { |text| sanitize(text) }
48
+
49
+ return BulkDetection.error(400, "At least one of the texts provided was empty.") if texts.any?(&:empty?)
50
+
51
+ httparty_response = self.class.post("/bulk", headers: headers, body: { q: texts })
52
+ BulkDetection.from_httparty(response: httparty_response)
53
+ end
54
+
55
+ def bulk!(texts)
56
+ bulk(texts).raise_on_error!
57
+ end
58
+
59
+ def status
60
+ ensure_api_key!
61
+ httparty_response = self.class.get("/status", headers: headers)
62
+ Status.from_httparty(response: httparty_response)
63
+ end
64
+
65
+ private
66
+
67
+ def configure_api_key(key)
68
+ @api_key = key
69
+ self.headers = { "Authorization" => "Bearer #{key}" }
70
+ end
71
+
72
+ def ensure_api_key!
73
+ raise Linguin::AuthenticationError, "Seems like you forgot to set Linguin.api_key" unless @api_key
74
+ end
75
+
76
+ def sanitize(text)
77
+ text ||= ""
78
+ text.to_s.strip
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Linguin
4
+ # == Linguin::Detection
5
+ # Returned by Linguin#detect(!).
6
+ #
7
+ # #success? - Bool - checks if detection results were found
8
+ # #error - Hash - contains `error` and `message` about what went wrong
9
+ # #results - Array - contains the detection results, ordered by confidence descending
10
+ class Detection < BaseResponse
11
+ attr_writer :success
12
+ attr_accessor :results
13
+
14
+ class << self
15
+ def error(code, message)
16
+ new do |detection|
17
+ detection.success = false
18
+ detection.error = {
19
+ code: code,
20
+ message: message
21
+ }
22
+ end
23
+ end
24
+
25
+ def success(results)
26
+ new do |detection|
27
+ detection.success = true
28
+ detection.results = results[:results]
29
+ end
30
+ end
31
+ end
32
+
33
+ def success?
34
+ !!@success
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Linguin
4
+ class Error < StandardError; end
5
+ class InputError < Error; end
6
+ class NotFoundError < Error; end
7
+ class AuthenticationError < Error; end
8
+ class RateLimitError < Error; end
9
+ class InternalError < Error; end
10
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Linguin
4
+ # == Linguin::Status
5
+ # Returned by Linguin#status.
6
+ #
7
+ # [:+detections_today+:] how many detections you performed today
8
+ # [:+daily_limit+:] your daily detection limit (or false)
9
+ # [:+remaining_today+:] detections remaining today (can be Float::INFINITY)
10
+ class Status < BaseResponse
11
+ attr_accessor :detections_today, :daily_limit, :remaining_today
12
+
13
+ class << self
14
+ def error(code, message)
15
+ new do |status|
16
+ status.error = {
17
+ code: code,
18
+ message: message
19
+ }
20
+ end.raise_on_error!
21
+ end
22
+
23
+ def success(response)
24
+ response = response[:status]
25
+ detections_today = response[:detections_today].to_i
26
+ daily_limit = response[:daily_limit] == -1 ? nil : response[:daily_limit].to_i
27
+ remaining_today = response[:remaining_today] == -1 ? Float::INFINITY : response[:remaining_today].to_i
28
+ new do |status|
29
+ status.detections_today = detections_today
30
+ status.daily_limit = daily_limit
31
+ status.remaining_today = remaining_today
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Linguin
4
+ VERSION = "1.0.0"
5
+ end
data/linguin.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/linguin/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "linguin"
7
+ spec.version = Linguin::VERSION
8
+ spec.authors = ["Jan Schwenzien"]
9
+ spec.email = ["jan@general-scripting.com"]
10
+
11
+ spec.summary = "API wrapper for the language detection as a service Linguin AI."
12
+ spec.description = "This is a Ruby API wrapper for consuming the https://linguin.ai/ API allowing you "\
13
+ "to detect the language of a text fast and with high accuracy."
14
+ spec.homepage = "https://linguin.ai/"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/LinguinAI/linguin-ruby"
20
+ spec.metadata["changelog_uri"] = "https://github.com/LinguinAI/linguin-ruby/blob/master/CHANGELOG.md"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "minitest", "~> 5.0"
32
+ spec.add_development_dependency "rake", "~> 13.0"
33
+ spec.add_development_dependency "rubocop", "~> 1.7"
34
+ spec.add_development_dependency "rubocop-minitest", "~> 0.12"
35
+ spec.add_development_dependency "rubocop-rake", "~> 0.5"
36
+ spec.add_development_dependency "webmock", "~> 3.12"
37
+
38
+ spec.add_dependency "httparty"
39
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linguin
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jan Schwenzien
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop-minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.12'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.12'
97
+ - !ruby/object:Gem::Dependency
98
+ name: httparty
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: This is a Ruby API wrapper for consuming the https://linguin.ai/ API
112
+ allowing you to detect the language of a text fast and with high accuracy.
113
+ email:
114
+ - jan@general-scripting.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".github/workflows/main.yml"
120
+ - ".gitignore"
121
+ - ".rubocop.yml"
122
+ - CHANGELOG.md
123
+ - CODE_OF_CONDUCT.md
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - lib/linguin.rb
131
+ - lib/linguin/base_response.rb
132
+ - lib/linguin/bulk_detection.rb
133
+ - lib/linguin/client.rb
134
+ - lib/linguin/detection.rb
135
+ - lib/linguin/exceptions.rb
136
+ - lib/linguin/status.rb
137
+ - lib/linguin/version.rb
138
+ - linguin.gemspec
139
+ homepage: https://linguin.ai/
140
+ licenses:
141
+ - MIT
142
+ metadata:
143
+ homepage_uri: https://linguin.ai/
144
+ source_code_uri: https://github.com/LinguinAI/linguin-ruby
145
+ changelog_uri: https://github.com/LinguinAI/linguin-ruby/blob/master/CHANGELOG.md
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: 2.5.0
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubygems_version: 3.2.15
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: API wrapper for the language detection as a service Linguin AI.
165
+ test_files: []