-
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 4 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,16 @@ | ||
class ShortenUrl | ||
|
||
SHORTEN_URL_LOG = 'log/tinyurl.log' | ||
|
||
def self.short(url) | ||
response = HTTParty.get("http://tinyurl.com/api-create.php?url=#{url}") | ||
raise HTTParty::Error.new response if response.match? 'ERROR' | ||
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. I'm note sure we'll ever see a response that matches that string. You should check the response.code against "success" codes, and raise the exception if it is not. For example, I forced a failure by sending a bad URI:
In
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. Also, you don't need the 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 lost 'p' in 'php' before '?'. The two only errors I know of look like this.
Second one I found out about a moment ago. I will change match to 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. OK, that will work. What I was trying to get at is the situation where the website is down and/or the request times out - in that case we won't get an "Error" from the site, but we should get a response.code other than So, maybe we should check for response matching |
||
response | ||
rescue HTTParty::Error => error | ||
ActivityLogger.open(SHORTEN_URL_LOG, 'TINYURL_API', 'shortening url', false) do |log| | ||
log.record('error', error.message) | ||
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 @@ | ||
TINYURL_LOG = 'log/shorten_url.log' | ||
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. This is not used anymore(?) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddShortBrandHUrlAndShortProofOfMembershipUrlToCompanies < ActiveRecord::Migration[5.1] | ||
def change | ||
add_column :companies, :short_h_brand_url, :string | ||
add_column :companies, :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 |
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.
Lint/UnusedMethodArgument: Unused method argument - user. If it's necessary, use _ or _user as an argument name to indicate that it won't be used. You can also write as get_or_create_short_h_brand_url(*) if you want the method to accept any arguments but don't care about them.