Skip to content

Commit d764db8

Browse files
authored
Merge pull request #58 from ipinfo/silvano/eng-641-add-resproxy-support-in-ipinforuby-library
Add Residential Proxy API support
2 parents 7fc965a + 0bd8f83 commit d764db8

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

lib/ipinfo.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ def details_v6(ip_address = nil)
5454
details_base(ip_address, :v6)
5555
end
5656

57+
def resproxy(ip_address)
58+
cache_key_str = "resproxy:#{ip_address}"
59+
res = @cache.get(cache_key(cache_key_str))
60+
return Response.new(res) unless res.nil?
61+
62+
response = @httpc.get("/resproxy/#{CGI.escape(ip_address)}", :v4)
63+
64+
if response.status.eql?(429)
65+
raise RateLimitError,
66+
RATE_LIMIT_MESSAGE
67+
end
68+
69+
details = JSON.parse(response.body, symbolize_names: true)
70+
@cache.set(cache_key(cache_key_str), details)
71+
Response.new(details)
72+
end
73+
5774
def get_map_url(ips)
5875
if !ips.kind_of?(Array)
5976
return JSON.generate({:error => 'Invalid input. Array required!'})

test/ipinfo_test.rb

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
class IPinfoTest < Minitest::Test
66
TEST_IPV4 = '8.8.8.8'
77
TEST_IPV6 = '2001:240:2a54:3900::'
8+
TEST_RESPROXY_IP = '175.107.211.204'
89

910
def assert_ip6(resp)
1011
assert_equal(resp.ip, TEST_IPV6)
@@ -52,14 +53,8 @@ def assert_ip6(resp)
5253
phone: '+000000000'
5354
}
5455
)
55-
assert_equal(
56-
resp.domains,
57-
{
58-
page: 0,
59-
total: 0,
60-
domains: []
61-
}
62-
)
56+
assert_equal(resp.domains[:total], 0)
57+
assert_equal(resp.domains[:domains], [])
6358
end
6459

6560
def assert_ip4(resp)
@@ -181,6 +176,26 @@ def test_lookup_ip4
181176
end
182177
end
183178

179+
def test_resproxy
180+
ipinfo = IPinfo.create(ENV['IPINFO_TOKEN'])
181+
182+
# multiple checks for cache
183+
(0...5).each do |_|
184+
resp = ipinfo.resproxy(TEST_RESPROXY_IP)
185+
assert_equal(resp.ip, TEST_RESPROXY_IP)
186+
refute_nil(resp.last_seen)
187+
refute_nil(resp.percent_days_seen)
188+
refute_nil(resp.service)
189+
end
190+
end
191+
192+
def test_resproxy_empty
193+
ipinfo = IPinfo.create(ENV['IPINFO_TOKEN'])
194+
195+
resp = ipinfo.resproxy(TEST_IPV4)
196+
assert_equal(resp.all, {})
197+
end
198+
184199
# # Requires IPv6 support
185200
# def test_lookup_ip4_on_host_v6
186201
# ipinfo = IPinfo.create(ENV['IPINFO_TOKEN'])

0 commit comments

Comments
 (0)