Class: Ipdb::Query

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ipdb/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(addr = nil, options = {}) ⇒ Query

Creates a new Query object.

Parameters:

  • addresses/domians (Array)

    An array of addresses or domains.

  • Options (Hash<Options>)

    for the new Query Object.

  • opts (Hash)

    a customizable set of options



30
31
32
33
34
35
36
# File 'lib/ipdb/query.rb', line 30

def initialize(addr=nil, options={})
  @timeout = options[:timeout]

  ip = [addr].flatten
  @url = "#{SCRIPT}?ip=#{URI.escape(ip.join(','))}"
  @xml = Nokogiri::XML.parse(open(@url))
end

Instance Method Details

#each {|Query| ... } ⇒ XML

Parses the Locations from the Query.

Yields:

  • (Query)

    Each query will be passed to a given block.

Yield Parameters:

  • location (Location)

    A location from the query.

Returns:

  • (XML)

    The XML object.



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

#parseLocation

Process The Query Object And Return A Location Object

Returns:

  • (Location)

    The Location object returned from the Query.



44
45
46
# File 'lib/ipdb/query.rb', line 44

def parse
  Location.new(@xml.xpath('//Location'), @timeout)
end

#simple_map_imageString

Convenient Method to render the map url as an image in Rails.

Returns:

  • (String)

    image



118
119
120
# File 'lib/ipdb/query.rb', line 118

def simple_map_image
  "image_path(#{simple_map_url})"
end

#simple_map_urlString

Return a map url of addresses from the Query

Returns:

  • (String)

    url



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

#to_jsonQuery

Return the Query object in json format.

Returns:



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

#to_sQuery Also known as: to_xml

Return the Query in XML format.

Returns:



91
92
93
# File 'lib/ipdb/query.rb', line 91

def to_s
  @xml
end

#urlString

Return the url of the Query

Returns:

  • (String)

    url



71
72
73
# File 'lib/ipdb/query.rb', line 71

def url
  @url
end