Skip to content

Commit 709bfdd

Browse files
authored
Merge pull request #50 from mconf/refactor-lti-response-logs
LTI-75 Replace 'debug' flag with a Logger
2 parents 91dc495 + ff50808 commit 709bfdd

File tree

6 files changed

+41
-34
lines changed

6 files changed

+41
-34
lines changed

features/config.yml.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
debug: false
1+
logger: nil
22

33
# maximum wait for a response to any API request (secs)
44
timeout_req: 60

features/step_definitions/common_steps.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@api = BigBlueButton::BigBlueButtonApi.new(@config_server['url'],
55
@config_server['secret'],
66
@config_server['version'].to_s,
7-
@config['debug'])
7+
@config['logger'])
88
@api.timeout = @config['timeout_req']
99
end
1010

lib/bigbluebutton_api.rb

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require 'bigbluebutton_modules'
1010
require 'bigbluebutton_config_xml'
1111
require 'bigbluebutton_config_layout'
12+
require 'logger'
1213

1314
module BigBlueButton
1415

@@ -50,8 +51,8 @@ class BigBlueButtonApi
5051
# API version e.g. 0.81
5152
attr_accessor :version
5253

53-
# Flag to turn on/off debug mode
54-
attr_accessor :debug
54+
# logger to log reponses and infos
55+
attr_accessor :logger
5556

5657
# Maximum wait time for HTTP requests (secs)
5758
attr_accessor :timeout
@@ -67,21 +68,25 @@ class BigBlueButtonApi
6768
# url:: URL to a BigBlueButton server (e.g. http://demo.bigbluebutton.org/bigbluebutton/api)
6869
# secret:: Shared secret for this server
6970
# version:: API version e.g. 0.81
70-
def initialize(url, secret, version=nil, debug=false)
71+
def initialize(url, secret, version=nil, logger=nil)
7172
@supported_versions = ['0.8', '0.81', '0.9', '1.0']
7273
@url = url
7374
@secret = secret
74-
@debug = debug
7575
@timeout = 10 # default timeout for api requests
7676
@request_headers = {} # http headers sent in all requests
77-
77+
@logger = logger
78+
if logger.nil?
79+
@logger = Logger.new(STDOUT)
80+
@logger.level = Logger::INFO
81+
end
82+
7883
version = nil if version && version.strip.empty?
7984
@version = nearest_version(version || get_api_version)
8085
unless @supported_versions.include?(@version)
81-
puts "BigBlueButtonAPI: detected unsupported version, using the closest one that is supported (#{@version})"
86+
@logger.warn("BigBlueButtonAPI: detected unsupported version, using the closest one that is supported (#{@version})")
8287
end
8388

84-
puts "BigBlueButtonAPI: Using version #{@version}" if @debug
89+
@logger.debug("BigBlueButtonAPI: Using version #{@version}")
8590
end
8691

8792
# Creates a new meeting. Returns the hash with the response or throws BigBlueButtonException
@@ -636,7 +641,7 @@ def check_url
636641
# API's are equal if all the following attributes are equal.
637642
def ==(other)
638643
r = true
639-
[:url, :supported_versions, :secret, :version, :debug].each do |param|
644+
[:url, :supported_versions, :secret, :version, :logger].each do |param|
640645
r = r && self.send(param) == other.send(param)
641646
end
642647
r
@@ -743,7 +748,7 @@ def send_api_request(method, params={}, data=nil, raw=false)
743748
# Otherwise uses GET
744749
def send_request(url, data=nil)
745750
begin
746-
puts "BigBlueButtonAPI: URL request = #{url}" if @debug
751+
@logger.debug("BigBlueButtonAPI: URL request = #{url}")
747752
url_parsed = URI.parse(url)
748753
http = Net::HTTP.new(url_parsed.host, url_parsed.port)
749754
http.open_timeout = @timeout
@@ -753,11 +758,12 @@ def send_request(url, data=nil)
753758
if data.nil?
754759
response = http.get(url_parsed.request_uri, @request_headers)
755760
else
756-
puts "BigBlueButtonAPI: Sending as a POST request with data.size = #{data.size}" if @debug
761+
@logger.debug("BigBlueButtonAPI: Sending as a POST request with data.size = #{data.size}")
757762
opts = { 'Content-Type' => 'application/xml' }.merge @request_headers
758763
response = http.post(url_parsed.request_uri, data, opts)
759764
end
760-
puts "BigBlueButtonAPI: URL response = #{response.body}" if @debug
765+
@logger.info("BigBlueButtonAPI: request=#{url} response_status=#{response.class.name} response_code=#{response.code} message_key=#{response.message}")
766+
@logger.debug("BigBlueButtonAPI: URL response = #{response.body}")
761767

762768
rescue Timeout::Error => error
763769
raise BigBlueButtonException.new("Timeout error. Your server is probably down: \"#{@url}\". Error: #{error}")

spec/bigbluebutton_api_0.81_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
let(:url) { "http://server.com" }
88
let(:secret) { "1234567890abcdefghijkl" }
99
let(:version) { "0.81" }
10-
let(:debug) { false }
11-
let(:api) { BigBlueButton::BigBlueButtonApi.new(url, secret, version, debug) }
10+
let(:api) { BigBlueButton::BigBlueButtonApi.new(url, secret, version) }
1211

1312
describe "#get_default_config_xml" do
1413
let(:response) { "<response><returncode>1</returncode></response>" }

spec/bigbluebutton_api_0.9_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
let(:url) { "http://server.com" }
88
let(:secret) { "1234567890abcdefghijkl" }
99
let(:version) { "0.9" }
10-
let(:debug) { false }
11-
let(:api) { BigBlueButton::BigBlueButtonApi.new(url, secret, version, debug) }
10+
let(:api) { BigBlueButton::BigBlueButtonApi.new(url, secret, version) }
1211

1312
describe "#create_meeting" do
1413
context "accepts the new parameters" do

spec/bigbluebutton_api_spec.rb

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
# default variables and API object for all tests
1010
let(:url) { "http://server.com" }
1111
let(:secret) { "1234567890abcdefghijkl" }
12-
let(:debug) { false }
13-
let(:api) { BigBlueButton::BigBlueButtonApi.new(url, secret, version, debug) }
12+
let(:logger) { Logger.new(STDOUT) }
13+
let(:api) { BigBlueButton::BigBlueButtonApi.new(url, secret, version, logger) }
14+
before { logger.level = Logger::INFO }
1415

1516
describe "#initialize" do
1617
context "standard initialization" do
17-
subject { BigBlueButton::BigBlueButtonApi.new(url, secret, version, debug) }
18+
subject { BigBlueButton::BigBlueButtonApi.new(url, secret, version, logger) }
1819
it { subject.url.should == url }
1920
it { subject.secret.should == secret }
2021
it { subject.version.should == version }
21-
it { subject.debug.should == debug }
22+
it { subject.logger.should == logger }
2223
it { subject.timeout.should == 10 }
2324
it { subject.supported_versions.should include("0.8") }
2425
it { subject.supported_versions.should include("0.81") }
@@ -350,14 +351,14 @@
350351
end
351352

352353
describe "#==" do
353-
let(:api2) { BigBlueButton::BigBlueButtonApi.new(url, secret, version, debug) }
354+
let(:api2) { BigBlueButton::BigBlueButtonApi.new(url, secret, version, logger) }
354355

355356
context "compares attributes" do
356357
it { api.should == api2 }
357358
end
358359

359-
context "differs #debug" do
360-
before { api2.debug = !api.debug }
360+
context "differs #logger" do
361+
before { api2.logger = !api.logger }
361362
it { api.should_not == api2 }
362363
end
363364

@@ -559,17 +560,19 @@
559560
describe "#send_request" do
560561
let(:url) { "http://test-server:8080/res?param1=value1&checksum=12345" }
561562
let(:url_parsed) { URI.parse(url) }
563+
let(:res) { Net::HTTPResponse.new(1.0, '200', 'OK') }
562564

563565
before do
564566
@http_mock = mock(Net::HTTP)
565567
@http_mock.should_receive(:"open_timeout=").with(api.timeout)
566568
@http_mock.should_receive(:"read_timeout=").with(api.timeout)
567569
Net::HTTP.should_receive(:new).with("test-server", 8080).and_return(@http_mock)
570+
res.stub(:body) { "ok" }
568571
end
569572

570573
context "standard case" do
571-
before { @http_mock.should_receive(:get).with("/res?param1=value1&checksum=12345", {}).and_return("ok") }
572-
it { api.send(:send_request, url).should == "ok" }
574+
before { @http_mock.should_receive(:get).with("/res?param1=value1&checksum=12345", {}).and_return(res) }
575+
it { api.send(:send_request, url).should == res }
573576
end
574577

575578
context "handles a TimeoutError" do
@@ -586,20 +589,20 @@
586589
let(:data) { "any data" }
587590
before {
588591
path = "/res?param1=value1&checksum=12345"
589-
opts = { 'Content-Type' => 'application/x-www-form-urlencoded' }
590-
@http_mock.should_receive(:post).with(path, data, opts).and_return("ok")
592+
opts = { 'Content-Type' => 'application/xml' }
593+
@http_mock.should_receive(:post).with(path, data, opts).and_return(res)
591594
}
592595
it {
593-
api.send(:send_request, url, data).should == "ok"
596+
api.send(:send_request, url, data).should == res
594597
}
595598
end
596599

597600
context "get with headers" do
598601
let(:headers_hash) { { :anything => "anything" } }
599-
before { @http_mock.should_receive(:get).with("/res?param1=value1&checksum=12345", headers_hash).and_return("ok") }
602+
before { @http_mock.should_receive(:get).with("/res?param1=value1&checksum=12345", headers_hash).and_return(res) }
600603
it {
601604
api.request_headers = headers_hash
602-
api.send(:send_request, url).should == "ok"
605+
api.send(:send_request, url).should == res
603606
}
604607
end
605608

@@ -608,12 +611,12 @@
608611
let(:data) { "any data" }
609612
before {
610613
path = "/res?param1=value1&checksum=12345"
611-
opts = { 'Content-Type' => 'application/x-www-form-urlencoded', :anything => "anything" }
612-
@http_mock.should_receive(:post).with(path, data, opts).and_return("ok")
614+
opts = { 'Content-Type' => 'application/xml', :anything => "anything" }
615+
@http_mock.should_receive(:post).with(path, data, opts).and_return(res)
613616
}
614617
it {
615618
api.request_headers = headers_hash
616-
api.send(:send_request, url, data).should == "ok"
619+
api.send(:send_request, url, data).should == res
617620
}
618621
end
619622
end

0 commit comments

Comments
 (0)