-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add small Spamfilter service wrapping Nyckel API
- Loading branch information
1 parent
86a9433
commit 278644f
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
class SpamfilterService | ||
class NyckelError < StandardError; end | ||
class NotAuthorizedError < NyckelError; end | ||
Result = Struct.new(:labelName, :confidence) do | ||
def spam? | ||
labelName == 'Spam' | ||
end | ||
|
||
def ham? | ||
labelName == 'Ham' | ||
end | ||
end | ||
|
||
NYCKEL_URL = 'https://www.nyckel.com' | ||
NYCKEL_FUNCTION = ENV.fetch('NYCKEL_FUNCTION', nil) | ||
|
||
def self.call(account_age_hours:, content:) | ||
Retriable.retriable( | ||
on_retry: -> { refresh_token! }, | ||
on: [NotAuthorizedError] | ||
) do | ||
response = HTTP.auth("Bearer #{access_token}").post( | ||
"#{NYCKEL_URL}/v1/functions/#{NYCKEL_FUNCTION}/invoke", | ||
json: { data: { account_age_hours:, content: } } | ||
) | ||
body = JSON.parse(response.body.to_s) | ||
|
||
raise NotAuthorizedError if response.code == 401 | ||
raise NyckelError, body.message if response.code != 200 | ||
|
||
Result.new(body['labelName'], body['confidence']) | ||
end | ||
end | ||
|
||
def self.access_token | ||
refresh_token! if @access_token.nil? || @access_token.expired? | ||
|
||
@access_token.token | ||
end | ||
|
||
def self.refresh_token! | ||
@access_token = oauth2.get_token | ||
end | ||
|
||
def self.oauth2 | ||
OAuth2::Client.new( | ||
ENV.fetch('NYCKEL_CLIENT_ID', nil), | ||
ENV.fetch('NYCKEL_CLIENT_SECRET', nil), | ||
site: NYCKEL_URL, | ||
token_url: '/connect/token' | ||
).client_credentials | ||
end | ||
end |