-
Notifications
You must be signed in to change notification settings - Fork 14
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: Expand urls in tweets #43
Open
bf4
wants to merge
3
commits into
rubyrogues:master
Choose a base branch
from
bf4:expand-urls-in-tweets
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'net/http' | ||
require 'net/https' | ||
require 'uri' | ||
|
||
# e.g. | ||
# url = http://t.co/z4t0E1vArh | ||
# ExpandUrl.expand_url(url) | ||
# => "http://www.haaretz.com/news/national/israel-s-ag-impels-ministers-to-crack-down-on-exclusion-of-women.premium-1.519917" | ||
module ExpandUrl | ||
class ExpansionError < StandardError; end | ||
module ExpansionErrors | ||
class BadUrl < ExpansionError; end | ||
class BadResponse < ExpansionError; end | ||
end | ||
HTTP_ERRORS = [Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, | ||
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError] | ||
class BasicResponse < Struct.new(:url, :code); end | ||
extend self | ||
|
||
# raises ExpandUrl::ExpansionError | ||
def expand_url(url) | ||
response = get_response(url) | ||
case response.code | ||
when '301' | ||
log "url: #{url}\tresponse: #{response.inspect}" | ||
expand_url(response['location']) | ||
when '200' | ||
log "url: #{url}\tresponse: #{response.inspect}" | ||
url | ||
else | ||
log "url: #{url}\tresponse: #{response.inspect}" | ||
expand_url(response['location']) | ||
end | ||
end | ||
|
||
def get_response(url) | ||
uri = url_to_uri(url) | ||
Net::HTTP.get_response(uri) | ||
rescue EOFError => e | ||
BasicResponse.new(url, '200') | ||
rescue *HTTP_ERRORS => e | ||
log url.inspect + e.inspect | ||
raise ExpansionErrors::BadResponse.new(e) | ||
end | ||
|
||
def url_to_uri(url) | ||
begin | ||
uri = URI.parse(url) | ||
rescue URI::InvalidURIError, SocketError => e | ||
raise ExpansionErrors::BadUrl.new(e) | ||
end | ||
end | ||
|
||
def log(msg) | ||
STDOUT.puts "#{msg}\t#{caller[1]}" if (ENV['debug'] == 'true') | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'spec_helper' | ||
|
||
describe ExpandUrl do | ||
|
||
it 'should be tested' | ||
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍 Pretty please?
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.
sure. I was thinking of also moving the expansion to the tweet processing leve, but since that would be destructive, was considering adding an 'original tweet text' field. Unrelated, I want to add a 'context' field that represents the search term the tweet was found from, then... eventually extract most of this as an engine.. :)
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.
I'm rewriting this to add a tweet_display_text field that is populated at import time.. should I continue to update this pull request?
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.
Just make it easy to merge and I'll do so.. Keep adding tests and I don't mind merging at a faster rate, given the feature is not going to bloat the code base -- aka, it is derived from something needed, with a reason that outweighs the expense of maintenance moving forward..