Class: Ipstack::API
- Inherits:
-
Object
- Object
- Ipstack::API
- Defined in:
- lib/ipstack/api.rb
Instance Attribute Summary collapse
-
#access_key ⇒ Object
Make sure we can access parameters of the object.
-
#api_url ⇒ Object
Make sure we can access parameters of the object.
-
#content_type ⇒ Object
Make sure we can access parameters of the object.
-
#optionals ⇒ Object
Make sure we can access parameters of the object.
-
#params_uri ⇒ Object
Make sure we can access parameters of the object.
-
#return_raw ⇒ Object
Make sure we can access parameters of the object.
Class Method Summary collapse
- .bulk(ips, optionals = {}) ⇒ Object
- .check(optionals = {}) ⇒ Object
- .make_request(object, query_ips = nil) ⇒ Object
- .standard(ip, optionals = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(optionals = {}, access_key = ENV['IPSTACK_ACCESS_KEY']) ⇒ API
constructor
A new instance of API.
Constructor Details
#initialize(optionals = {}, access_key = ENV['IPSTACK_ACCESS_KEY']) ⇒ API
Returns a new instance of API.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ipstack/api.rb', line 10 def initialize(optionals = {}, access_key = ENV['IPSTACK_ACCESS_KEY']) raise ArgumentError, 'Requires a hash of optional values found on https://ipstack.com/documentation' unless optionals.is_a?(Hash) raise ArgumentError, '\'access_key\' (api key) cannot be nil. Obtain your key from https://ipstack.com/quickstart and set it as ENV[\'IPSTACK_ACCESS_KEY\']' if access_key.nil? || access_key == '' @return_raw = optionals[:return_raw] optionals.delete(:return_raw) # remove it from optionals since we each optionals for our url params @api_url = 'http://api.ipstack.com/' @access_key = access_key @params_uri = "?access_key=#{access_key}" # Set params_uri with each non-nil optionals key/value optionals.each{ |param_name,param_value| @params_uri << "&#{param_name}=#{param_value}" unless param_value.nil? } @optionals = optionals end |
Instance Attribute Details
#access_key ⇒ Object
Make sure we can access parameters of the object
9 10 11 |
# File 'lib/ipstack/api.rb', line 9 def access_key @access_key end |
#api_url ⇒ Object
Make sure we can access parameters of the object
9 10 11 |
# File 'lib/ipstack/api.rb', line 9 def api_url @api_url end |
#content_type ⇒ Object
Make sure we can access parameters of the object
9 10 11 |
# File 'lib/ipstack/api.rb', line 9 def content_type @content_type end |
#optionals ⇒ Object
Make sure we can access parameters of the object
9 10 11 |
# File 'lib/ipstack/api.rb', line 9 def optionals @optionals end |
#params_uri ⇒ Object
Make sure we can access parameters of the object
9 10 11 |
# File 'lib/ipstack/api.rb', line 9 def params_uri @params_uri end |
#return_raw ⇒ Object
Make sure we can access parameters of the object
9 10 11 |
# File 'lib/ipstack/api.rb', line 9 def return_raw @return_raw end |
Class Method Details
.bulk(ips, optionals = {}) ⇒ Object
52 53 54 55 56 |
# File 'lib/ipstack/api.rb', line 52 def self.bulk(ips, optionals = {}) api_request_object = Ipstack::API.new(optionals) raise ArgumentError, 'Requires a list of IPs comma separated as first parameter' if ips.nil? || ips.empty? || ip.is_a?(Hash) || !( ips.include?(',') && ips.match(/((25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)(,\n|,?$))/) ) Ipstack::API.make_request(api_request_object,ips) end |
.check(optionals = {}) ⇒ Object
58 59 60 61 62 |
# File 'lib/ipstack/api.rb', line 58 def self.check(optionals = {}) raise ArgumentError,'Only optionals hash allowed as first parameter' unless optionals.is_a?(Hash) || optionals.empty? api_request_object = Ipstack::API.new(optionals) Ipstack::API.make_request(api_request_object) end |
.make_request(object, query_ips = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ipstack/api.rb', line 25 def self.make_request(object, query_ips = nil) url = URI(object.api_url) http = Net::HTTP.new(url.host,url.port) # TODO: HTTP/HTTPS support #http.use_ssl = false # https.verify_mode = OpenSSL::SSL::VERIFY_NONE # SSL not signed? Don't care! request = Net::HTTP::Get.new(query_ips.nil? || query_ips.empty? ? "/check#{object.params_uri}" : "/#{query_ips}#{object.params_uri}") response = http.request(request) case response when Net::HTTPSuccess if object.return_raw response.body else object.optionals[:output] == 'xml' ? Nokogiri::XML(response.body) : JSON.parse(response.body) end else return response #"HTTP Error: #{response.code} #{response.message} : #{response.body}" end end |
.standard(ip, optionals = {}) ⇒ Object
46 47 48 49 50 |
# File 'lib/ipstack/api.rb', line 46 def self.standard(ip, optionals = {}) api_request_object = Ipstack::API.new(optionals) raise ArgumentError, 'Requires a single IP as first parameter' if ip.nil? || ip.empty? || ip.is_a?(Hash) Ipstack::API.make_request(api_request_object,ip) end |