-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for html services and add 3 more services
- Loading branch information
1 parent
cea561f
commit 5ffd79e
Showing
9 changed files
with
119 additions
and
0 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
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 |
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,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 |
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,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 |
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,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 |
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 '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 |
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,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 |