journalduhacker/extras/story_cacher.rb
joshua stein b641d0232d sort of merge cache branch but don't do anything automatically
allow manual caching of story text using diffbot, if an api key is
configured and Story#fetch_story_cache! is called
2013-06-30 00:54:01 -05:00

36 lines
805 B
Ruby

class StoryCacher
cattr_accessor :DIFFBOT_API_KEY
# this needs to be overridden in config/initializers/production.rb
@@DIFFBOT_API_KEY = nil
DIFFBOT_API_URL = "http://www.diffbot.com/api/article"
def self.get_story_text(url)
if !@@DIFFBOT_API_KEY
return
end
db_url = "#{DIFFBOT_API_URL}?token=#{@@DIFFBOT_API_KEY}&url=" <<
CGI.escape(url)
begin
s = Sponge.new
# we're not doing this interactively, so take a while
s.timeout = 20
res = s.fetch(db_url)
if res.present?
j = JSON.parse(res)
# turn newlines into double newlines, so they become paragraphs
return j["text"].gsub("\n", "\n\n")
end
rescue => e
Rails.logger.error "error fetching #{db_url}: #{e.message}"
end
nil
end
end