diff --git a/Gemfile.lock b/Gemfile.lock index 08fb1b5..8615924 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,20 +1,20 @@ PATH remote: . specs: - go_transit (1.0.2) + go_transit (1.1.2) activesupport GEM remote: https://rubygems.org/ specs: - activesupport (8.0.2) + activesupport (8.1.2) base64 - benchmark (>= 0.3) bigdecimal concurrent-ruby (~> 1.0, >= 1.3.1) connection_pool (>= 2.2.5) drb i18n (>= 1.6, < 2) + json logger (>= 1.4.2) minitest (>= 5.1) securerandom (>= 0.3) @@ -23,26 +23,27 @@ GEM addressable (2.8.7) public_suffix (>= 2.0.2, < 7.0) base64 (0.3.0) - benchmark (0.4.0) bigdecimal (3.1.9) coderay (1.1.3) - concurrent-ruby (1.3.5) - connection_pool (2.5.0) + concurrent-ruby (1.3.6) + connection_pool (3.0.2) crack (1.0.0) bigdecimal rexml diff-lcs (1.6.0) docile (1.4.1) - drb (2.2.1) + drb (2.2.3) hashdiff (1.1.2) - i18n (1.14.7) + i18n (1.14.8) concurrent-ruby (~> 1.0) json (2.10.2) logger (1.6.6) method_source (1.1.0) - minitest (5.25.5) + minitest (6.0.1) + prism (~> 1.5) mustermann (3.0.3) ruby2_keywords (~> 0.0.1) + prism (1.9.0) pry (0.15.2) coderay (~> 1.1) method_source (~> 1.0) @@ -88,7 +89,7 @@ GEM timecop (0.9.10) tzinfo (2.0.6) concurrent-ruby (~> 1.0) - uri (1.0.3) + uri (1.1.1) webmock (3.25.1) addressable (>= 2.8.0) crack (>= 0.3.2) diff --git a/lib/go_transit.rb b/lib/go_transit.rb index 60a720a..368b34f 100644 --- a/lib/go_transit.rb +++ b/lib/go_transit.rb @@ -62,9 +62,10 @@ module GoTransit @api_key = "" + @log_response = false class << self - attr_accessor :api_key, :custom_base_url + attr_accessor :api_key, :custom_base_url, :log_response, :logger def configure yield self @@ -73,5 +74,11 @@ def configure def base_url custom_base_url || "https://api.openmetrolinx.com/OpenDataAPI/api" end + + def logger + @logger ||= Logger.new($stdout).tap do |log| + log.progname = self.name + end + end end end diff --git a/lib/go_transit/client.rb b/lib/go_transit/client.rb index 2ac306c..35b4134 100644 --- a/lib/go_transit/client.rb +++ b/lib/go_transit/client.rb @@ -9,8 +9,17 @@ def get(path) uri = URI("#{GoTransit.base_url}/#{API_VERSION}/#{path}"\ "?key=#{GoTransit.api_key}") response = Net::HTTP.get_response(uri) + log response json = JSON.parse(response.body) Response.new(json) end + + private + + def log(response) + return unless GoTransit.log_response + GoTransit.logger.debug "[HTTP Response #{response.code}]" + GoTransit.logger.debug response.body + end end end diff --git a/lib/go_transit/version.rb b/lib/go_transit/version.rb index fb48389..043e9c3 100644 --- a/lib/go_transit/version.rb +++ b/lib/go_transit/version.rb @@ -1,3 +1,3 @@ module GoTransit - VERSION = "1.0.2".freeze + VERSION = "1.1.2".freeze end diff --git a/readme.md b/readme.md index d12c05e..80bbfce 100644 --- a/readme.md +++ b/readme.md @@ -88,7 +88,8 @@ At the time of development I was unable to get test data for the following endpo * `GET api/V1/Fleet/Consist/All` - 403 Forbidden * `GET api/V1/Fleet/Consist/Engine/{EngineNumber}` - 403 Forbidden -## Changing the API base url +## Additional Configuration +### Changing the API base url In some cases you may want to change the base go transit API url. You can use the `custom_base_url` config to set one: ```ruby @@ -96,3 +97,22 @@ GoTransit.configure do |config| config.custom_base_url = "https://example.com" end ``` + +### Logger +Set your own logger. Defaults to stdout. + +```ruby +GoTransit.configure do |config| + config.logger = Rails.logger +end +``` + +### Response Logging +Getting an unexpected response? Use the response logging to debug the HTTP +response code and body. Default to false. Outputs to debug log. + +```ruby +GoTransit.configure do |config| + config.log_response = true +end +``` diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 69216c8..ab6d2db 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -63,5 +63,35 @@ }.to raise_error(GoTransit::NotFoundError) end end + + context "when log response is enabled" do + it "logs a debug message" do + logger = Logger.new(@stdout) + allow(logger).to receive(:debug) + GoTransit.configure do |config| + config.log_response = true + config.logger = logger + end + client = GoTransit::Client.new + + client.get("Stop/Details/UN") + + expect(logger).to have_received(:debug).twice + end + + it "logs the status code" do + logger = Logger.new(@stdout) + allow(logger).to receive(:debug) + GoTransit.configure do |config| + config.log_response = true + config.logger = logger + end + client = GoTransit::Client.new + + client.get("Stop/Details/UN") + + expect(logger).to have_received(:debug).with("[HTTP Response 200]").once + end + end end end