commit f630c0eb2aee31c4a211507668b790965ed98537 parent 5be0f54faa1e3fda58b340645d739527baad24e6 Author: Alex Balgavy <a.balgavy@gmail.com> Date: Mon, 5 Oct 2020 14:23:22 +0200 radio: get rid of rest-client, use open-uri Former-commit-id: ccd9c4ec32bb01dc8c910a4ab617ff1fdc442751 Diffstat:
M | scripts/radio | | | 18 | ++++++++++++------ |
1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/scripts/radio b/scripts/radio @@ -1,18 +1,24 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require 'json' -require 'rest-client' # TODO: get rid of this gem, just use net/http class Subreddit + require 'json' + require 'open-uri' + def initialize(name) @url = "https://www.reddit.com/r/#{name}/top.json?t=week&limit=100&show=all" - response = RestClient.get(@url) - data = JSON.parse(response)['data'] - @posts = data['children'] + begin + URI.open(@url, 'User-Agent' => 'ruby/2.7', 'Accept' => 'application/json') do |response| + data = JSON.parse(response.read)['data'] + @posts = data['children'] + end + rescue OpenURI::HTTPError + @posts = [] + end end def posts? - @posts.!empty? + !@posts.empty? end def play