From d82dbd565bad213f9df026a9b3ed2300bd81fdd9 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 16 Jun 2017 09:08:15 +0900 Subject: [PATCH] FIX: Remove dependency on `rest-client`. --- Gemfile | 1 - Gemfile.lock | 10 ---------- app/jobs/regular/push_notification.rb | 6 ++++-- lib/discourse_hub.rb | 15 +++++++++++---- script/import_scripts/quandora/quandora_api.rb | 7 +++---- .../socialcast/socialcast_api.rb | 3 +-- spec/components/discourse_hub_spec.rb | 5 ++++- spec/services/post_alerter_spec.rb | 18 ++++++++---------- spec/services/user_destroyer_spec.rb | 5 ----- .../lib/discourse_imgur/imgur.rb | 9 +++------ 10 files changed, 34 insertions(+), 45 deletions(-) diff --git a/Gemfile b/Gemfile index 53d34033ff6..17e75538c57 100644 --- a/Gemfile +++ b/Gemfile @@ -95,7 +95,6 @@ gem 'r2', '~> 0.2.5', require: false gem 'rake' gem 'thor', require: false -gem 'rest-client' gem 'rinku' gem 'sanitize' gem 'sidekiq' diff --git a/Gemfile.lock b/Gemfile.lock index 3faa8c8a02e..16b9e2b326b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -86,8 +86,6 @@ GEM image_size (~> 1.5) in_threads (~> 1.3) progress (~> 3.0, >= 3.0.1) - domain_name (0.5.20170404) - unf (>= 0.0.5, < 1.0.0) email_reply_trimmer (0.1.6) ember-data-source (2.2.1) ember-source (>= 1.8, < 3.0) @@ -130,8 +128,6 @@ GEM highline (1.7.8) hiredis (0.6.1) htmlentities (4.3.4) - http-cookie (1.0.3) - domain_name (~> 0.5) http_accept_language (2.0.5) i18n (0.8.4) image_size (1.5.0) @@ -174,7 +170,6 @@ GEM multi_xml (0.6.0) multipart-post (2.0.0) mustache (1.0.5) - netrc (0.11.0) nokogiri (1.8.0) mini_portile2 (~> 2.2.0) nokogumbo (1.4.13) @@ -289,10 +284,6 @@ GEM redis (3.3.3) redis-namespace (1.5.3) redis (~> 3.0, >= 3.0.4) - rest-client (1.8.0) - http-cookie (>= 1.0.2, < 2.0) - mime-types (>= 1.16, < 3.0) - netrc (~> 0.7) rinku (2.0.2) rmmseg-cpp (0.2.9) rspec (3.6.0) @@ -467,7 +458,6 @@ DEPENDENCIES rbtrace redis redis-namespace - rest-client rinku rmmseg-cpp rspec diff --git a/app/jobs/regular/push_notification.rb b/app/jobs/regular/push_notification.rb index 651a7486bbe..ac924a4533a 100644 --- a/app/jobs/regular/push_notification.rb +++ b/app/jobs/regular/push_notification.rb @@ -20,8 +20,10 @@ module Jobs }) end - RestClient.send :post, push_url, payload.merge({notifications: notifications}).to_json, content_type: :json, accept: :json - + Excon.post(push_url, + body: payload.merge(notifications: notifications).to_json, + headers: { 'Content-Type' => 'application/json', 'Accept' => 'applicaton/json' } + ) end end diff --git a/lib/discourse_hub.rb b/lib/discourse_hub.rb index dc97b1748d6..a10f3a1b65b 100644 --- a/lib/discourse_hub.rb +++ b/lib/discourse_hub.rb @@ -1,4 +1,3 @@ -require_dependency 'rest_client' require_dependency 'version' module DiscourseHub @@ -41,11 +40,19 @@ module DiscourseHub end def self.singular_action(action, rel_url, params={}) - JSON.parse RestClient.send(action, "#{hub_base_url}#{rel_url}", {params: params, accept: accepts, referer: referer } ) + JSON.parse(Excon.send(action, + "#{hub_base_url}#{rel_url}", + body: params.to_query, + headers: { 'Referer' => referer, 'Accept' => accepts.join(', ') } + ).body) end def self.collection_action(action, rel_url, params={}) - JSON.parse RestClient.send(action, "#{hub_base_url}#{rel_url}", params, content_type: :json, accept: accepts, referer: referer ) + JSON.parse(Excon.send(action, + "#{hub_base_url}#{rel_url}", + body: params, + headers: { 'Referer' => referer, 'Accept' => accepts.join(', '), 'Content-Type' => 'applicaton/json' } + ).body) end def self.hub_base_url @@ -57,7 +64,7 @@ module DiscourseHub end def self.accepts - [:json, 'application/vnd.discoursehub.v1'] + ['application/json', 'application/vnd.discoursehub.v1'] end def self.referer diff --git a/script/import_scripts/quandora/quandora_api.rb b/script/import_scripts/quandora/quandora_api.rb index 37ef726f1f4..c559d8a710f 100644 --- a/script/import_scripts/quandora/quandora_api.rb +++ b/script/import_scripts/quandora/quandora_api.rb @@ -1,6 +1,5 @@ require 'base64' require 'json' -require 'rest-client' class QuandoraApi @@ -31,10 +30,10 @@ class QuandoraApi url end - def request url - JSON.parse(RestClient.get url, auth_header(@username, @password)) + def request(url) + JSON.parse(Excon.get(url, headers: auth_header(@username, @password))) end - + def list_bases response = request list_bases_url response['data'] diff --git a/script/import_scripts/socialcast/socialcast_api.rb b/script/import_scripts/socialcast/socialcast_api.rb index ab27546a484..2c9399b1227 100644 --- a/script/import_scripts/socialcast/socialcast_api.rb +++ b/script/import_scripts/socialcast/socialcast_api.rb @@ -1,6 +1,5 @@ require 'base64' require 'json' -require 'rest-client' class SocialcastApi @@ -22,7 +21,7 @@ class SocialcastApi end def request url - JSON.parse(RestClient.get url, headers) + JSON.parse(Excon.get(url, headers: headers) end def list_users(opts={}) diff --git a/spec/components/discourse_hub_spec.rb b/spec/components/discourse_hub_spec.rb index 29699ebeec2..8999d264be1 100644 --- a/spec/components/discourse_hub_spec.rb +++ b/spec/components/discourse_hub_spec.rb @@ -5,7 +5,10 @@ describe DiscourseHub do describe '.discourse_version_check' do it 'should return just return the json that the hub returns' do hub_response = {'success' => 'OK', 'latest_version' => '0.8.1', 'critical_updates' => false} - RestClient.stubs(:get).returns( hub_response.to_json ) + + stub_request(:get, "http://local.hub:3000/api/version_check"). + to_return(status: 200, body: hub_response.to_json) + expect(DiscourseHub.discourse_version_check).to eq(hub_response) end end diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb index c53e47977e3..1a8103a4d1b 100644 --- a/spec/services/post_alerter_spec.rb +++ b/spec/services/post_alerter_spec.rb @@ -329,7 +329,7 @@ describe PostAlerter do let(:topic) { mention_post.topic } it "pushes nothing to suspended users" do - + SiteSetting.queue_jobs = true SiteSetting.allowed_user_api_push_urls = "https://site.com/push|https://site2.com/push" evil_trout.update_columns(suspended_till: 1.year.from_now) @@ -343,8 +343,7 @@ describe PostAlerter do push_url: "https://site2.com/push") end - RestClient.expects(:post).never - mention_post + expect { mention_post }.to_not change { Jobs::PushNotification.jobs.count } end it "correctly pushes notifications if configured correctly" do @@ -362,14 +361,11 @@ describe PostAlerter do body = nil headers = nil - # should only happen once even though we are using 2 keys - RestClient.expects(:post).with{|_req,_body,_headers| - headers = _headers - body = _body + Excon.expects(:post).with{|_req, _body| + headers = _body[:headers] + body = _body[:body] }.returns("OK") - mention_post - payload = { "secret_key" => SiteSetting.push_api_secret_key, "url" => Discourse.base_url, @@ -399,8 +395,10 @@ describe PostAlerter do ] } + mention_post + expect(JSON.parse(body)).to eq(payload) - expect(headers[:content_type]).to eq(:json) + expect(headers["Content-Type"]).to eq('application/json') end end diff --git a/spec/services/user_destroyer_spec.rb b/spec/services/user_destroyer_spec.rb index 55b94ff3fbd..4d0a23043b8 100644 --- a/spec/services/user_destroyer_spec.rb +++ b/spec/services/user_destroyer_spec.rb @@ -2,11 +2,6 @@ require 'rails_helper' require_dependency 'user_destroyer' describe UserDestroyer do - - before do - RestClient.stubs(:delete).returns( {success: 'OK'}.to_json ) - end - describe 'new' do it 'raises an error when user is nil' do expect { UserDestroyer.new(nil) }.to raise_error(Discourse::InvalidParameters) diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/imgur.rb b/vendor/gems/discourse_imgur/lib/discourse_imgur/imgur.rb index 1e4f084a059..c7e03acee4a 100644 --- a/vendor/gems/discourse_imgur/lib/discourse_imgur/imgur.rb +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/imgur.rb @@ -1,5 +1,3 @@ -require 'rest_client' - # /!\ WARNING /!\ # This plugin has been extracted from the Discourse source code and has not been tested. # It really needs some love <3 @@ -16,10 +14,9 @@ module Imgur blob = file.read - response = RestClient.post( - SiteSetting.imgur_endpoint, - { image: Base64.encode64(blob) }, - { 'Authorization' => "ClientID #{SiteSetting.imgur_client_id}" } + response = Excon.post(SiteSetting.imgur_endpoint, + body: { image: Base64.encode64(blob) }, + headers: { 'Authorization' => "ClientID #{SiteSetting.imgur_client_id}" } ) json = JSON.parse(response.body)['data'] rescue nil