forked from thesuss/shf-project
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Convert image links to shortened URLs (#546)
* tinyurl * forgot to add in last * shorten_url service, added columns * fixed ShortenUrl, company and user #short...url methods * fixes in models, extended error logs * ShortenUrl and spec fixes * cognitive complexity * back to the helpers * just a little change
- Loading branch information
1 parent
95e8572
commit 70b41fe
Showing
22 changed files
with
264 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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 | ||
log_error(error) | ||
nil | ||
end | ||
|
||
private_class_method | ||
|
||
def self.log_error(error) | ||
ActivityLogger.open(SHORTEN_URL_LOG, 'TINYURL_API', 'shortening url', false) do |log| | ||
log.record('error', "Exception: #{error.message}") | ||
if response | ||
log.record('error', "Attempted URL: #{url}") | ||
log.record('error', "Response body: #{response.body}") | ||
log.record('error', "HTTP code: #{response.code}") | ||
else | ||
log.record('error', "Exception raised by HTTParty") | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
db/migrate/20180717043851_add_short_h_brand_url_to_companies.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
5 changes: 5 additions & 0 deletions
5
db/migrate/20180719021503_add_short_proof_of_membership_url_to_users.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ya6sa84h' | ||
end | ||
end | ||
it 'if the service raises an error, returns nil and writes to the log' do | ||
VCR.use_cassette('shorten_url/error') 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.
Oops, something went wrong.
Oops, something went wrong.