lightcast-ruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6e84099140e62596cbb9183473bfebee944442e8dbedc4fac70b8c5935c168c1
4
+ data.tar.gz: a00a7882cd0025d56927bf3b8b695904d168e7bfc347478f6d770ed8667c9b7b
5
+ SHA512:
6
+ metadata.gz: '08905bbcf66ab46fbc0fd337c6ad54aa8bdc8416f1c915d400f871f91e61fa9a80ee2bcf8417bfb8e1df3356f2d862b829666a82846a8e8a3fada84a615ce3b4'
7
+ data.tar.gz: dfc54efac8196b4c12f3a439b62dc4ac8f24c607085ca83ada13c9f5a430e9793dbf32bfc490a0cc60ade9755e628f2c9550ad55d9ba9f187e3a8e1590b6c361
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,49 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ NewCops: enable
6
+ SuggestExtensions: false
7
+ TargetRubyVersion: 2.6
8
+
9
+ Metrics/BlockLength:
10
+ Enabled: true
11
+ Exclude:
12
+ - spec/**/*
13
+
14
+ Naming/FileName:
15
+ Enabled: true
16
+ Exclude:
17
+ - lib/lightcast-ruby.rb
18
+
19
+ RSpec/ExampleLength:
20
+ Enabled: false
21
+
22
+ RSpec/FilePath:
23
+ Enabled: false
24
+
25
+ RSpec/InstanceVariable:
26
+ Enabled: false
27
+
28
+ RSpec/MultipleMemoizedHelpers:
29
+ Enabled: false
30
+
31
+ RSpec/SubjectStub:
32
+ Enabled: false
33
+
34
+ RSpec/VerifiedDoubles:
35
+ Enabled: false
36
+
37
+ Style/Documentation:
38
+ Enabled: false
39
+
40
+ Style/StringLiterals:
41
+ Enabled: true
42
+ EnforcedStyle: single_quotes
43
+
44
+ Style/StringLiteralsInInterpolation:
45
+ Enabled: true
46
+ EnforcedStyle: double_quotes
47
+
48
+ Layout/LineLength:
49
+ Max: 150
data/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ ## [Unreleased]
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ gem 'faraday', '~> 1.0', '>= 1.0.0'
8
+
9
+ group :development, :test do
10
+ gem 'rake', '~> 13.0.6'
11
+ gem 'rspec', '~> 3.12.0'
12
+ gem 'rspec_junit_formatter', '~> 0.6.0'
13
+ gem 'rubocop', '~> 1.52.1'
14
+ gem 'rubocop-rspec', '~> 2.22.0'
15
+ gem 'webmock', '~> 3.18.1'
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,110 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lightcast-ruby (0.1.0)
5
+ faraday (~> 1.0, >= 1.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.8.5)
11
+ public_suffix (>= 2.0.2, < 6.0)
12
+ ast (2.4.2)
13
+ crack (0.4.5)
14
+ rexml
15
+ diff-lcs (1.5.0)
16
+ faraday (1.10.3)
17
+ faraday-em_http (~> 1.0)
18
+ faraday-em_synchrony (~> 1.0)
19
+ faraday-excon (~> 1.1)
20
+ faraday-httpclient (~> 1.0)
21
+ faraday-multipart (~> 1.0)
22
+ faraday-net_http (~> 1.0)
23
+ faraday-net_http_persistent (~> 1.0)
24
+ faraday-patron (~> 1.0)
25
+ faraday-rack (~> 1.0)
26
+ faraday-retry (~> 1.0)
27
+ ruby2_keywords (>= 0.0.4)
28
+ faraday-em_http (1.0.0)
29
+ faraday-em_synchrony (1.0.0)
30
+ faraday-excon (1.1.0)
31
+ faraday-httpclient (1.0.1)
32
+ faraday-multipart (1.0.4)
33
+ multipart-post (~> 2)
34
+ faraday-net_http (1.0.1)
35
+ faraday-net_http_persistent (1.2.0)
36
+ faraday-patron (1.0.0)
37
+ faraday-rack (1.0.0)
38
+ faraday-retry (1.0.3)
39
+ hashdiff (1.0.1)
40
+ json (2.6.3)
41
+ multipart-post (2.3.0)
42
+ parallel (1.23.0)
43
+ parser (3.2.2.3)
44
+ ast (~> 2.4.1)
45
+ racc
46
+ public_suffix (5.0.3)
47
+ racc (1.7.3)
48
+ rainbow (3.1.1)
49
+ rake (13.0.6)
50
+ regexp_parser (2.8.1)
51
+ rexml (3.2.6)
52
+ rspec (3.12.0)
53
+ rspec-core (~> 3.12.0)
54
+ rspec-expectations (~> 3.12.0)
55
+ rspec-mocks (~> 3.12.0)
56
+ rspec-core (3.12.2)
57
+ rspec-support (~> 3.12.0)
58
+ rspec-expectations (3.12.3)
59
+ diff-lcs (>= 1.2.0, < 2.0)
60
+ rspec-support (~> 3.12.0)
61
+ rspec-mocks (3.12.6)
62
+ diff-lcs (>= 1.2.0, < 2.0)
63
+ rspec-support (~> 3.12.0)
64
+ rspec-support (3.12.1)
65
+ rspec_junit_formatter (0.6.0)
66
+ rspec-core (>= 2, < 4, != 2.12.0)
67
+ rubocop (1.52.1)
68
+ json (~> 2.3)
69
+ parallel (~> 1.10)
70
+ parser (>= 3.2.2.3)
71
+ rainbow (>= 2.2.2, < 4.0)
72
+ regexp_parser (>= 1.8, < 3.0)
73
+ rexml (>= 3.2.5, < 4.0)
74
+ rubocop-ast (>= 1.28.0, < 2.0)
75
+ ruby-progressbar (~> 1.7)
76
+ unicode-display_width (>= 2.4.0, < 3.0)
77
+ rubocop-ast (1.29.0)
78
+ parser (>= 3.2.1.0)
79
+ rubocop-capybara (2.18.0)
80
+ rubocop (~> 1.41)
81
+ rubocop-factory_bot (2.23.1)
82
+ rubocop (~> 1.33)
83
+ rubocop-rspec (2.22.0)
84
+ rubocop (~> 1.33)
85
+ rubocop-capybara (~> 2.17)
86
+ rubocop-factory_bot (~> 2.22)
87
+ ruby-progressbar (1.13.0)
88
+ ruby2_keywords (0.0.5)
89
+ unicode-display_width (2.4.2)
90
+ webmock (3.18.1)
91
+ addressable (>= 2.8.0)
92
+ crack (>= 0.3.2)
93
+ hashdiff (>= 0.4.0, < 2.0.0)
94
+
95
+ PLATFORMS
96
+ x86_64-darwin-22
97
+ x86_64-linux
98
+
99
+ DEPENDENCIES
100
+ faraday (~> 1.0, >= 1.0.0)
101
+ lightcast-ruby!
102
+ rake (~> 13.0.6)
103
+ rspec (~> 3.12.0)
104
+ rspec_junit_formatter (~> 0.6.0)
105
+ rubocop (~> 1.52.1)
106
+ rubocop-rspec (~> 2.22.0)
107
+ webmock (~> 3.18.1)
108
+
109
+ BUNDLED WITH
110
+ 2.4.10
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Riipen
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,111 @@
1
+ [![CircleCI](https://dl.circleci.com/status-badge/img/gh/riipen/lightcast-ruby/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/riipen/lightcast-ruby/tree/main)
2
+
3
+ # Lightcast Ruby
4
+
5
+ An API client for the Lightcast REST APIs in ruby.
6
+
7
+ Lightcast APIs documentation can be found here:
8
+
9
+ https://docs.lightcast.dev/apis
10
+
11
+ ## Installation
12
+
13
+ Add to your `Gemfile`:
14
+
15
+ ```ruby
16
+ gem 'lightcast-ruby'
17
+ ```
18
+
19
+ Then `bundle install`.
20
+
21
+ ## Usage
22
+
23
+ ### Quick Start
24
+
25
+ Create your client
26
+
27
+ ```ruby
28
+ client = Lightcast::Client.new(
29
+ client_id: '123ABC'
30
+ client_secret: '456DEF'
31
+ scope: 'profiles:us',
32
+ )
33
+ ```
34
+
35
+ and then create an authentication token that lasts 1 hour
36
+
37
+ ```ruby
38
+ client.authenticate
39
+ ```
40
+
41
+ Now you can make use of any of your available APIs for your client.
42
+
43
+ ### Skills API
44
+
45
+ You can access the skills API via
46
+
47
+ ```ruby
48
+ client.skills(version: 'latest')
49
+ ```
50
+
51
+ where the optional version is any valid version.
52
+
53
+ #### Skills Extract
54
+
55
+ Extract skills from plain text.
56
+
57
+ ```ruby
58
+ client.skills.extract({ text: 'blah blah blah' }, { language: 'en', confidence_threshold: 0.5 })
59
+ ```
60
+
61
+ [API docs](https://docs.lightcast.dev/apis/skills#versions-version-extract)
62
+
63
+ #### Skills Get
64
+
65
+ Get a single skill.
66
+
67
+ ```ruby
68
+ client.skills.get(123)
69
+ ```
70
+
71
+ [API docs](https://docs.lightcast.dev/apis/skills#versions-version-skills-skill_id)
72
+
73
+ #### Skills Related
74
+
75
+ Get related skills from provided skills.
76
+
77
+ ```ruby
78
+ client.related.get(ids: ['12345', 'abcde'])
79
+ ```
80
+
81
+ [API docs](https://docs.lightcast.dev/apis/skills#versions-version-related)
82
+
83
+ #### Skills Status
84
+
85
+ Get the status of the skills API.
86
+
87
+ ```ruby
88
+ client.skills.status
89
+ ```
90
+
91
+ [API docs](https://docs.lightcast.dev/apis/skills#status)
92
+
93
+ ### Errors
94
+
95
+ Any error code returned by the Lightcast API will result in one of the following expections
96
+
97
+ |Code|Exception|
98
+ |----|---------|
99
+ |400| Lightcast::BadRequest|
100
+ |401| Lightcast::Unauthorized|
101
+ |403| Lightcast::Forbidden|
102
+ |404| Lightcast::NotFound|
103
+ |410| Lightcast::Gone|
104
+ |4xx| Lightcast::ClientError|
105
+ |500| Lightcast::InternalServerError|
106
+ |503| Lightcast::ServiceUnavailable|
107
+ |5xx| Lightcast::ServerError|
108
+
109
+ ## License
110
+
111
+ Copyright (C) 2023 Riipen. See [LICENSE](https://github.com/riipen/lightcast-ruby/blob/master/LICENSE.md) for details.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lightcast
4
+ module Authentication
5
+ def authenticate # rubocop:disable Metrics/MethodLength
6
+ response = connection_auth.post('/connect/token',
7
+ {
8
+ client_id: @client_id,
9
+ client_secret: @client_secret,
10
+ grant_type: 'client_credentials'
11
+ },
12
+ {
13
+ body: :form,
14
+ content_type: 'application/x-www-form-urlencoded'
15
+ })
16
+
17
+ # Set the client values inline
18
+ @access_token = response['access_token']
19
+
20
+ # Give a chance to the invoker of the client to do something with the response
21
+ response
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'services/skills'
4
+
5
+ module Lightcast
6
+ class Client
7
+ include Lightcast::Authentication
8
+
9
+ BASE_URL_AUTH = 'https://auth.emsicloud.com'
10
+ BASE_URL_SERVICES = 'https://emsiservices.com'
11
+
12
+ def initialize(client_id:, client_secret:, scope:)
13
+ @client_id = client_id
14
+ @client_secret = client_secret
15
+ @scope = scope
16
+
17
+ @skills = nil
18
+ end
19
+
20
+ def connection_auth
21
+ Connection.new(url: BASE_URL_AUTH)
22
+ end
23
+
24
+ def connection_services
25
+ Connection.new(access_token: @access_token, url: BASE_URL_SERVICES)
26
+ end
27
+
28
+ def skills(version: 'latest')
29
+ @skills ||= Lightcast::Services::Skills.new(client: self, version: version)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+
5
+ module Lightcast
6
+ class Connection
7
+ attr_accessor :access_token, :url
8
+
9
+ def initialize(url:, access_token: nil)
10
+ @access_token = access_token
11
+ @url = url
12
+ end
13
+
14
+ def delete(path, **params)
15
+ request(:delete, path, params)
16
+ end
17
+
18
+ def get(path, **params)
19
+ request(:get, path, params)
20
+ end
21
+
22
+ def head(path, **params)
23
+ request(:head, path, params)
24
+ end
25
+
26
+ def post(path, params = {}, options = {})
27
+ request(:post, path, params, options)
28
+ end
29
+
30
+ def put(path, **params)
31
+ request(:put, path, params)
32
+ end
33
+
34
+ def request(method, path, params, options = {}) # rubocop:disable Metrics/MethodLength
35
+ response = connection.public_send(method, path, params) do |request|
36
+ request.headers['accept'] = 'application/json'
37
+ request.headers['Authorization'] = "Bearer #{@access_token}" if @access_token
38
+
39
+ if options[:body] == :form
40
+ request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
41
+ request.body = URI.encode_www_form(params)
42
+ end
43
+ end
44
+
45
+ error = Error.from_response(response)
46
+
47
+ raise error if error
48
+
49
+ response.body
50
+ end
51
+
52
+ private
53
+
54
+ def connection
55
+ @connection ||= Faraday.new(url: @url) do |c|
56
+ c.request :json, content_type: /\bjson$/
57
+ c.response :json, content_type: /\bjson$/
58
+ c.request :url_encoded, content_type: /x-www-form-urlencoded/
59
+ c.adapter Faraday.default_adapter
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lightcast
4
+ class Error < StandardError
5
+ attr_reader :response
6
+
7
+ def self.from_response(response) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength:
8
+ klass =
9
+ case response.status
10
+ when 400 then BadRequest
11
+ when 401 then Unauthorized
12
+ when 403 then Forbidden
13
+ when 404 then NotFound
14
+ when 429 then TooManyRequests
15
+ when 400..499 then ClientError
16
+ when 500 then InternalServerError
17
+ when 503 then ServiceUnavailable
18
+ when 500..599 then ServerError
19
+ end
20
+
21
+ klass&.new(response)
22
+ end
23
+
24
+ def initialize(response = nil)
25
+ @response = response
26
+
27
+ super(build_error_message)
28
+ end
29
+
30
+ private
31
+
32
+ def build_error_message
33
+ return nil if @response.nil? || !@response.body['errors']
34
+
35
+ "#{@response.body.dig("errors", 0, "title")}: #{@response.body.dig("errors", 0, "detail")}"
36
+ end
37
+ end
38
+
39
+ class ClientError < Error; end
40
+ class BadRequest < Error; end
41
+ class Unauthorized < Error; end
42
+ class Forbidden < Error; end
43
+ class NotFound < Error; end
44
+ class TooManyRequests < Error; end
45
+ class ServerError < Error; end
46
+ class InternalServerError < Error; end
47
+ class BadGateway < Error; end
48
+ class ServiceUnavailable < Error; end
49
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lightcast
4
+ module Services
5
+ class Skills
6
+ def initialize(client:, version:)
7
+ @client = client
8
+ @version = version
9
+ end
10
+
11
+ def extract(body = {}, query = { language: 'en', confidence_threshold: 0 })
12
+ @client.connection_services.post(
13
+ "/skills/versions/#{@version}/extract?language=#{query[:language]}&confidenceThreshold=#{query[:confidence_threshold]}", body
14
+ )
15
+ end
16
+
17
+ def get(id)
18
+ @client.connection_services.get("/skills/versions/#{@version}/skills/#{id}")
19
+ end
20
+
21
+ def related(**params)
22
+ @client.connection_services.post("/skills/versions/#{@version}/related", **params)
23
+ end
24
+
25
+ def status
26
+ @client.connection_services.get('/skills/status')
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lightcast
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.dirname(__FILE__)
4
+
5
+ require 'lightcast-ruby/services/skills'
6
+
7
+ require 'lightcast-ruby/authentication'
8
+ require 'lightcast-ruby/client'
9
+ require 'lightcast-ruby/connection'
10
+ require 'lightcast-ruby/error'
11
+ require 'lightcast-ruby/version'
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lightcast-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jordan Ell
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-01-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
33
+ description: Access the Lightcast REST API.
34
+ email:
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - ".rspec"
40
+ - ".rubocop.yml"
41
+ - CHANGELOG.md
42
+ - Gemfile
43
+ - Gemfile.lock
44
+ - LICENSE
45
+ - README.md
46
+ - Rakefile
47
+ - lib/lightcast-ruby.rb
48
+ - lib/lightcast-ruby/authentication.rb
49
+ - lib/lightcast-ruby/client.rb
50
+ - lib/lightcast-ruby/connection.rb
51
+ - lib/lightcast-ruby/error.rb
52
+ - lib/lightcast-ruby/services/skills.rb
53
+ - lib/lightcast-ruby/version.rb
54
+ homepage: https://github.com/riipen/lightcast-ruby
55
+ licenses:
56
+ - MIT
57
+ metadata:
58
+ homepage_uri: https://github.com/riipen/lightcast-ruby
59
+ source_code_uri: https://github.com/riipen/lightcast-ruby
60
+ changelog_uri: https://github.com/riipen/lightcast-ruby/blob/master/CHANGELOG.md
61
+ rubygems_mfa_required: 'true'
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.6.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.3.7
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: An API client for Lightcast in ruby.
81
+ test_files: []