Module: ProxyCheck::API::Util

Includes:
Configurable
Included in:
ProxyLookup
Defined in:
lib/proxycheck/api/util.rb

Constant Summary collapse

ROOT_URL =
'https://proxycheck.io'

Instance Attribute Summary

Attributes included from Configurable

#api_key

Instance Method Summary collapse

Methods included from Configurable

#configure, keys

Instance Method Details

#build_url(path, api_version) ⇒ Object



36
37
38
# File 'lib/proxycheck/api/util.rb', line 36

def build_url(path, api_version)
	"#{ File.join(ROOT_URL, api_version, path) }"
end

#call(path, api_version = "v1", type = :get, params = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/proxycheck/api/util.rb', line 12

def call(path, api_version = "v1", type = :get, params = {})

			params.merge!({key: api_key}) if !api_key.blank?

			_path = path + "&" + URI.encode_www_form(params)
    			uri = URI.parse(build_url(_path, api_version))
       #uri.query = _ip + "&" + URI.encode_www_form(params)

       http = Net::HTTP.new(uri.host, uri.port)

       http.use_ssl = true if uri.scheme.upcase == "HTTPS"
			http.verify_mode = OpenSSL::SSL::VERIFY_NONE if uri.scheme.upcase == "HTTPS"
       
       case type
       when :get
         request = Net::HTTP::Get.new(uri.request_uri)
       when :post
         request = Net::HTTP::Post.new(uri.request_uri)
       end

     		response = http.request(request)
     		JSON.parse(response.body)
end