2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-24 01:50:20 -04:00
|
|
|
require 'excon'
|
|
|
|
|
|
|
|
module DiscourseNarrativeBot
|
|
|
|
class QuoteGenerator
|
|
|
|
API_ENDPOINT = 'http://api.forismatic.com/api/1.0/'.freeze
|
|
|
|
|
2019-10-16 13:40:05 -04:00
|
|
|
def self.format_quote(quote, author)
|
|
|
|
I18n.t('discourse_narrative_bot.quote.results', quote: quote, author: author)
|
|
|
|
end
|
|
|
|
|
2017-05-24 01:50:20 -04:00
|
|
|
def self.generate(user)
|
|
|
|
quote, author =
|
2019-05-15 17:43:00 -04:00
|
|
|
if !user.effective_locale.start_with?('en')
|
2017-05-24 01:50:20 -04:00
|
|
|
translation_key = "discourse_narrative_bot.quote.#{rand(1..10)}"
|
|
|
|
|
|
|
|
[
|
|
|
|
I18n.t("#{translation_key}.quote"),
|
|
|
|
I18n.t("#{translation_key}.author")
|
|
|
|
]
|
|
|
|
else
|
|
|
|
connection = Excon.new("#{API_ENDPOINT}?lang=en&format=json&method=getQuote")
|
|
|
|
response = connection.request(expects: [200, 201], method: :Get)
|
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
[response_body["quoteText"].strip, response_body["quoteAuthor"].strip]
|
|
|
|
end
|
|
|
|
|
2019-10-16 13:40:05 -04:00
|
|
|
format_quote(quote, author)
|
2017-05-24 01:50:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|