Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Remove activeresource dependency #56

Merged
merged 4 commits into from Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
@@ -1,6 +1,4 @@
language: ruby
rvm:
- 2.0.0
- 2.1.6
- 2.2.4
- 2.3.1
21 changes: 3 additions & 18 deletions lib/socialcast/command_line/cli.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions 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
Expand Down
64 changes: 53 additions & 11 deletions lib/socialcast/command_line/message.rb
@@ -1,17 +1,59 @@
require 'active_resource'

ActiveResource::Base.include_root_in_json = true
require 'ostruct'
require 'json'

module Socialcast
module CommandLine
class Message < ActiveResource::Base
headers['Accept'] = 'application/json'

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]
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
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)
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]
end

def user
Socialcast::CommandLine.credentials[:user]
end

def password
Socialcast::CommandLine.credentials[:password]
end

def create_url
File.join(site, 'messages.json')
end

def configure_from_credentials
# backwards-compatibility noop
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/socialcast/command_line/provision_user.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/socialcast/command_line/version.rb
@@ -1,5 +1,5 @@
module Socialcast
module CommandLine
VERSION = '1.3.20'
VERSION = '1.4.0'
end
end
2 changes: 1 addition & 1 deletion socialcast.gemspec
Expand Up @@ -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'
Expand Down
33 changes: 33 additions & 0 deletions spec/socialcast/command_line/cli_spec.rb
Expand Up @@ -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").
Expand Down