Class: AVIP

Inherits:
Object
  • Object
show all
Defined in:
lib/av-ip.rb

Instance Method Summary collapse

Constructor Details

#initialize(ip = '127.0.0.1', content = []) ⇒ AVIP

Returns a new instance of AVIP.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/av-ip.rb', line 6

def initialize(ip = '127.0.0.1', content = [])
	@ip = ip
	@content = content
	@params = [
		:format,
		:ip,
		:content
	]

	@contents = [
		:ip,
		:hostname,
		:city,
		:region,
		:country,
		:loc,
		:org,
		:postal
	]
end

Instance Method Details

#search(ip = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/av-ip.rb', line 56

def search(ip = nil)
	if ip == nil
		p_ip = @ip
	else
		p_ip = ip
	end
	uri = URI("http://ipinfo.io/#{p_ip}/#{@format}")
	Net::HTTP.start(uri.host, uri.port) do |http|
		request = Net::HTTP::Get.new uri
		response = http.request request
		res = eval(response.body )
		if @content == []
			return res
		else
			new_res = {}
			res.each do |key,value|
				@content.each do |con|
					if key == con
						new_res[key] = value
					end
				end
			end
			return new_res
		end
	end
end

#update(param_hash) ⇒ Object

Input options in hash form ie => :json, :ip => ‘127.0.0.1’



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/av-ip.rb', line 28

def update(param_hash)
	param_hash.each do |key,value|
		if @params.include?(key)
			if key == :ip
				@ip = value
				puts "Ip changed to #{value}."
			elsif key == :content
				if value.length == 0
					puts "Invalid Input, using all outputs"
				else
					content = []
					value.each do |val|
						if @contents.include?(val)
							@content.push(val)
							puts "Including: #{val}"
						else
							puts "#{val} is not a valid option."
						end
					end
				end
			end
		else
			puts "Paramater: #{key} is not a valid option."
			puts "Warning: No change has been made."  
		end
	end
end