From e9335ccc0ae9511280288e6fb85ff21cade3a93a Mon Sep 17 00:00:00 2001 From: Sean Walbran Date: Thu, 22 Sep 2016 09:39:22 -0500 Subject: [PATCH 1/4] remove dependency on active_resource --- lib/socialcast/command_line/cli.rb | 21 ++------ lib/socialcast/command_line/ldap_connector.rb | 4 +- lib/socialcast/command_line/message.rb | 54 +++++++++++++++---- lib/socialcast/command_line/provision_user.rb | 3 +- lib/socialcast/command_line/version.rb | 2 +- socialcast.gemspec | 2 +- 6 files changed, 52 insertions(+), 34 deletions(-) diff --git a/lib/socialcast/command_line/cli.rb b/lib/socialcast/command_line/cli.rb index 24123c8..b458ff1 100644 --- a/lib/socialcast/command_line/cli.rb +++ b/lib/socialcast/command_line/cli.rb @@ -10,20 +10,6 @@ require 'logger' require 'fileutils' -# uncomment to debug HTTP traffic -# class ActiveResource::Connection -# def configure_http(http) -# http = apply_ssl_options(http) -# # Net::HTTP timeouts default to 60 seconds. -# if @timeout -# http.open_timeout = @timeout -# http.read_timeout = @timeout -# end -# http.set_debug_output STDOUT -# http -# end -# end - module Socialcast module CommandLine class CLI < Thor @@ -109,10 +95,9 @@ def share(message = nil) end end - ActiveResource::Base.logger = Logger.new(STDOUT) if options[:trace] - Socialcast::CommandLine::Message.configure_from_credentials - Socialcast::CommandLine::Message.create :body => message, :url => options[:url], :message_type => options[:message_type], :attachment_ids => attachment_ids, :group_id => options[:group_id] - + Socialcast::CommandLine::Message.with_debug options[:trace] do + Socialcast::CommandLine::Message.create :body => message, :url => options[:url], :message_type => options[:message_type], :attachment_ids => attachment_ids, :group_id => options[:group_id] + end say "Message has been shared" end diff --git a/lib/socialcast/command_line/ldap_connector.rb b/lib/socialcast/command_line/ldap_connector.rb index 9f3fd6c..fab4dae 100644 --- a/lib/socialcast/command_line/ldap_connector.rb +++ b/lib/socialcast/command_line/ldap_connector.rb @@ -1,6 +1,6 @@ require 'net/ldap' -require 'active_support/core_ext/object/blank' -require 'active_support/core_ext/array/wrap' +require 'active_support' +require 'active_support/core_ext' module Socialcast module CommandLine diff --git a/lib/socialcast/command_line/message.rb b/lib/socialcast/command_line/message.rb index 82dfa8b..6a12609 100644 --- a/lib/socialcast/command_line/message.rb +++ b/lib/socialcast/command_line/message.rb @@ -1,17 +1,49 @@ -require 'active_resource' - -ActiveResource::Base.include_root_in_json = true - module Socialcast module CommandLine - class Message < ActiveResource::Base - headers['Accept'] = 'application/json' + class Message + class << self + attr_accessor :debug + + def create(attributes = {}) + options = { + :user => user, + :password => password, + } + RestClient.proxy = proxy if proxy + resource = RestClient::Resource.new create_url, options + attributes_json = { :message => attributes }.to_json + response = resource.post attributes_json, :accept => :json, :content_type => :json + puts "API response: #{response.body.to_s}" if debug + response + end + + def with_debug(new_value) + old_value = debug + self.debug = new_value + yield + ensure + self.debug = old_value + end + + def site + File.join('https://', Socialcast::CommandLine.credentials[:domain], 'api') + end + + def proxy + Socialcast::CommandLine.credentials[:proxy] if Socialcast::CommandLine.credentials[:proxy] + end + + def user + Socialcast::CommandLine.credentials[:user] + end + + def password + Socialcast::CommandLine.credentials[:password] + end - def self.configure_from_credentials - Socialcast::CommandLine::Message.site = ['https://', Socialcast::CommandLine.credentials[:domain], '/api'].join - Socialcast::CommandLine::Message.proxy = Socialcast::CommandLine.credentials[:proxy] if Socialcast::CommandLine.credentials[:proxy] - Socialcast::CommandLine::Message.user = Socialcast::CommandLine.credentials[:user] - Socialcast::CommandLine::Message.password = Socialcast::CommandLine.credentials[:password] + def create_url + File.join(site, 'messages.json') + end end end end diff --git a/lib/socialcast/command_line/provision_user.rb b/lib/socialcast/command_line/provision_user.rb index c18783c..6ca6adf 100644 --- a/lib/socialcast/command_line/provision_user.rb +++ b/lib/socialcast/command_line/provision_user.rb @@ -2,7 +2,8 @@ require 'builder' require 'set' require 'fileutils' -require 'active_support/core_ext/string/strip' +require 'active_support' +require 'active_support/core_ext' module Socialcast module CommandLine diff --git a/lib/socialcast/command_line/version.rb b/lib/socialcast/command_line/version.rb index b8cf96a..e824a4e 100644 --- a/lib/socialcast/command_line/version.rb +++ b/lib/socialcast/command_line/version.rb @@ -1,5 +1,5 @@ module Socialcast module CommandLine - VERSION = '1.3.20' + VERSION = '1.4.0' end end diff --git a/socialcast.gemspec b/socialcast.gemspec index 26faaf4..ee0b4c0 100644 --- a/socialcast.gemspec +++ b/socialcast.gemspec @@ -20,8 +20,8 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'thor', '~> 0.14', '>= 0.14.6' s.add_runtime_dependency 'highline', '~> 1.6', '>= 1.6.2' s.add_runtime_dependency 'socialcast-net-ldap', '~> 0.1', '>= 0.1.6' - s.add_runtime_dependency 'activeresource', '>= 4.0' s.add_runtime_dependency 'activesupport', '>= 4.0' + s.add_runtime_dependency 'builder', '~> 3.1' s.add_development_dependency 'rspec', '~> 3.3' s.add_development_dependency 'webmock', '~> 1.7', '>= 1.7.7' s.add_development_dependency 'rake', '0.9.2.2' From 956dacef2c5ca1b59314b9bfb8a73540be1a058a Mon Sep 17 00:00:00 2001 From: Sean Walbran Date: Thu, 22 Sep 2016 10:47:26 -0500 Subject: [PATCH 2/4] return a more backwards-compatible object from Message.create --- lib/socialcast/command_line/message.rb | 14 ++++++++-- spec/socialcast/command_line/cli_spec.rb | 33 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/socialcast/command_line/message.rb b/lib/socialcast/command_line/message.rb index 6a12609..ccf91ac 100644 --- a/lib/socialcast/command_line/message.rb +++ b/lib/socialcast/command_line/message.rb @@ -1,3 +1,6 @@ +require 'ostruct' +require 'json' + module Socialcast module CommandLine class Message @@ -13,8 +16,11 @@ def create(attributes = {}) resource = RestClient::Resource.new create_url, options attributes_json = { :message => attributes }.to_json response = resource.post attributes_json, :accept => :json, :content_type => :json - puts "API response: #{response.body.to_s}" if debug - response + response_body = response.body.to_s.presence + puts "API response: #{response_body}" if debug + + response_data = response_body ? JSON.parse(response_body) : {} + OpenStruct.new(response_data['message'] || {}) end def with_debug(new_value) @@ -44,6 +50,10 @@ def password def create_url File.join(site, 'messages.json') end + + def configure_from_credentials + # backwards-compatibility noop + end end end end diff --git a/spec/socialcast/command_line/cli_spec.rb b/spec/socialcast/command_line/cli_spec.rb index 07c3e7b..a95c6fd 100644 --- a/spec/socialcast/command_line/cli_spec.rb +++ b/spec/socialcast/command_line/cli_spec.rb @@ -121,6 +121,39 @@ end end + context 'with response data' do + before do + message_request_data = { + 'message' => { + 'body' => 'testing', + 'url' => nil, + 'message_type' => nil, + 'attachment_ids' => [], + 'group_id' => nil + } + } + message_response_data = { + 'message' => message_request_data['message'].merge( + 'id' => 123, + 'permalink_url' => 'https://test.stagings.socialcast.com/messages/123' + ) + } + stub_request(:post, "https://ryan%40socialcast.com:foo@test.staging.socialcast.com/api/messages.json") + .with(:body => message_request_data) + .with(:headers => {'Accept' => 'application/json'}) + .to_return(:status => 200, :body => message_response_data.to_json, :headers => {}) + end + it do + message_object = nil + expect(Socialcast::CommandLine::Message).to receive(:create).and_wrap_original do |method, *args| + message_object = method.call(*args) + end + Socialcast::CommandLine::CLI.start ['share', 'testing'] + expect(message_object.permalink_url).to eq 'https://test.stagings.socialcast.com/messages/123' + expect(message_object['permalink_url']).to eq 'https://test.stagings.socialcast.com/messages/123' + end + end + context 'with a message_type message' do before do stub_request(:post, "https://ryan%40socialcast.com:foo@test.staging.socialcast.com/api/messages.json"). From b605a3b1b7cf1d16823f71ef894b6b8cc50ec54e Mon Sep 17 00:00:00 2001 From: Sean Walbran Date: Thu, 22 Sep 2016 10:52:15 -0500 Subject: [PATCH 3/4] skip testing old rubies since activesupport 5 needs 2.2+ --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 26b67ce..48813ad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,4 @@ language: ruby rvm: - - 2.0.0 - - 2.1.6 - 2.2.4 - 2.3.1 From a4929dc36a3e0934b3b64758810b4c90daafd9eb Mon Sep 17 00:00:00 2001 From: Sean Walbran Date: Thu, 22 Sep 2016 11:18:18 -0500 Subject: [PATCH 4/4] remove unnecessary guard --- lib/socialcast/command_line/message.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/socialcast/command_line/message.rb b/lib/socialcast/command_line/message.rb index ccf91ac..7d8f800 100644 --- a/lib/socialcast/command_line/message.rb +++ b/lib/socialcast/command_line/message.rb @@ -36,7 +36,7 @@ def site end def proxy - Socialcast::CommandLine.credentials[:proxy] if Socialcast::CommandLine.credentials[:proxy] + Socialcast::CommandLine.credentials[:proxy] end def user