-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Convert image links to shortened URLs #546
Changes from 7 commits
93e3a86
1be6bc6
8facb79
2c5e3e5
eb3c901
d43264a
abe568f
0185302
12d8de9
f38808c
d21f70e
99b7c21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class ShortenUrl | ||
|
||
SHORTEN_URL_LOG = 'log/tinyurl.log' | ||
SUCCESS_CODES = [200, 201, 202].freeze | ||
|
||
def self.short(url) | ||
response = HTTParty.get("http://tinyurl.com/api-create.php?url=#{url}") | ||
raise HTTParty::Error if response.match?(/error/i) || !SUCCESS_CODES.include?(response.code) | ||
response | ||
rescue HTTParty::Error => error | ||
ActivityLogger.open(SHORTEN_URL_LOG, 'TINYURL_API', 'shortening url', false) do |log| | ||
log.record('error', "Exception: #{error.message}") | ||
log.record('error', "Attempted URL: #{url}") | ||
log.record('error', "Response body: #{response.body}") | ||
log.record('error', "HTTP code: #{response.code}") | ||
end | ||
nil | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're raising a
I altered this code to:
I then called the method with:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, this approach allows us to capture more information for HTTParty-specific errors. For other exceptions, we could follow this with a second rescue clause, picking up other exceptions such as the one you have now. That way we rescue all exceptions, but we maximize the information we can get from HTTParty exceptions. |
||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddShortHBrandUrlToCompanies < ActiveRecord::Migration[5.1] | ||
def change | ||
add_column :companies, :short_h_brand_url, :string | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddShortProofOfMembershipUrlToUsers < ActiveRecord::Migration[5.1] | ||
def change | ||
add_column :users, :short_proof_of_membership_url, :string | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'rails_helper' | ||
|
||
describe ShortenUrl do | ||
describe '.short' do | ||
it 'creates shortened link' do | ||
VCR.use_cassette('shorten_url/short') do | ||
shortened_url = ShortenUrl.short 'http://sverigeshundforetagare.se/anvandare/2/proof_of_membership' | ||
expect(shortened_url).to match 'tinyurl.com' | ||
end | ||
end | ||
it 'if the service raises an error, returns nil and writes to the log' do | ||
VCR.use_cassette('shorten_url/short') do | ||
expect(ActivityLogger).to receive(:open) | ||
shortened_url = ShortenUrl.short '/' | ||
expect(shortened_url).to eq nil | ||
end | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more change requested (I missed this before) - we should handle the situation that occurs if
response
is nil.This is because
response
will benil
if HTTParty raises the exception. If we raise it, then it will not benil
.Something like: