Class: Ipdb::Query
- Inherits:
-
Object
- Object
- Ipdb::Query
- Includes:
- Enumerable
- Defined in:
- lib/ipdb/query.rb
Instance Method Summary (collapse)
-
- (XML) each {|Query| ... }
Parses the Locations from the Query.
-
- (Query) initialize(addr = nil, options = {})
constructor
Creates a new Query object.
-
- (Location) parse
Process The Query Object And Return A Location Object.
-
- (String) simple_map_image
Convenient Method to render the map url as an image in Rails.
-
- (String) simple_map_url
Return a map url of addresses from the Query.
-
- (Query) to_json
Return the Query object in json format.
-
- (Query) to_s
(also: #to_xml)
Return the Query in XML format.
-
- (String) url
Return the url of the Query.
Constructor Details
- (Query) initialize(addr = nil, options = {})
Creates a new Query object.
30 31 32 33 34 35 36 |
# File 'lib/ipdb/query.rb', line 30 def initialize(addr=nil, ={}) @timeout = [:timeout] ip = [addr].flatten @url = "#{SCRIPT}?ip=#{URI.escape(ip.join(','))}" @xml = Nokogiri::XML.parse(open(@url)) end |
Instance Method Details
- (XML) each {|Query| ... }
Parses the Locations from the Query.
60 61 62 63 64 |
# File 'lib/ipdb/query.rb', line 60 def each(&block) @xml.xpath('//Location').each do |location| block.call(Location.new(location, @timeout)) if block end end |
- (Location) parse
Process The Query Object And Return A Location Object
44 45 46 |
# File 'lib/ipdb/query.rb', line 44 def parse Location.new(@xml.xpath('//Location'), @timeout) end |
- (String) simple_map_image
Convenient Method to render the map url as an image in Rails.
118 119 120 |
# File 'lib/ipdb/query.rb', line 118 def simple_map_image "image_path(#{simple_map_url})" end |
- (String) simple_map_url
Return a map url of addresses from the Query
103 104 105 106 107 108 109 110 111 |
# File 'lib/ipdb/query.rb', line 103 def simple_map_url @country_codes = [] @data = [] Enumerator.new(self,:each).to_a.collect {|x| @country_codes << x.country_code } @country_codes = @country_codes.uniq 1.upto(@country_codes.size) { |x| @data << '0' } url = "http://chart.apis.google.com/chart?cht=t&chs=440x220&chd=t:#{@data.join(',')}&chco=FFFFFF,4A4A4A,EBEBEB&chld=#{@country_codes}&chtm=world&chf=bg,s,EAF7FE" return url end |
- (Query) to_json
Return the Query object in json format.
80 81 82 83 84 |
# File 'lib/ipdb/query.rb', line 80 def to_json json = "#{@url}&output=json" @doc = Nokogiri::HTML(open(json)) return @doc.xpath('//body/.').inner_text end |
- (Query) to_s Also known as: to_xml
Return the Query in XML format.
91 92 93 |
# File 'lib/ipdb/query.rb', line 91 def to_s @xml end |
- (String) url
Return the url of the Query
71 72 73 |
# File 'lib/ipdb/query.rb', line 71 def url @url end |