-
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.
Merge pull request #1 from zamN/master
Added JSON support
- Loading branch information
Showing
7 changed files
with
82 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module PublicIp | ||
module Service | ||
class Ipify < JsonType | ||
def self.uri | ||
URI('http://api.ipify.org/?format=json') | ||
end | ||
|
||
def self.parse_json(json_data) | ||
json_data['ip'] | ||
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 'json' | ||
|
||
module PublicIp | ||
module Service | ||
class JsonType < Simple | ||
extend PublicIp::Service::Registrable | ||
|
||
def self.parse_json(_json_data) | ||
fail 'Not implemented' | ||
end | ||
|
||
def self.ip | ||
response = perform_request | ||
|
||
parse_json(JSON.parse(response.body.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,13 @@ | ||
module PublicIp | ||
module Service | ||
class WtfIsMyIp < JsonType | ||
def self.uri | ||
URI('http://wtfismyip.com/json') | ||
end | ||
|
||
def self.parse_json(json_data) | ||
json_data['YourFuckingIPAddress'] | ||
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,33 @@ | ||
require 'spec_helper' | ||
|
||
module PublicIp | ||
module Service | ||
class JSONTypeService < JsonType | ||
def self.uri | ||
URI('http://www.example.com/json') | ||
end | ||
|
||
def self.parse_json(resp) | ||
resp['ExampleIP'] | ||
end | ||
|
||
def self.headers | ||
{ 'User-Agent' => 'rspec - testing public_ip via json response', 'Content-Type' => 'application/json' } | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe PublicIp::Service::JSONTypeService do | ||
describe '.ip' do | ||
before do | ||
stub_request(:get, /.*/) | ||
.with(headers: described_class.headers) | ||
.to_return(status: 200, body: "{\"ExampleIP\": \"127.0.0.1\"}\n", headers: {}) | ||
end | ||
|
||
it 'strips the response body, parses json, and returns the ip address' do | ||
expect(described_class.ip).to eq('127.0.0.1') | ||
end | ||
end | ||
end |