Get redirect of a URL in Ruby
You can use Net::Http and read the Location:
header from the response
require 'net/http'
require 'uri'
url = URI.parse('http://www.example.com/index.html')
res = Net::HTTP.start(url.host, url.port) {|http|
http.get('/index.html')
}
res['location']
Detect destination of shortened, or tiny url
The easiest way to get the destination of a shortened URL is with urllib
. Given that the short URL is valid (response code 200), the URL be returned to you.
>>> import urllib
>>> resp = urllib.urlopen('http://bit.ly/bcFOko')
>>> resp.getcode()
200
>>> resp.url
'http://mrdoob.com/lab/javascript/harmony/'
And that's that!
Ruby on rails get current location using http url
Sure, there are lots of services... For example:
http://ipinfodb.com/
https://www.telize.com/
or just google for "ip api"
I can recommend ipinfodb over telize. It is slower, but much more accurate (at least in my implementation).
Keep in mind, that when querying those APIs from your server you also share your quota with all other users on this server. So, let's say you are on heroku and you share one IP with multiple others, your quota limit will be reached soon. In this case, locate the user on the client-side.
Hope this helps
How check correct url protocol in ruby?
"Protocol"? As in the IP protocol used to connect to a host as defined by the URL?
require 'uri'
URI.parse('http://foo.com').scheme # => "http"
URI.parse('https://foo.com').scheme # => "https"
URI.parse('ftp://foo.com').scheme # => "ftp"
URI.parse('scp://foo.com').scheme # => "scp"
If you want to know whether a site accepts HTTPS vs. HTTP, I'd start by checking for HTTPS, as the majority of sites allow HTTP:
require 'net/http'
%w[
example.com
www.example.com
mail.google.com
account.dyn.com
].each do |url|
begin
Net::HTTP.start(url, 443, :use_ssl => true) {}
puts "#{url} is HTTPS"
rescue
puts "#{url} is HTTP"
end
end
# >> example.com is HTTP
# >> www.example.com is HTTP
# >> mail.google.com is HTTPS
# >> account.dyn.com is HTTPS
Even though mail.google.com and account.dyn.com are HTTPS, if you test them for HTTP first, you'll see they also have that protocol. Some sites will redirect their HTTP request to their HTTPS server, others run both to allow a user to decide whether they want HTTP or HTTPS. You can test both protocols to figure out which cases are true.
start
doesn't require a block, but by providing an empty one it will automatically close the connection immediately after establishing it.
Sites don't necessarily run their web services on ports 80 and 443. As a result, assuming the connection should be to one of those ports isn't necessarily right and could give you bad results if they use a different one. 8080 and 8081 are also often used so those should be checked too.
Also, a site might respond on a port, but its content could be a redirect pointing you to the real port they want you to use, so you need to also consider whether you should only care about the connection succeeding, or look inside the HTTPd headers, or actually read the entire page returned, and parse it in case it's a software redirect.
In other words, a connection succeeding doesn't tell you enough about what the site wants you to use, you'll have to conduct additional tests too.
Reading a webcal URL in ruby on rails
webcal is just an unofficial URI scheme for accessing icalendar documents. You can get the calendar using http and parse it using one of several icalendar plugins:
require 'net/http'
uri = URI('http://www.facebook.com/ical/b.php?uid=13301632&key=asdgagaweg')
calendar = Net::HTTP.get(uri)
For alternative ways to perform the http get request:
How make a HTTP request using Ruby on Rails?
Then parse the response using your favourite plug-in:
icalendar:
cal = icalendar.parse(calendar).first
vPim:
cal = Vpim::Icalendar.decode(calendar).first
ri_cal:
cal = RiCal.parse_string(calendar).first
Related Topics
How to Get the Destination Url of a Shortened Url Using Ruby
Ruby Copy a Paperclip Attachment from One Model to Another
What Are the Differences Between Lazy, Greedy and Possessive Quantifiers
Rails Helper - How to Get a Helper to Give Me a '<Br/>' (Or Other Markup)
Rails: Get Values of Http Response
Is There a Solution to Bypass 'Can't Add a New Key into Hash During Iteration (Runtimeerror)'
Drop-Down-Menu for Many-To-Many Relation in Rails Using Nested Attributes
In Ruby How to Automatically Populate Instance Variables Somehow in the Initialize Method
How to Call a Parent App's Helper Method from a Rails 3.1 Engine
How to Express 'Infinite Time'
Is Ruby's Stdlib Logger Class Thread-Safe
No Implicit Conversion from Nil to Integer - When Trying to Add Anything to Array
Problems Keeping an Object in an Array, Ruby Issues and Rails Issues
Troubles with Ruby-2.X Installation Using Rvm
Finding the Product of a Variable Number of Ruby Arrays
Where to Put a Before_Filter Shared Between Multiple Controllers