Skip to content

Commit

Permalink
Add support for html services and add 3 more services
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrocarrico committed Jan 5, 2016
1 parent cea561f commit 5ffd79e
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 0 deletions.
4 changes: 4 additions & 0 deletions features/public_ip.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ Feature: PublicIp CLI application
i_can_haz_ip (http://icanhazip.com)
ident_me (http://ident.me)
ifconfig_me (http://ifconfig.me/ip)
ip_chicken (http://www.ipchicken.com/)
ip_echo (http://ipecho.net/plain)
ip_info (http://ipinfo.io/ip)
ip_ogre (http://ipogre.com)
mx_toolbox (http://mxtoolbox.com/WhatIsMyIP/)
private_internet_access (https://www.privateinternetaccess.com/pages/whats-my-ip/)
smart_ip (http://smart-ip.net/myip)
what_is_my_ip (https://www.whatismyip.com/)
what_is_my_ip_address (http://bot.whatismyipaddress.com)
You may pick a service and run public_ip [service] to get your IP address from that service
"""
Expand Down
1 change: 1 addition & 0 deletions lib/public_ip.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'public_ip/service/registry'
require 'public_ip/service/registrable'
require 'public_ip/service/simple'
require 'public_ip/service/parsed_html'
require 'public_ip/service/plain'
require 'public_ip/service/matched_expression'

Expand Down
16 changes: 16 additions & 0 deletions lib/public_ip/service/ip_chicken.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'nokogiri'

module PublicIp
module Service
class IpChicken < ParsedHTML
def self.uri
URI('http://www.ipchicken.com/')
end

def self.parse_ip_address(response_body)
parsed_html = Nokogiri::HTML(response_body).css('table:nth-of-type(2) p:nth-of-type(2) b')
parsed_html.text.match(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}/)[0]
end
end
end
end
15 changes: 15 additions & 0 deletions lib/public_ip/service/mx_toolbox.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'nokogiri'

module PublicIp
module Service
class MxToolbox < ParsedHTML
def self.uri
URI('http://mxtoolbox.com/WhatIsMyIP/')
end

def self.parse_ip_address(response_body)
Nokogiri::HTML(response_body).css('#ctl00_ContentPlaceHolder1_hlIP').text
end
end
end
end
13 changes: 13 additions & 0 deletions lib/public_ip/service/parsed_html.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module PublicIp
module Service
class ParsedHTML < Simple
extend PublicIp::Service::Registrable

def self.ip
response = perform_request

parse_ip_address(response.body)
end
end
end
end
15 changes: 15 additions & 0 deletions lib/public_ip/service/private_internet_access.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'nokogiri'

module PublicIp
module Service
class PrivateInternetAccess < ParsedHTML
def self.uri
URI('https://www.privateinternetaccess.com/pages/whats-my-ip/')
end

def self.parse_ip_address(response_body)
Nokogiri::HTML(response_body).css('.ipbox-footer ul li:first-of-type span').text.strip
end
end
end
end
19 changes: 19 additions & 0 deletions lib/public_ip/service/what_is_my_ip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'nokogiri'

module PublicIp
module Service
class WhatIsMyIp < ParsedHTML
def self.uri
URI('https://www.whatismyip.com/')
end

def self.headers
{ 'User-Agent' => 'Chrome' }
end

def self.parse_ip_address(response_body)
Nokogiri::HTML(response_body).css('.ip div').text
end
end
end
end
1 change: 1 addition & 0 deletions public_ip.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ DESCRIPTION
spec.require_paths = ['lib']

spec.add_dependency 'methadone', '~> 1.9.2'
spec.add_dependency 'nokogiri', '~> 1.6.7'

spec.add_development_dependency 'bundler', '~> 1.10'
spec.add_development_dependency 'rake', '~> 10.0'
Expand Down
35 changes: 35 additions & 0 deletions spec/provider/parsed_html_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'spec_helper'

require 'nokogiri'

module PublicIp
module Service
class ParsedHTMLService < ParsedHTML
def self.uri
URI('http://www.example.com')
end

def self.headers
{ 'User-Agent' => 'rspec - testing public_ip' }
end

def self.parse_ip_address(response_body)
Nokogiri::HTML(response_body).css('.ip').text
end
end
end
end

describe PublicIp::Service::ParsedHTMLService do
describe '.ip' do
before do
stub_request(:get, /.*/)
.with(headers: { 'User-Agent' => 'rspec - testing public_ip' })
.to_return(status: 200, body: '<html><body><div class="ip">127.0.0.1</div></body></html>', headers: {})
end

it 'parses the html in the response body and returns the ip address' do
expect(described_class.ip).to eq('127.0.0.1')
end
end
end

0 comments on commit 5ffd79e

Please sign in to comment.