Class: Ipdb::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/ipdb/map.rb

Instance Method Summary collapse

Constructor Details

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

Creates a new Map object.

Examples:

Ipdb::Map.new(@ips, :zoom => 1, :width => 100, :height => 100, :units => :%)

Parameters:

  • addresses/domians (Array)

    An array of addresses or domains.

  • options (Hash) (defaults to: {})

    The options to create a Map with.

Options Hash (options):

  • :timeout (Integer) — default: '10'

    The Timeout.

  • :units (Symbol) — default: ':px'

    The units for width and height.

  • :width (Integer) — default: '600'

    The width of the Map.

  • :height (Integer) — default: '350'

    The height of the Map.

  • :zoom (Integer) — default: '10'

    The zoom of the Map.

  • :div_id (String) — default: 'map_canvas'

    The div id of the Map container.

  • :div_class (String) — default: 'ipdb'

    The div class of the Map container.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ipdb/map.rb', line 26

def initialize(addr=nil, options={})
  @timeout = options[:timeout] || 10
  @units = (options[:units] || :px).to_sym
  @width = options[:width] || 600
  @height = options[:height] || 350
  @zoom = options[:zoom] || 10
  @div_id = options[:div_id] || 'map_canvas'
  @div_class = options[:div_class] || 'ipdb'

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

Instance Method Details

#render(js = true) ⇒ String<HTML, JavaScript>

Return a Google Map for a Query Object

Returns:

  • (String<HTML, JavaScript>)

    map The Google Map html and JavaScript.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ipdb/map.rb', line 47

def render(js=true)
  @map = ""; @id = 0
  @xml.each do |x|
    location = Location.new(x, @timeout)
    id = "ip_#{@id += 1}_id"
    @start = new_location(location.latitude, location.longitude) if @id == 1
    @map += add_marker(id, location.address, location.latitude, location.longitude)
    @map += add_window(id, location.address, location.city, location.country)
    @map += add_listener(id)
  end
  if js
    build_map(@start, @map)
  else
    build_map_without_js(@start, @map)
  end
end

#render_divObject



77
78
79
# File 'lib/ipdb/map.rb', line 77

def render_div
  "<div id='#{@div_id}' class='#{@div_class}' style='width: #{@width}#{@units}; height: #{@height}#{@units}'></div>"
end

#render_jsObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ipdb/map.rb', line 64

def render_js
  @map = ""; @id = 0
  @xml.each do |x|
    location = Location.new(x, @timeout)
    id = "ip_#{@id += 1}_id"
    @start = new_location(location.latitude, location.longitude) if @id == 1
    @map += add_marker(id, location.address, location.latitude, location.longitude)
    @map += add_window(id, location.address, location.city, location.country)
    @map += add_listener(id)
  end
  build_map_just_js(@start, @map)
end