Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion lib/go_transit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -73,5 +74,11 @@ def configure
def base_url
custom_base_url || "https://api.openmetrolinx.com/OpenDataAPI/api"
end

def logger
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint/DuplicateMethods: Method GoTransit.logger is defined at both lib/go_transit.rb:68 and lib/go_transit.rb:78.

@logger ||= Logger.new($stdout).tap do |log|
log.progname = self.name
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

end
end
end
end
9 changes: 9 additions & 0 deletions lib/go_transit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/go_transit/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GoTransit
VERSION = "1.0.2".freeze
VERSION = "1.1.2".freeze
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

end
22 changes: 21 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,31 @@ 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
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
```
30 changes: 30 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,35 @@
}.to raise_error(GoTransit::NotFoundError)
end
end

context "when log response is enabled" do
Comment thread
jmazur marked this conversation as resolved.
it "logs a debug message" do
Comment thread
jmazur marked this conversation as resolved.
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")
Comment thread
jmazur marked this conversation as resolved.

expect(logger).to have_received(:debug).twice
end

it "logs the status code" do
Comment thread
jmazur marked this conversation as resolved.
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")
Comment thread
jmazur marked this conversation as resolved.

expect(logger).to have_received(:debug).with("[HTTP Response 200]").once
Comment thread
jmazur marked this conversation as resolved.
end
end
end
end